diff --git a/packages/benchmark/web/bench2d.dart b/packages/benchmark/web/bench2d.dart index c5b3dd7..232a92d 100644 --- a/packages/benchmark/web/bench2d.dart +++ b/packages/benchmark/web/bench2d.dart @@ -64,9 +64,7 @@ class Bench2d { y.setFrom(x); for (var j = i; j < pyramidSize; ++j) { - final bd = BodyDef() - ..type = BodyType.dynamic - ..position.setFrom(y); + final bd = BodyDef(type: BodyType.dynamic, position: y.clone()); world.createBody(bd).createFixtureFromShape(shape, density: 5.0); y.add(deltaY); } diff --git a/packages/forge2d/example/web/ball_cage.dart b/packages/forge2d/example/web/ball_cage.dart index fbb89fb..1f922b0 100644 --- a/packages/forge2d/example/web/ball_cage.dart +++ b/packages/forge2d/example/web/ball_cage.dart @@ -27,12 +27,14 @@ class BallCage extends Demo { @override void initialize() { // Define the circle shape. - final circleShape = CircleShape()..radius = wallBallRadius; + final circleShape = CircleShape(radius: wallBallRadius); // Create fixture using the circle shape. - final circleFixtureDef = FixtureDef(circleShape) - ..friction = .9 - ..restitution = 1.0; + final circleFixtureDef = FixtureDef( + circleShape, + friction: .9, + restitution: 1.0, + ); // Create a body def. final circleBodyDef = BodyDef(); @@ -67,12 +69,14 @@ class BallCage extends Demo { } // Create a bouncing ball. - final bouncingCircle = CircleShape()..radius = activeBallRadius; + final bouncingCircle = CircleShape(radius: activeBallRadius); // Create fixture for that ball shape. - final activeFixtureDef = FixtureDef(bouncingCircle) - ..restitution = 1.0 - ..density = 0.05; + final activeFixtureDef = FixtureDef( + bouncingCircle, + restitution: 1.0, + density: 0.05, + ); // Create the active ball body. final activeBodyDef = BodyDef(); diff --git a/packages/forge2d/example/web/blob_test.dart b/packages/forge2d/example/web/blob_test.dart index 7cb4f68..1791c20 100644 --- a/packages/forge2d/example/web/blob_test.dart +++ b/packages/forge2d/example/web/blob_test.dart @@ -53,7 +53,7 @@ class BlobTest extends Demo { bd.type = BodyType.dynamic; final body = world.createBody(bd); - final shape = CircleShape()..radius = bodyRadius; + final shape = CircleShape(radius: bodyRadius); final fixtureDef = FixtureDef(shape) ..density = 1.0 ..filter.groupIndex = -2; diff --git a/packages/forge2d/example/web/box_test.dart b/packages/forge2d/example/web/box_test.dart index 844d84e..a62ebc8 100644 --- a/packages/forge2d/example/web/box_test.dart +++ b/packages/forge2d/example/web/box_test.dart @@ -51,14 +51,13 @@ class BoxTest extends Demo { shape.setAsBox(3.0, 1.5, Vector2.zero(), pi / 2); // Define fixture (links body and shape) - final activeFixtureDef = FixtureDef(shape) - ..restitution = 0.5 - ..density = 0.05; + final activeFixtureDef = FixtureDef(shape, restitution: 0.5, density: 0.05); // Define body - final bodyDef = BodyDef(); - bodyDef.type = BodyType.dynamic; - bodyDef.position = Vector2(0.0, 30.0); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(0.0, 30.0), + ); // Create body and fixture from definitions final fallingBox = world.createBody(bodyDef); diff --git a/packages/forge2d/example/web/circle_stress.dart b/packages/forge2d/example/web/circle_stress.dart index ee6075d..2c9ea6f 100644 --- a/packages/forge2d/example/web/circle_stress.dart +++ b/packages/forge2d/example/web/circle_stress.dart @@ -31,17 +31,15 @@ class CircleStress extends Demo { // Ground final shape = PolygonShape(); shape.setAsBoxXY(50.0, 10.0); - final bodyDef = BodyDef() - ..type = BodyType.static - ..position = Vector2(0.0, -10.0); + final bodyDef = BodyDef(position: Vector2(0.0, -10.0)); final body = world.createBody(bodyDef); bodies.add(body); - final fixtureDef = FixtureDef(shape)..friction = 1.0; + final fixtureDef = FixtureDef(shape, friction: 1.0); body.createFixture(fixtureDef); // Walls shape.setAsBoxXY(3.0, 50.0); - final wallDef = BodyDef()..position = Vector2(45.0, 25.0); + final wallDef = BodyDef(position: Vector2(45.0, 25.0)); final rightWall = world.createBody(wallDef); bodies.add(rightWall); rightWall.createFixtureFromShape(shape); @@ -51,10 +49,11 @@ class CircleStress extends Demo { leftWall.createFixtureFromShape(shape); // Corners - final cornerDef = BodyDef(); + final cornerDef = BodyDef( + angle: -pi / 4.0, + position: Vector2(-35.0, 8.0), + ); shape.setAsBoxXY(20.0, 3.0); - cornerDef.angle = -pi / 4.0; - cornerDef.position = Vector2(-35.0, 8.0); var myBod = world.createBody(cornerDef); bodies.add(myBod); myBod.createFixtureFromShape(shape); @@ -66,10 +65,7 @@ class CircleStress extends Demo { // top shape.setAsBoxXY(50.0, 10.0); - final topDef = BodyDef() - ..type = BodyType.static - ..angle = 0.0 - ..position = Vector2(0.0, 75.0); + final topDef = BodyDef(position: Vector2(0.0, 75.0)); final topBody = world.createBody(topDef); bodies.add(topBody); fixtureDef.shape = shape; @@ -85,22 +81,14 @@ class CircleStress extends Demo { ); shape.createChain(vertices); - final fixtureDef = FixtureDef(shape) - ..restitution = 0.0 - ..friction = 0.1; - - final bodyDef = BodyDef() - ..position = Vector2.zero() - ..type = BodyType.static; - + final fixtureDef = FixtureDef(shape, friction: 0.1); + final bodyDef = BodyDef(position: Vector2.zero()); final body = world.createBody(bodyDef)..createFixture(fixtureDef); bodies.add(body); } { - final bd = BodyDef() - ..type = BodyType.dynamic - ..position = Vector2(0.0, 10.0); + final bd = BodyDef(type: BodyType.dynamic, position: Vector2(0.0, 10.0)); const numPieces = 5; const radius = 6.0; final body = world.createBody(bd); @@ -110,14 +98,13 @@ class CircleStress extends Demo { final xPos = radius * cos(2 * pi * (i / numPieces.toDouble())); final yPos = radius * sin(2 * pi * (i / numPieces.toDouble())); - final shape = CircleShape() - ..radius = 1.2 - ..position.setValues(xPos, yPos); - - final fixtureDef = FixtureDef(shape) - ..density = 25.0 - ..friction = .1 - ..restitution = .9; + final shape = CircleShape(radius: 1.2, position: Vector2(xPos, yPos)); + final fixtureDef = FixtureDef( + shape, + density: 25.0, + friction: 0.1, + restitution: 0.9, + ); body.createFixture(fixtureDef); } @@ -139,17 +126,21 @@ class CircleStress extends Demo { for (var j = 0; j < columns; j++) { for (var i = 0; i < loadSize; i++) { - final shape = CircleShape() - ..radius = 1.0 + (i.isEven ? 1.0 : -1.0) * .5 * .75; - final fd2 = FixtureDef(shape) - ..density = shape.radius * 1.5 - ..friction = 0.5 - ..restitution = 0.7; + final shape = CircleShape( + radius: 1.0 + (i.isEven ? 1.0 : -1.0) * .5 * .75, + ); + final fd2 = FixtureDef( + shape, + density: shape.radius * 1.5, + friction: 0.5, + restitution: 0.7, + ); final xPos = -39.0 + 2 * i; final yPos = 50.0 + j; - final bodyDef = BodyDef() - ..type = BodyType.dynamic - ..position = Vector2(xPos, yPos); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(xPos, yPos), + ); final body = world.createBody(bodyDef); bodies.add(body); body.createFixture(fd2); diff --git a/packages/forge2d/example/web/domino_test.dart b/packages/forge2d/example/web/domino_test.dart index 12e052e..ab831f8 100644 --- a/packages/forge2d/example/web/domino_test.dart +++ b/packages/forge2d/example/web/domino_test.dart @@ -11,10 +11,9 @@ class DominoTest extends Demo { { // Floor final shape = PolygonShape()..setAsBoxXY(50.0, 10.0); - final fixtureDef = FixtureDef(shape)..friction = 0.1; + final fixtureDef = FixtureDef(shape, friction: 0.1); - final bd = BodyDef(); - bd.position = Vector2(0.0, -10.0); + final bd = BodyDef(position: Vector2(0.0, -10.0)); final body = world.createBody(bd); body.createFixture(fixtureDef); bodies.add(body); @@ -24,9 +23,9 @@ class DominoTest extends Demo { // Platforms for (var i = 0; i < 4; i++) { final shape = PolygonShape()..setAsBoxXY(15.0, 0.125); - final fixtureDef = FixtureDef(shape)..friction = 0.1; + final fixtureDef = FixtureDef(shape, friction: 0.1); - final bodyDef = BodyDef()..position = Vector2(0.0, 5.0 + 5 * i); + final bodyDef = BodyDef(position: Vector2(0.0, 5.0 + 5 * i)); final body = world.createBody(bodyDef); body.createFixture(fixtureDef); bodies.add(body); @@ -36,11 +35,8 @@ class DominoTest extends Demo { // Dominoes { final shape = PolygonShape()..setAsBoxXY(0.125, 2.0); - final fixtureDef = FixtureDef(shape) - ..density = 25.0 - ..friction = 0.5; - - final bodyDef = BodyDef()..type = BodyType.dynamic; + final fixtureDef = FixtureDef(shape, density: 25.0, friction: 0.5); + final bodyDef = BodyDef(type: BodyType.dynamic); const numPerRow = 25; diff --git a/packages/forge2d/example/web/domino_tower.dart b/packages/forge2d/example/web/domino_tower.dart index a5a9c00..36e6eaa 100644 --- a/packages/forge2d/example/web/domino_tower.dart +++ b/packages/forge2d/example/web/domino_tower.dart @@ -28,13 +28,17 @@ class DominoTower extends Demo { void makeDomino(double x, double y, {required bool horizontal}) { final shape = PolygonShape() ..setAsBoxXY(.5 * dominoWidth, .5 * dominoHeight); - final fixtureDef = FixtureDef(shape) - ..density = dominoDensity - ..friction = dominoFriction - ..restitution = 0.65; - final bodyDef = BodyDef()..type = BodyType.dynamic; - bodyDef.position = Vector2(x, y); - bodyDef.angle = horizontal ? (pi / 2.0) : 0.0; + final fixtureDef = FixtureDef( + shape, + density: dominoDensity, + friction: dominoFriction, + restitution: 0.65, + ); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(x, y), + angle: horizontal ? (pi / 2.0) : 0.0, + ); final body = world.createBody(bodyDef); body.createFixture(fixtureDef); bodies.add(body); @@ -48,8 +52,7 @@ class DominoTower extends Demo { final sd = PolygonShape(); sd.setAsBoxXY(50.0, 10.0); - final bd = BodyDef(); - bd.position = Vector2(0.0, -10.0); + final bd = BodyDef(position: Vector2(0.0, -10.0)); final body = world.createBody(bd); body.createFixtureFromShape(sd); bodies.add(body); @@ -59,15 +62,12 @@ class DominoTower extends Demo { dominoDensity = 10.0; // Make bullet final shape = PolygonShape()..setAsBoxXY(.7, .7); - final fixtureDef = FixtureDef(shape) - ..density = 35.0 - ..shape = shape - ..friction = 0.0 - ..restitution = 0.85; - final bodyDef = BodyDef() - ..type = BodyType.dynamic - ..bullet = true - ..position = Vector2(30.0, 5.00); + final fixtureDef = FixtureDef(shape, density: 35.0, restitution: 0.85); + final bodyDef = BodyDef( + type: BodyType.dynamic, + bullet: true, + position: Vector2(30.0, 5.00), + ); var body = world.createBody(bodyDef); bodies.add(body); body.createFixture(fixtureDef); diff --git a/packages/forge2d/example/web/friction_joint_test.dart b/packages/forge2d/example/web/friction_joint_test.dart index cf84888..f8a8240 100644 --- a/packages/forge2d/example/web/friction_joint_test.dart +++ b/packages/forge2d/example/web/friction_joint_test.dart @@ -55,16 +55,15 @@ class FrictionJointTest extends Demo { boxShape.setAsBox(3.0, 1.5, Vector2.zero(), pi / 2); // Define fixture (links body and shape) - _boxFixture = FixtureDef(boxShape) - ..restitution = 0.5 - ..density = 0.10; + _boxFixture = FixtureDef(boxShape, restitution: 0.5, density: 0.10); } void _createBox() { // Define body - final bodyDef = BodyDef(); - bodyDef.type = BodyType.dynamic; - bodyDef.position = Vector2(-10.0, 30.0); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(-10.0, 30.0), + ); // Create body and fixture from definitions final fallingBox = world.createBody(bodyDef); @@ -76,9 +75,10 @@ class FrictionJointTest extends Demo { void _createFrictionBox() { // Define body - final bodyDef = BodyDef(); - bodyDef.type = BodyType.dynamic; - bodyDef.position = Vector2(10.0, 30.0); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(10.0, 30.0), + ); // Create body and fixture from definitions final fallingBox = world.createBody(bodyDef); diff --git a/packages/forge2d/example/web/particles.dart b/packages/forge2d/example/web/particles.dart index 6177e0a..ed63a9b 100644 --- a/packages/forge2d/example/web/particles.dart +++ b/packages/forge2d/example/web/particles.dart @@ -26,12 +26,14 @@ class Particles extends Demo { @override void initialize() { // Define the circle shape. - final circleShape = CircleShape()..radius = wallBallRadius; + final circleShape = CircleShape(radius: wallBallRadius); // Create fixture using the circle shape. - final circleFixtureDef = FixtureDef(circleShape) - ..friction = .9 - ..restitution = 1.0; + final circleFixtureDef = FixtureDef( + circleShape, + friction: 0.9, + restitution: 1.0, + ); // Create a body def. final circleBodyDef = BodyDef(); @@ -70,9 +72,11 @@ class Particles extends Demo { bouncingCircle.radius = activeBallRadius; // Create fixture for that ball shape. - final activeFixtureDef = FixtureDef(bouncingCircle) - ..restitution = 1.0 - ..density = 0.05; + final activeFixtureDef = FixtureDef( + bouncingCircle, + restitution: 1.0, + density: 0.05, + ); // Create the active ball body. final activeBodyDef = BodyDef(); @@ -87,7 +91,7 @@ class Particles extends Demo { world.particleSystem.particleRadius = 0.35; world.particleSystem.dampingStrength = 0.2; - final shape = CircleShape()..radius = 5; + final shape = CircleShape(radius: 5); final particleGroup = ParticleGroupDef() ..position.setFrom(fixture.renderCenter) ..destroyAutomatically = true diff --git a/packages/forge2d/example/web/racer.dart b/packages/forge2d/example/web/racer.dart index 6cf1f2d..0ccb0c0 100644 --- a/packages/forge2d/example/web/racer.dart +++ b/packages/forge2d/example/web/racer.dart @@ -76,9 +76,11 @@ class Racer extends Demo implements ContactListener { final shape = PolygonShape() ..setAsBox(27.0, 21.0, Vector2(-30.0, 30.0), radians(20.0)); - final fixtureDef = FixtureDef(shape) - ..isSensor = true - ..userData = GroundArea(0.001, outOfCourse: false); + final fixtureDef = FixtureDef( + shape, + isSensor: true, + userData: GroundArea(0.001, outOfCourse: false), + ); _groundBody.createFixture(fixtureDef); fixtureDef.userData = GroundArea(0.2, outOfCourse: false); diff --git a/packages/forge2d/example/web/racer/control_state.dart b/packages/forge2d/example/web/racer/control_state.dart index 6d5e8db..68e12aa 100644 --- a/packages/forge2d/example/web/racer/control_state.dart +++ b/packages/forge2d/example/web/racer/control_state.dart @@ -1,5 +1,4 @@ /// Used to track keyboard state. Entries are masked bitwise. - class ControlState { static const int up = 1; static const int down = 2; diff --git a/packages/forge2d/lib/src/collision/shapes/circle_shape.dart b/packages/forge2d/lib/src/collision/shapes/circle_shape.dart index 827535b..2de57d8 100644 --- a/packages/forge2d/lib/src/collision/shapes/circle_shape.dart +++ b/packages/forge2d/lib/src/collision/shapes/circle_shape.dart @@ -7,18 +7,18 @@ import 'package:forge2d/src/settings.dart' as settings; class CircleShape extends Shape { final Vector2 position = Vector2.zero(); - CircleShape() : super(ShapeType.circle) { - radius = 0.0; + CircleShape({ + double radius = 0.0, + Vector2? position, + }) : super(ShapeType.circle) { + this.radius = radius; + if (position != null) { + this.position.setFrom(position); + } } @override - Shape clone() { - final shape = CircleShape(); - shape.position.x = position.x; - shape.position.y = position.y; - shape.radius = radius; - return shape; - } + Shape clone() => CircleShape(radius: radius, position: position); @override int get childCount => 1; diff --git a/packages/forge2d/lib/src/dynamics/body.dart b/packages/forge2d/lib/src/dynamics/body.dart index 125edd9..8091716 100644 --- a/packages/forge2d/lib/src/dynamics/body.dart +++ b/packages/forge2d/lib/src/dynamics/body.dart @@ -177,10 +177,12 @@ class Body { double restitution = 0.0, }) { return createFixture( - FixtureDef(shape) - ..density = density - ..friction = friction - ..restitution = restitution, + FixtureDef( + shape, + density: density, + friction: friction, + restitution: restitution, + ), ); } diff --git a/packages/forge2d/test/collision/shapes/circle_shape_test.dart b/packages/forge2d/test/collision/shapes/circle_shape_test.dart new file mode 100644 index 0000000..449ca9f --- /dev/null +++ b/packages/forge2d/test/collision/shapes/circle_shape_test.dart @@ -0,0 +1,27 @@ +import 'package:forge2d/forge2d.dart'; +import 'package:test/test.dart'; + +void main() { + group('CircleShape', () { + test('Test radius', () { + const radius = 5.0; + final circleShape = CircleShape(radius: radius); + expect(circleShape.radius, equals(radius)); + }); + + test('Test position', () { + final position = Vector2(1.0, 2.0); + final circleShape = CircleShape(position: position); + expect(circleShape.position, equals(position)); + }); + + test('Test clone', () { + const radius = 5.0; + final position = Vector2(1.0, 2.0); + final circleShape = CircleShape(radius: radius, position: position); + final clonedCircleShape = circleShape.clone() as CircleShape; + expect(clonedCircleShape.radius, equals(radius)); + expect(clonedCircleShape.position, equals(position)); + }); + }); +} diff --git a/packages/forge2d/test/dynamics/body_test.dart b/packages/forge2d/test/dynamics/body_test.dart index 1b18e8d..9f5b288 100644 --- a/packages/forge2d/test/dynamics/body_test.dart +++ b/packages/forge2d/test/dynamics/body_test.dart @@ -27,9 +27,7 @@ void main() { final world = World(gravity); final body = world.createBody( - BodyDef() - ..type = BodyType.dynamic - ..gravityOverride = Vector2.zero(), + BodyDef(type: BodyType.dynamic, gravityOverride: Vector2.zero()), ); final bodyInitialPosition = body.position.clone(); @@ -45,9 +43,7 @@ void main() { 'when gravityOverride is not specfied', () { final gravity = Vector2(10, 10); final world = World(gravity); - - final body = world.createBody(BodyDef()..type = BodyType.dynamic); - + final body = world.createBody(BodyDef(type: BodyType.dynamic)); final bodyInitialPosition = body.position.clone(); world.stepDt(1); @@ -63,9 +59,7 @@ void main() { final world = World(gravity); final body = world.createBody( - BodyDef() - ..type = BodyType.dynamic - ..gravityScale = Vector2.zero(), + BodyDef(type: BodyType.dynamic, gravityScale: Vector2.zero()), ); final bodyInitialPosition = body.position.clone(); @@ -82,8 +76,7 @@ void main() { final gravity = Vector2(10, 10); final world = World(gravity); - final body = world.createBody(BodyDef()..type = BodyType.dynamic); - + final body = world.createBody(BodyDef(type: BodyType.dynamic)); final bodyInitialPosition = body.position.clone(); world.stepDt(1); diff --git a/packages/forge2d/test/dynamics/joints/joint_test.dart b/packages/forge2d/test/dynamics/joints/joint_test.dart index 8a8bbb0..2bb7bf1 100644 --- a/packages/forge2d/test/dynamics/joints/joint_test.dart +++ b/packages/forge2d/test/dynamics/joints/joint_test.dart @@ -17,14 +17,13 @@ void main() { final bodyDef = BodyDef(); final body1 = world.createBody(bodyDef); final body2 = world.createBody(bodyDef..position = Vector2.all(2)); - final shape = CircleShape() - ..radius = 1.2 - ..position.setValues(10, 10); - - final fixtureDef = FixtureDef(shape) - ..density = 50.0 - ..friction = .1 - ..restitution = .9; + final shape = CircleShape(radius: 1.2, position: Vector2.all(10)); + final fixtureDef = FixtureDef( + shape, + density: 50.0, + friction: 0.1, + restitution: 0.9, + ); body1.createFixture(fixtureDef); body2.createFixture(fixtureDef);