mirror of
https://github.com/flame-engine/flame.git
synced 2025-10-30 00:17:20 +08:00
docs: Added bouncing ball example in collision_detection examples and updated flame_forge2d to latest (#1868)
This PR adds two updates. It updates the flame_forge2d to latest version i.e on pub.dev in examples and padracing projects. Adds a bouncing ball example in the collision detection examples demonstrating a simple example of collision and bouncing of a ball from the walls around it.
This commit is contained in:
@ -0,0 +1,107 @@
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flame/collisions.dart';
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BouncingBallExample extends FlameGame with HasCollisionDetection {
|
||||
static const description = '''
|
||||
This example shows how you can use the Collisions detection api to know when a ball
|
||||
collides with the screen boundaries and then update it to bounce off these boundaries.
|
||||
''';
|
||||
@override
|
||||
Future<void>? onLoad() {
|
||||
addAll([
|
||||
ScreenHitbox(),
|
||||
Ball(),
|
||||
]);
|
||||
return super.onLoad();
|
||||
}
|
||||
}
|
||||
|
||||
class Ball extends CircleComponent
|
||||
with HasGameRef<FlameGame>, CollisionCallbacks {
|
||||
late Vector2 velocity;
|
||||
|
||||
Ball() {
|
||||
paint = Paint()..color = Colors.white;
|
||||
radius = 10;
|
||||
}
|
||||
|
||||
static const double speed = 500;
|
||||
static const degree = math.pi / 180;
|
||||
|
||||
@override
|
||||
Future<void>? onLoad() {
|
||||
_resetBall;
|
||||
final hitBox = CircleHitbox(
|
||||
radius: radius,
|
||||
);
|
||||
|
||||
addAll([
|
||||
hitBox,
|
||||
]);
|
||||
|
||||
return super.onLoad();
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
super.update(dt);
|
||||
position += velocity * dt;
|
||||
}
|
||||
|
||||
void get _resetBall {
|
||||
position = gameRef.size / 2;
|
||||
final spawnAngle = getSpawnAngle;
|
||||
|
||||
final vx = math.cos(spawnAngle * degree) * speed;
|
||||
final vy = math.sin(spawnAngle * degree) * speed;
|
||||
velocity = Vector2(
|
||||
vx,
|
||||
vy,
|
||||
);
|
||||
}
|
||||
|
||||
double get getSpawnAngle {
|
||||
final random = math.Random().nextDouble();
|
||||
final spawnAngle = lerpDouble(0, 360, random)!;
|
||||
|
||||
return spawnAngle;
|
||||
}
|
||||
|
||||
@override
|
||||
void onCollisionStart(
|
||||
Set<Vector2> intersectionPoints,
|
||||
PositionComponent other,
|
||||
) {
|
||||
super.onCollisionStart(intersectionPoints, other);
|
||||
|
||||
if (other is ScreenHitbox) {
|
||||
final collisionPoint = intersectionPoints.first;
|
||||
|
||||
// Left Side Collision
|
||||
if (collisionPoint.x == 0) {
|
||||
velocity.x = -velocity.x;
|
||||
velocity.y = velocity.y;
|
||||
}
|
||||
// Right Side Collision
|
||||
if (collisionPoint.x == gameRef.size.x) {
|
||||
velocity.x = -velocity.x;
|
||||
velocity.y = velocity.y;
|
||||
}
|
||||
// Top Side Collision
|
||||
if (collisionPoint.y == 0) {
|
||||
velocity.x = velocity.x;
|
||||
velocity.y = -velocity.y;
|
||||
}
|
||||
// Bottom Side Collision
|
||||
if (collisionPoint.y == gameRef.size.y) {
|
||||
velocity.x = velocity.x;
|
||||
velocity.y = -velocity.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user