diff --git a/examples/games/padracing/pubspec.yaml b/examples/games/padracing/pubspec.yaml index 1db67821d..9fb17e3b5 100644 --- a/examples/games/padracing/pubspec.yaml +++ b/examples/games/padracing/pubspec.yaml @@ -8,7 +8,7 @@ environment: dependencies: flame: ^1.3.0 - flame_forge2d: ^0.11.0 + flame_forge2d: ^0.12.2 flutter: sdk: flutter google_fonts: ^2.3.2 diff --git a/examples/lib/stories/collision_detection/bouncing_ball_example.dart b/examples/lib/stories/collision_detection/bouncing_ball_example.dart new file mode 100644 index 000000000..e79f93cf7 --- /dev/null +++ b/examples/lib/stories/collision_detection/bouncing_ball_example.dart @@ -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? onLoad() { + addAll([ + ScreenHitbox(), + Ball(), + ]); + return super.onLoad(); + } +} + +class Ball extends CircleComponent + with HasGameRef, 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? 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 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; + } + } + } +} diff --git a/examples/lib/stories/collision_detection/collision_detection.dart b/examples/lib/stories/collision_detection/collision_detection.dart index b9b8e49dd..243b6817f 100644 --- a/examples/lib/stories/collision_detection/collision_detection.dart +++ b/examples/lib/stories/collision_detection/collision_detection.dart @@ -1,5 +1,6 @@ import 'package:dashbook/dashbook.dart'; import 'package:examples/commons/commons.dart'; +import 'package:examples/stories/collision_detection/bouncing_ball_example.dart'; import 'package:examples/stories/collision_detection/circles_example.dart'; import 'package:examples/stories/collision_detection/collidable_animation_example.dart'; import 'package:examples/stories/collision_detection/multiple_shapes_example.dart'; @@ -23,6 +24,12 @@ void addCollisionDetectionStories(Dashbook dashbook) { codeLink: baseLink('collision_detection/circles_example.dart'), info: CirclesExample.description, ) + ..add( + 'Bouncing Ball', + (_) => GameWidget(game: BouncingBallExample()), + codeLink: baseLink('collision_detection/bouncing_ball_example.dart'), + info: BouncingBallExample.description, + ) ..add( 'Multiple shapes', (_) => GameWidget(game: MultipleShapesExample()), diff --git a/examples/pubspec.yaml b/examples/pubspec.yaml index e94ecd6d0..7be4e113b 100644 --- a/examples/pubspec.yaml +++ b/examples/pubspec.yaml @@ -13,7 +13,7 @@ dependencies: dashbook: 0.1.8 flame: ^1.3.0 flame_audio: ^1.3.1 - flame_forge2d: ^0.11.0 + flame_forge2d: ^0.12.2 flame_svg: ^1.5.0 flutter: sdk: flutter