mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-01 01:18:38 +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:
		| @ -8,7 +8,7 @@ environment: | |||||||
|  |  | ||||||
| dependencies: | dependencies: | ||||||
|   flame: ^1.3.0 |   flame: ^1.3.0 | ||||||
|   flame_forge2d: ^0.11.0 |   flame_forge2d: ^0.12.2 | ||||||
|   flutter: |   flutter: | ||||||
|     sdk: flutter |     sdk: flutter | ||||||
|   google_fonts: ^2.3.2 |   google_fonts: ^2.3.2 | ||||||
|  | |||||||
| @ -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; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -1,5 +1,6 @@ | |||||||
| import 'package:dashbook/dashbook.dart'; | import 'package:dashbook/dashbook.dart'; | ||||||
| import 'package:examples/commons/commons.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/circles_example.dart'; | ||||||
| import 'package:examples/stories/collision_detection/collidable_animation_example.dart'; | import 'package:examples/stories/collision_detection/collidable_animation_example.dart'; | ||||||
| import 'package:examples/stories/collision_detection/multiple_shapes_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'), |       codeLink: baseLink('collision_detection/circles_example.dart'), | ||||||
|       info: CirclesExample.description, |       info: CirclesExample.description, | ||||||
|     ) |     ) | ||||||
|  |     ..add( | ||||||
|  |       'Bouncing Ball', | ||||||
|  |       (_) => GameWidget(game: BouncingBallExample()), | ||||||
|  |       codeLink: baseLink('collision_detection/bouncing_ball_example.dart'), | ||||||
|  |       info: BouncingBallExample.description, | ||||||
|  |     ) | ||||||
|     ..add( |     ..add( | ||||||
|       'Multiple shapes', |       'Multiple shapes', | ||||||
|       (_) => GameWidget(game: MultipleShapesExample()), |       (_) => GameWidget(game: MultipleShapesExample()), | ||||||
|  | |||||||
| @ -13,7 +13,7 @@ dependencies: | |||||||
|   dashbook: 0.1.8 |   dashbook: 0.1.8 | ||||||
|   flame: ^1.3.0 |   flame: ^1.3.0 | ||||||
|   flame_audio: ^1.3.1 |   flame_audio: ^1.3.1 | ||||||
|   flame_forge2d: ^0.11.0 |   flame_forge2d: ^0.12.2 | ||||||
|   flame_svg: ^1.5.0 |   flame_svg: ^1.5.0 | ||||||
|   flutter: |   flutter: | ||||||
|     sdk: flutter |     sdk: flutter | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 RutvikTak
					RutvikTak