flame_forge2d 0.13.0+1 flame_forge2d: ^0.13.0+1 copied to clipboard
Forge2D (Box2D) support for the Flame game engine. This uses the forge2d package and provides wrappers and components to be used inside Flame.
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/game.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(GameWidget(game: Forge2DExample()));
}
class Forge2DExample extends Forge2DGame {
@override
Future<void> onLoad() async {
add(Ball(size / 2));
addAll(createBoundaries());
return super.onLoad();
}
List<Component> createBoundaries() {
final topLeft = Vector2.zero();
final bottomRight = screenToWorld(camera.viewport.effectiveSize);
final topRight = Vector2(bottomRight.x, topLeft.y);
final bottomLeft = Vector2(topLeft.x, bottomRight.y);
return [
Wall(topLeft, topRight),
Wall(topRight, bottomRight),
Wall(bottomLeft, bottomRight),
Wall(topLeft, bottomLeft)
];
}
}
class Ball extends BodyComponent with TapCallbacks {
final Vector2 _position;
Ball(this._position);
@override
Body createBody() {
final shape = CircleShape();
shape.radius = 5;
final fixtureDef = FixtureDef(
shape,
restitution: 0.8,
density: 1.0,
friction: 0.4,
);
final bodyDef = BodyDef(
userData: this,
angularDamping: 0.8,
position: _position,
type: BodyType.dynamic,
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
@override
void onTapDown(_) {
body.applyLinearImpulse(Vector2.random() * 5000);
}
}
class Wall extends BodyComponent {
final Vector2 _start;
final Vector2 _end;
Wall(this._start, this._end);
@override
Body createBody() {
final shape = EdgeShape()..set(_start, _end);
final fixtureDef = FixtureDef(shape, friction: 0.3);
final bodyDef = BodyDef(
userData: this,
position: Vector2.zero(),
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}