From e0ec0f2ddea729940f50b20fe3b3bdc8fff920ee Mon Sep 17 00:00:00 2001 From: Filip Hracek Date: Mon, 4 Dec 2017 13:52:51 -0800 Subject: [PATCH 1/7] Turn strong mode on Fix minor strong mode error. --- analysis_options.yaml | 2 ++ lib/src/particle/particle_system.dart | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 analysis_options.yaml diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 00000000..a10d4c5a --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,2 @@ +analyzer: + strong-mode: true diff --git a/lib/src/particle/particle_system.dart b/lib/src/particle/particle_system.dart index 77d89b76..55a95fcb 100644 --- a/lib/src/particle/particle_system.dart +++ b/lib/src/particle/particle_system.dart @@ -526,7 +526,7 @@ class ParticleSystem { World world; static Vector2 allocVec2() => new Vector2.zero(); - static Vector2 allocObject() => new Object(); + static Object allocObject() => new Object(); static ParticleColor allocParticleColor() => new ParticleColor(); static ParticleGroup allocParticleGroup() => new ParticleGroup(); static PsProxy allocPsProxy() => new PsProxy(); From f66c7b93524aba55c12f325b79cf8b68a0795dd6 Mon Sep 17 00:00:00 2001 From: Filip Hracek Date: Mon, 4 Dec 2017 14:26:35 -0800 Subject: [PATCH 2/7] Disable implicit casts and and implicit dynamic Resolve errors. box2d.dart is now fully strong-mode compliant. --- analysis_options.yaml | 4 +++- benchmark/bench2d_web.dart | 2 +- example/ball_cage.dart | 8 +++---- example/circle_stress.dart | 8 ++++--- example/demo.dart | 2 +- example/domino_test.dart | 2 +- example/domino_tower.dart | 14 +++++------ example/racer.dart | 2 +- example/racer/car.dart | 4 ++-- lib/src/buffer_utils.dart | 12 +++++----- lib/src/callbacks/pair_callback.dart | 2 +- lib/src/collision/contactid.dart | 8 +++---- lib/src/collision/distance.dart | 8 +++---- lib/src/dynamics/contact_manager.dart | 2 +- .../contacts/chain_and_circle_contact.dart | 7 +++--- .../contacts/chain_and_polygon_contact.dart | 7 +++--- lib/src/dynamics/contacts/circle_contact.dart | 6 ++++- .../contacts/edge_and_circle_contact.dart | 6 ++++- .../contacts/edge_and_polygon_contact.dart | 6 ++++- .../contacts/polygon_and_circle_contact.dart | 6 ++++- .../dynamics/contacts/polygon_contact.dart | 6 ++++- .../joints/constant_volume_joints.dart | 2 +- lib/src/dynamics/joints/gear_joint.dart | 8 +++---- lib/src/dynamics/joints/joint.dart | 24 +++++++++---------- lib/src/dynamics/world.dart | 14 +++++------ lib/src/particle/particle_color.dart | 8 +++---- lib/src/particle/particle_system.dart | 24 ++++++++++--------- lib/src/pooling/normal/ordered_stack.dart | 8 +++---- 28 files changed, 117 insertions(+), 93 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index a10d4c5a..4135297c 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,2 +1,4 @@ analyzer: - strong-mode: true + strong-mode: + implicit-casts: false + implicit-dynamic: false diff --git a/benchmark/bench2d_web.dart b/benchmark/bench2d_web.dart index ba9cf5b1..e840b62e 100644 --- a/benchmark/bench2d_web.dart +++ b/benchmark/bench2d_web.dart @@ -55,7 +55,7 @@ class Bench2dWeb extends Bench2d { ..width = CANVAS_WIDTH ..height = CANVAS_HEIGHT; - ctx = canvas.getContext("2d"); + ctx = canvas.getContext("2d") as CanvasRenderingContext2D; document.body.append(canvas); // Create the viewport transform with the center at extents. diff --git a/example/ball_cage.dart b/example/ball_cage.dart index 0cd91baf..65209a07 100644 --- a/example/ball_cage.dart +++ b/example/ball_cage.dart @@ -34,10 +34,10 @@ class BallCage extends Demo { static const double START_Y = -20.0; /** The radius of the balls forming the arena. */ - static const num WALL_BALL_RADIUS = 2.0; + static const double WALL_BALL_RADIUS = 2.0; /** Radius of the active ball. */ - static const num ACTIVE_BALL_RADIUS = 1.0; + static const double ACTIVE_BALL_RADIUS = 1.0; /** Constructs a new BallCage. */ BallCage() : super("Ball cage"); @@ -65,8 +65,8 @@ class BallCage extends Demo { final circleBodyDef = new BodyDef(); int maxShapeinRow = 10; - final num borderLimitX = START_X + maxShapeinRow * 2 * circleShape.radius; - final num borderLimitY = START_Y + maxShapeinRow * 2 * circleShape.radius; + final double borderLimitX = START_X + maxShapeinRow * 2 * circleShape.radius; + final double borderLimitY = START_Y + maxShapeinRow * 2 * circleShape.radius; for (int i = 0; i < maxShapeinRow; i++) { final double shiftX = START_X + circleShape.radius * 2 * i; diff --git a/example/circle_stress.dart b/example/circle_stress.dart index df9ee1a1..f75148b9 100644 --- a/example/circle_stress.dart +++ b/example/circle_stress.dart @@ -111,13 +111,15 @@ class CircleStress extends Demo { ..type = BodyType.DYNAMIC ..position = new Vector2(0.0, 10.0); int numPieces = 5; - num radius = 6; + double radius = 6.0; var body = world.createBody(bd); bodies.add(body); for (int i = 0; i < numPieces; i++) { - num xPos = radius * Math.cos(2 * Math.PI * (i / numPieces.toDouble())); - num yPos = radius * Math.sin(2 * Math.PI * (i / numPieces.toDouble())); + double xPos = + radius * Math.cos(2 * Math.PI * (i / numPieces.toDouble())); + double yPos = + radius * Math.sin(2 * Math.PI * (i / numPieces.toDouble())); var cd = new CircleShape() ..radius = 1.2 diff --git a/example/demo.dart b/example/demo.dart index 975b361a..d49f1552 100644 --- a/example/demo.dart +++ b/example/demo.dart @@ -49,7 +49,7 @@ abstract class Demo { static const double GRAVITY = -10.0; /** The timestep and iteration numbers. */ - static const num TIME_STEP = 1 / 60; + static const double TIME_STEP = 1 / 60; static const int VELOCITY_ITERATIONS = 10; static const int POSITION_ITERATIONS = 10; diff --git a/example/domino_test.dart b/example/domino_test.dart index da848eb8..051f9f3e 100644 --- a/example/domino_test.dart +++ b/example/domino_test.dart @@ -74,7 +74,7 @@ class DominoTest extends Demo { BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; - num friction = .5; + double friction = .5; int numPerRow = 25; for (int i = 0; i < 4; ++i) { diff --git a/example/domino_tower.dart b/example/domino_tower.dart index a905d1ee..cd3bf479 100644 --- a/example/domino_tower.dart +++ b/example/domino_tower.dart @@ -30,10 +30,10 @@ import 'package:box2d/box2d.dart'; import 'demo.dart'; class DominoTower extends Demo { - static const num DOMINO_WIDTH = .2; - static const num DOMINO_FRICTION = 0.1; - static const num DOMINO_HEIGHT = 1.0; - static const num BASE_COUNT = 25; + static const double DOMINO_WIDTH = .2; + static const double DOMINO_FRICTION = 0.1; + static const double DOMINO_HEIGHT = 1.0; + static const int BASE_COUNT = 25; /** * The density of the dominos under construction. Varies for different parts @@ -52,7 +52,7 @@ class DominoTower extends Demo { tower.runAnimation(); } - void makeDomino(num x, num y, bool horizontal) { + void makeDomino(double x, double y, bool horizontal) { PolygonShape sd = new PolygonShape(); sd.setAsBoxXY(.5 * DOMINO_WIDTH, .5 * DOMINO_HEIGHT); FixtureDef fd = new FixtureDef(); @@ -115,7 +115,7 @@ class DominoTower extends Demo { } { - num currX; + double currX; // Make base for (int i = 0; i < BASE_COUNT; ++i) { currX = @@ -131,7 +131,7 @@ class DominoTower extends Demo { if (j > 3) dominoDensity *= .8; // The y at the center of the I structure. - num currY = + double currY = DOMINO_HEIGHT * .5 + (DOMINO_HEIGHT + 2 * DOMINO_WIDTH) * .99 * j; for (int i = 0; i < BASE_COUNT - j; ++i) { diff --git a/example/racer.dart b/example/racer.dart index 72711b25..fe0c0c32 100644 --- a/example/racer.dart +++ b/example/racer.dart @@ -198,6 +198,6 @@ class Racer extends Demo implements ContactListener { num _lastTime = 0; } -main() { +void main() { Racer.main(); } diff --git a/example/racer/car.dart b/example/racer/car.dart index 0bb519f4..19e0f9d3 100644 --- a/example/racer/car.dart +++ b/example/racer/car.dart @@ -60,13 +60,13 @@ class Car { _frontTireMaxDriveForce, _frontTireMaxLateralImpulse); jointDef.bodyB = _flTire._body; jointDef.localAnchorA.setValues(-3.0, 8.5); - _flJoint = world.createJoint(jointDef); + _flJoint = world.createJoint(jointDef) as RevoluteJoint; _frTire = new Tire(world, _maxForwardSpeed, _maxBackwardSpeed, _frontTireMaxDriveForce, _frontTireMaxLateralImpulse); jointDef.bodyB = _frTire._body; jointDef.localAnchorA.setValues(3.0, 8.5); - _frJoint = world.createJoint(jointDef); + _frJoint = world.createJoint(jointDef) as RevoluteJoint; } void _updateFriction() { diff --git a/lib/src/buffer_utils.dart b/lib/src/buffer_utils.dart index 81a1274d..bec23deb 100644 --- a/lib/src/buffer_utils.dart +++ b/lib/src/buffer_utils.dart @@ -27,10 +27,10 @@ library box2d.buffer_utils; import 'dart:typed_data'; /** Reallocate a buffer. */ -List reallocateBufferWithAlloc( - List oldBuffer, int oldCapacity, int newCapacity, alloc) { +List reallocateBufferWithAlloc( + List oldBuffer, int oldCapacity, int newCapacity, T alloc()) { assert(newCapacity > oldCapacity); - List newBuffer = new List(newCapacity); + List newBuffer = new List(newCapacity); if (oldBuffer != null) { arraycopy(oldBuffer, 0, newBuffer, 0, oldCapacity); } @@ -73,8 +73,8 @@ Float64List reallocateBuffer( * Reallocate a buffer. A 'deferred' buffer is reallocated only if it is not NULL. If * 'userSuppliedCapacity' is not zero, buffer is user supplied and must be kept. */ -List reallocateBufferWithAllocDeferred(List buffer, int userSuppliedCapacity, - int oldCapacity, int newCapacity, bool deferred, alloc) { +List reallocateBufferWithAllocDeferred(List buffer, int userSuppliedCapacity, + int oldCapacity, int newCapacity, bool deferred, T alloc()) { assert(newCapacity > oldCapacity); assert(userSuppliedCapacity == 0 || newCapacity <= userSuppliedCapacity); if ((!deferred || buffer != null) && userSuppliedCapacity == 0) { @@ -112,7 +112,7 @@ Float64List reallocateBufferFloat64Deferred(Float64List buffer, } /** Rotate an array, see std::rotate */ -void rotate(List ray, int first, int new_first, int last) { +void rotate(List ray, int first, int new_first, int last) { int next = new_first; while (next != first) { var temp = ray[first]; diff --git a/lib/src/callbacks/pair_callback.dart b/lib/src/callbacks/pair_callback.dart index bdddf8f4..cc685426 100644 --- a/lib/src/callbacks/pair_callback.dart +++ b/lib/src/callbacks/pair_callback.dart @@ -25,5 +25,5 @@ part of box2d; abstract class PairCallback { - void addPair(Object userDataA, Object userDataB); + void addPair(covariant Object userDataA, covariant Object userDataB); } diff --git a/lib/src/collision/contactid.dart b/lib/src/collision/contactid.dart index 313840b8..4b3d5eb0 100644 --- a/lib/src/collision/contactid.dart +++ b/lib/src/collision/contactid.dart @@ -29,19 +29,19 @@ enum ContactIDType { VERTEX, FACE } class ContactID implements Comparable { Int8List _data = new Int8List(4); - void set indexA(v) { + void set indexA(int v) { _data[0] = v; } - void set indexB(v) { + void set indexB(int v) { _data[1] = v; } - void set typeA(v) { + void set typeA(int v) { _data[2] = v; } - void set typeB(v) { + void set typeB(int v) { _data[3] = v; ; } diff --git a/lib/src/collision/distance.dart b/lib/src/collision/distance.dart index 13f8a5f1..5a6d3d10 100644 --- a/lib/src/collision/distance.dart +++ b/lib/src/collision/distance.dart @@ -509,14 +509,14 @@ class DistanceProxy { void set(final Shape shape, int index) { switch (shape.shapeType) { case ShapeType.CIRCLE: - final CircleShape circle = shape; + final circle = shape as CircleShape; vertices[0].setFrom(circle.p); _count = 1; radius = circle.radius; break; case ShapeType.POLYGON: - final PolygonShape poly = shape; + final poly = shape as PolygonShape; _count = poly.count; radius = poly.radius; for (int i = 0; i < _count; i++) { @@ -524,7 +524,7 @@ class DistanceProxy { } break; case ShapeType.CHAIN: - final ChainShape chain = shape; + final chain = shape as ChainShape; assert(0 <= index && index < chain._count); buffer[0] = chain._vertices[index]; @@ -540,7 +540,7 @@ class DistanceProxy { radius = chain.radius; break; case ShapeType.EDGE: - EdgeShape edge = shape; + final edge = shape as EdgeShape; vertices[0].setFrom(edge.vertex1); vertices[1].setFrom(edge.vertex2); _count = 2; diff --git a/lib/src/dynamics/contact_manager.dart b/lib/src/dynamics/contact_manager.dart index fd422672..db82737a 100644 --- a/lib/src/dynamics/contact_manager.dart +++ b/lib/src/dynamics/contact_manager.dart @@ -52,7 +52,7 @@ class ContactManager implements PairCallback { * @param proxyUserDataA * @param proxyUserDataB */ - void addPair(Object proxyUserDataA, Object proxyUserDataB) { + void addPair(FixtureProxy proxyUserDataA, FixtureProxy proxyUserDataB) { FixtureProxy proxyA = proxyUserDataA; FixtureProxy proxyB = proxyUserDataB; diff --git a/lib/src/dynamics/contacts/chain_and_circle_contact.dart b/lib/src/dynamics/contacts/chain_and_circle_contact.dart index 84d2c497..c7056e4c 100644 --- a/lib/src/dynamics/contacts/chain_and_circle_contact.dart +++ b/lib/src/dynamics/contacts/chain_and_circle_contact.dart @@ -36,10 +36,9 @@ class ChainAndCircleContact extends Contact { final EdgeShape _edge = new EdgeShape(); void evaluate(Manifold manifold, Transform xfA, Transform xfB) { - ChainShape chain = _fixtureA.getShape(); + final chain = _fixtureA.getShape() as ChainShape; chain.getChildEdge(_edge, _indexA); - _pool - .getCollision() - .collideEdgeAndCircle(manifold, _edge, xfA, _fixtureB.getShape(), xfB); + _pool.getCollision().collideEdgeAndCircle( + manifold, _edge, xfA, _fixtureB.getShape() as CircleShape, xfB); } } diff --git a/lib/src/dynamics/contacts/chain_and_polygon_contact.dart b/lib/src/dynamics/contacts/chain_and_polygon_contact.dart index 2d95ceea..481752dc 100644 --- a/lib/src/dynamics/contacts/chain_and_polygon_contact.dart +++ b/lib/src/dynamics/contacts/chain_and_polygon_contact.dart @@ -36,10 +36,9 @@ class ChainAndPolygonContact extends Contact { final EdgeShape _edge = new EdgeShape(); void evaluate(Manifold manifold, Transform xfA, Transform xfB) { - ChainShape chain = _fixtureA.getShape(); + final chain = _fixtureA.getShape() as ChainShape; chain.getChildEdge(_edge, _indexA); - _pool - .getCollision() - .collideEdgeAndPolygon(manifold, _edge, xfA, _fixtureB.getShape(), xfB); + _pool.getCollision().collideEdgeAndPolygon( + manifold, _edge, xfA, _fixtureB.getShape() as PolygonShape, xfB); } } diff --git a/lib/src/dynamics/contacts/circle_contact.dart b/lib/src/dynamics/contacts/circle_contact.dart index 4df4e1c2..07ed2742 100644 --- a/lib/src/dynamics/contacts/circle_contact.dart +++ b/lib/src/dynamics/contacts/circle_contact.dart @@ -35,6 +35,10 @@ class CircleContact extends Contact { void evaluate(Manifold manifold, Transform xfA, Transform xfB) { _pool.getCollision().collideCircles( - manifold, _fixtureA.getShape(), xfA, _fixtureB.getShape(), xfB); + manifold, + _fixtureA.getShape() as CircleShape, + xfA, + _fixtureB.getShape() as CircleShape, + xfB); } } diff --git a/lib/src/dynamics/contacts/edge_and_circle_contact.dart b/lib/src/dynamics/contacts/edge_and_circle_contact.dart index 3de6f75b..0489c4bd 100644 --- a/lib/src/dynamics/contacts/edge_and_circle_contact.dart +++ b/lib/src/dynamics/contacts/edge_and_circle_contact.dart @@ -35,6 +35,10 @@ class EdgeAndCircleContact extends Contact { void evaluate(Manifold manifold, Transform xfA, Transform xfB) { _pool.getCollision().collideEdgeAndCircle( - manifold, _fixtureA.getShape(), xfA, _fixtureB.getShape(), xfB); + manifold, + _fixtureA.getShape() as EdgeShape, + xfA, + _fixtureB.getShape() as CircleShape, + xfB); } } diff --git a/lib/src/dynamics/contacts/edge_and_polygon_contact.dart b/lib/src/dynamics/contacts/edge_and_polygon_contact.dart index 136306a0..6a1a4ed9 100644 --- a/lib/src/dynamics/contacts/edge_and_polygon_contact.dart +++ b/lib/src/dynamics/contacts/edge_and_polygon_contact.dart @@ -35,6 +35,10 @@ class EdgeAndPolygonContact extends Contact { void evaluate(Manifold manifold, Transform xfA, Transform xfB) { _pool.getCollision().collideEdgeAndPolygon( - manifold, _fixtureA.getShape(), xfA, _fixtureB.getShape(), xfB); + manifold, + _fixtureA.getShape() as EdgeShape, + xfA, + _fixtureB.getShape() as PolygonShape, + xfB); } } diff --git a/lib/src/dynamics/contacts/polygon_and_circle_contact.dart b/lib/src/dynamics/contacts/polygon_and_circle_contact.dart index 22c67934..c16dba66 100644 --- a/lib/src/dynamics/contacts/polygon_and_circle_contact.dart +++ b/lib/src/dynamics/contacts/polygon_and_circle_contact.dart @@ -35,6 +35,10 @@ class PolygonAndCircleContact extends Contact { void evaluate(Manifold manifold, Transform xfA, Transform xfB) { _pool.getCollision().collidePolygonAndCircle( - manifold, _fixtureA.getShape(), xfA, _fixtureB.getShape(), xfB); + manifold, + _fixtureA.getShape() as PolygonShape, + xfA, + _fixtureB.getShape() as CircleShape, + xfB); } } diff --git a/lib/src/dynamics/contacts/polygon_contact.dart b/lib/src/dynamics/contacts/polygon_contact.dart index 6f7b7fe9..2ed23813 100644 --- a/lib/src/dynamics/contacts/polygon_contact.dart +++ b/lib/src/dynamics/contacts/polygon_contact.dart @@ -37,6 +37,10 @@ class PolygonContact extends Contact { void evaluate(Manifold manifold, Transform xfA, Transform xfB) { _pool.getCollision().collidePolygons( - manifold, _fixtureA.getShape(), xfA, _fixtureB.getShape(), xfB); + manifold, + _fixtureA.getShape() as PolygonShape, + xfA, + _fixtureB.getShape() as PolygonShape, + xfB); } } diff --git a/lib/src/dynamics/joints/constant_volume_joints.dart b/lib/src/dynamics/joints/constant_volume_joints.dart index b71af674..6a33e09a 100644 --- a/lib/src/dynamics/joints/constant_volume_joints.dart +++ b/lib/src/dynamics/joints/constant_volume_joints.dart @@ -77,7 +77,7 @@ class ConstantVolumeJoint extends Joint { djd.collideConnected = def.collideConnected; djd.initialize(_bodies[i], _bodies[next], _bodies[i].worldCenter, _bodies[next].worldCenter); - _distanceJoints[i] = _world.createJoint(djd); + _distanceJoints[i] = _world.createJoint(djd) as DistanceJoint; } } else { _distanceJoints = def.joints.toList(); diff --git a/lib/src/dynamics/joints/gear_joint.dart b/lib/src/dynamics/joints/gear_joint.dart index 425b60dc..521b621f 100644 --- a/lib/src/dynamics/joints/gear_joint.dart +++ b/lib/src/dynamics/joints/gear_joint.dart @@ -119,7 +119,7 @@ class GearJoint extends Joint { double aC = _bodyC._sweep.a; if (_typeA == JointType.REVOLUTE) { - RevoluteJoint revolute = def.joint1; + final revolute = def.joint1 as RevoluteJoint; _localAnchorC.setFrom(revolute._localAnchorA); _localAnchorA.setFrom(revolute._localAnchorB); _referenceAngleA = revolute._referenceAngle; @@ -129,7 +129,7 @@ class GearJoint extends Joint { } else { Vector2 pA = pool.popVec2(); Vector2 temp = pool.popVec2(); - PrismaticJoint prismatic = def.joint1; + final prismatic = def.joint1 as PrismaticJoint; _localAnchorC.setFrom(prismatic._localAnchorA); _localAnchorA.setFrom(prismatic._localAnchorB); _referenceAngleA = prismatic._referenceAngle; @@ -154,7 +154,7 @@ class GearJoint extends Joint { double aD = _bodyD._sweep.a; if (_typeB == JointType.REVOLUTE) { - RevoluteJoint revolute = def.joint2; + final revolute = def.joint2 as RevoluteJoint; _localAnchorD.setFrom(revolute._localAnchorA); _localAnchorB.setFrom(revolute._localAnchorB); _referenceAngleB = revolute._referenceAngle; @@ -164,7 +164,7 @@ class GearJoint extends Joint { } else { Vector2 pB = pool.popVec2(); Vector2 temp = pool.popVec2(); - PrismaticJoint prismatic = def.joint2; + final prismatic = def.joint2 as PrismaticJoint; _localAnchorD.setFrom(prismatic._localAnchorA); _localAnchorB.setFrom(prismatic._localAnchorB); _referenceAngleB = prismatic._referenceAngle; diff --git a/lib/src/dynamics/joints/joint.dart b/lib/src/dynamics/joints/joint.dart index 08d81f54..3bd623dc 100644 --- a/lib/src/dynamics/joints/joint.dart +++ b/lib/src/dynamics/joints/joint.dart @@ -35,29 +35,29 @@ abstract class Joint { // Joint joint = null; switch (def.type) { case JointType.MOUSE: - return new MouseJoint(world.getPool(), def); + return new MouseJoint(world.getPool(), def as MouseJointDef); case JointType.DISTANCE: - return new DistanceJoint(world.getPool(), def); + return new DistanceJoint(world.getPool(), def as DistanceJointDef); case JointType.PRISMATIC: - return new PrismaticJoint(world.getPool(), def); + return new PrismaticJoint(world.getPool(), def as PrismaticJointDef); case JointType.REVOLUTE: - return new RevoluteJoint(world.getPool(), def); + return new RevoluteJoint(world.getPool(), def as RevoluteJointDef); case JointType.WELD: - return new WeldJoint(world.getPool(), def); + return new WeldJoint(world.getPool(), def as WeldJointDef); case JointType.FRICTION: - return new FrictionJoint(world.getPool(), def); + return new FrictionJoint(world.getPool(), def as FrictionJointDef); case JointType.WHEEL: - return new WheelJoint(world.getPool(), def); + return new WheelJoint(world.getPool(), def as WheelJointDef); case JointType.GEAR: - return new GearJoint(world.getPool(), def); + return new GearJoint(world.getPool(), def as GearJointDef); case JointType.PULLEY: - return new PulleyJoint(world.getPool(), def); + return new PulleyJoint(world.getPool(), def as PulleyJointDef); case JointType.CONSTANT_VOLUME: - return new ConstantVolumeJoint(world, def); + return new ConstantVolumeJoint(world, def as ConstantVolumeJointDef); case JointType.ROPE: - return new RopeJoint(world.getPool(), def); + return new RopeJoint(world.getPool(), def as RopeJointDef); case JointType.MOTOR: - return new MotorJoint(world.getPool(), def); + return new MotorJoint(world.getPool(), def as MotorJointDef); case JointType.UNKNOWN: default: return null; diff --git a/lib/src/dynamics/world.dart b/lib/src/dynamics/world.dart index bdc71215..644808d9 100644 --- a/lib/src/dynamics/world.dart +++ b/lib/src/dynamics/world.dart @@ -1417,7 +1417,7 @@ class World { case JointType.PULLEY: { - PulleyJoint pulley = joint; + final pulley = joint as PulleyJoint; Vector2 s1 = pulley.getGroundAnchorA(); Vector2 s2 = pulley.getGroundAnchorB(); debugDraw.drawSegment(s1, p1, color); @@ -1461,7 +1461,7 @@ class World { switch (fixture.getType()) { case ShapeType.CIRCLE: { - CircleShape circle = fixture.getShape(); + final circle = fixture.getShape() as CircleShape; // Vec2 center = Mul(xf, circle.m_p); Transform.mulToOutUnsafeVec2(xf, circle.p, center); @@ -1495,7 +1495,7 @@ class World { case ShapeType.POLYGON: { - PolygonShape poly = fixture.getShape(); + final poly = fixture.getShape() as PolygonShape; int vertexCount = poly.count; assert(vertexCount <= Settings.maxPolygonVertices); List vertices = tlvertices.get(Settings.maxPolygonVertices); @@ -1513,7 +1513,7 @@ class World { break; case ShapeType.EDGE: { - EdgeShape edge = fixture.getShape(); + final edge = fixture.getShape() as EdgeShape; Transform.mulToOutUnsafeVec2(xf, edge.vertex1, v1); Transform.mulToOutUnsafeVec2(xf, edge.vertex2, v2); debugDraw.drawSegment(v1, v2, color); @@ -1521,7 +1521,7 @@ class World { break; case ShapeType.CHAIN: { - ChainShape chain = fixture.getShape(); + final chain = fixture.getShape() as ChainShape; int count = chain._count; List vertices = chain._vertices; @@ -1911,7 +1911,7 @@ class World { class WorldQueryWrapper implements TreeCallback { bool treeCallback(int nodeId) { - FixtureProxy proxy = broadPhase.getUserData(nodeId); + final proxy = broadPhase.getUserData(nodeId) as FixtureProxy; return callback.reportFixture(proxy.fixture); } @@ -1926,7 +1926,7 @@ class WorldRayCastWrapper implements TreeRayCastCallback { final Vector2 _point = new Vector2.zero(); double raycastCallback(RayCastInput input, int nodeId) { - Object userData = broadPhase.getUserData(nodeId); + final userData = broadPhase.getUserData(nodeId) as FixtureProxy; FixtureProxy proxy = userData; Fixture fixture = proxy.fixture; int index = proxy.childIndex; diff --git a/lib/src/particle/particle_color.dart b/lib/src/particle/particle_color.dart index 41cd28fe..a8ca488c 100644 --- a/lib/src/particle/particle_color.dart +++ b/lib/src/particle/particle_color.dart @@ -48,10 +48,10 @@ class ParticleColor { _data[3] = v; } - get r => _data[0]; - get g => _data[1]; - get b => _data[2]; - get a => _data[3]; + int get r => _data[0]; + int get g => _data[1]; + int get b => _data[2]; + int get a => _data[3]; ParticleColor() { _data[0] = 127; diff --git a/lib/src/particle/particle_system.dart b/lib/src/particle/particle_system.dart index 55a95fcb..adbb608c 100644 --- a/lib/src/particle/particle_system.dart +++ b/lib/src/particle/particle_system.dart @@ -26,11 +26,13 @@ part of box2d; class ParticleBuffer { List data; - final allocClosure; + final AllocClosure allocClosure; int userSuppliedCapacity = 0; ParticleBuffer(this.allocClosure); } +typedef T AllocClosure(); + class ParticleBufferInt { List data; int userSuppliedCapacity; @@ -69,7 +71,7 @@ class PsProxy implements Comparable { if (this == obj) return true; if (obj == null) return false; if (obj is! PsProxy) return false; - PsProxy other = obj; + final other = obj as PsProxy; if (tag != other.tag) return false; return true; } @@ -138,8 +140,8 @@ class UpdateBodyContactsCallback implements QueryCallback { Vector2 bp = b.worldCenter; double bm = b.mass; double bI = b.getInertia() - bm * b.getLocalCenter().length2; - double invBm = bm > 0 ? 1 / bm : 0; - double invBI = bI > 0 ? 1 / bI : 0; + double invBm = bm > 0 ? 1 / bm : 0.0; + double invBI = bI > 0 ? 1 / bI : 0.0; int childCount = shape.getChildCount(); for (int childIndex = 0; childIndex < childCount; childIndex++) { AABB aabb = fixture.getAABB(childIndex); @@ -175,7 +177,7 @@ class UpdateBodyContactsCallback implements QueryCallback { if (d < system.particleDiameter) { double invAm = (system.flagsBuffer.data[a] & ParticleType.b2_wallParticle) != 0 - ? 0 + ? 0.0 : system.getParticleInvMass(); final double rpx = ap.x - bp.x; final double rpy = ap.y - bp.y; @@ -624,10 +626,10 @@ class ParticleSystem { } // reallocate a buffer - static List reallocateBuffer( - ParticleBuffer buffer, int oldCapacity, int newCapacity, bool deferred) { + static List reallocateBuffer( + ParticleBuffer buffer, int oldCapacity, int newCapacity, bool deferred) { assert(newCapacity > oldCapacity); - return BufferUtils.reallocateBufferWithAllocDeferred( + return BufferUtils.reallocateBufferWithAllocDeferred( buffer.data, buffer.userSuppliedCapacity, oldCapacity, @@ -643,9 +645,9 @@ class ParticleSystem { buffer.userSuppliedCapacity, oldCapacity, newCapacity, deferred); } - List requestParticleBuffer(List buffer, allocClosure) { + List requestParticleBuffer(List buffer, T allocClosure()) { if (buffer == null) { - buffer = new List(internalAllocatedCapacity); + buffer = new List(internalAllocatedCapacity); for (int i = 0; i < internalAllocatedCapacity; i++) { try { buffer[i] = allocClosure(); @@ -945,7 +947,7 @@ class ParticleSystem { depthBuffer = requestParticleBufferFloat64(depthBuffer); for (int i = group._firstIndex; i < group._lastIndex; i++) { double w = accumulationBuffer[i]; - depthBuffer[i] = w < 0.8 ? 0 : double.MAX_FINITE; + depthBuffer[i] = w < 0.8 ? 0.0 : double.MAX_FINITE; } int interationCount = group.getParticleCount(); for (int t = 0; t < interationCount; t++) { diff --git a/lib/src/pooling/normal/ordered_stack.dart b/lib/src/pooling/normal/ordered_stack.dart index 7035d6f8..ec5d84cc 100644 --- a/lib/src/pooling/normal/ordered_stack.dart +++ b/lib/src/pooling/normal/ordered_stack.dart @@ -25,16 +25,16 @@ part of box2d; abstract class OrderedStack { - final List _pool; + final List _pool; int _index; final int _size; - final List _container; + final List _container; OrderedStack(int argStackSize, int argContainerSize) - : _pool = new List(argStackSize), + : _pool = new List(argStackSize), _index = 0, _size = argStackSize, - _container = new List(argContainerSize) { + _container = new List(argContainerSize) { // pool = new List(argStackSize); for (int i = 0; i < argStackSize; i++) { _pool[i] = newInstance(); From da42c737d859546bf20cdf105bee8accb7a05eac Mon Sep 17 00:00:00 2001 From: Filip Hracek Date: Mon, 4 Dec 2017 16:28:00 -0800 Subject: [PATCH 3/7] Apply dartfmt --- example/ball_cage.dart | 6 ++++-- lib/src/buffer_utils.dart | 9 +++++++-- lib/src/particle/particle_system.dart | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/example/ball_cage.dart b/example/ball_cage.dart index 65209a07..e8107f20 100644 --- a/example/ball_cage.dart +++ b/example/ball_cage.dart @@ -65,8 +65,10 @@ class BallCage extends Demo { final circleBodyDef = new BodyDef(); int maxShapeinRow = 10; - final double borderLimitX = START_X + maxShapeinRow * 2 * circleShape.radius; - final double borderLimitY = START_Y + maxShapeinRow * 2 * circleShape.radius; + final double borderLimitX = + START_X + maxShapeinRow * 2 * circleShape.radius; + final double borderLimitY = + START_Y + maxShapeinRow * 2 * circleShape.radius; for (int i = 0; i < maxShapeinRow; i++) { final double shiftX = START_X + circleShape.radius * 2 * i; diff --git a/lib/src/buffer_utils.dart b/lib/src/buffer_utils.dart index bec23deb..6784a1bb 100644 --- a/lib/src/buffer_utils.dart +++ b/lib/src/buffer_utils.dart @@ -73,8 +73,13 @@ Float64List reallocateBuffer( * Reallocate a buffer. A 'deferred' buffer is reallocated only if it is not NULL. If * 'userSuppliedCapacity' is not zero, buffer is user supplied and must be kept. */ -List reallocateBufferWithAllocDeferred(List buffer, int userSuppliedCapacity, - int oldCapacity, int newCapacity, bool deferred, T alloc()) { +List reallocateBufferWithAllocDeferred( + List buffer, + int userSuppliedCapacity, + int oldCapacity, + int newCapacity, + bool deferred, + T alloc()) { assert(newCapacity > oldCapacity); assert(userSuppliedCapacity == 0 || newCapacity <= userSuppliedCapacity); if ((!deferred || buffer != null) && userSuppliedCapacity == 0) { diff --git a/lib/src/particle/particle_system.dart b/lib/src/particle/particle_system.dart index adbb608c..cfe6dd05 100644 --- a/lib/src/particle/particle_system.dart +++ b/lib/src/particle/particle_system.dart @@ -626,8 +626,8 @@ class ParticleSystem { } // reallocate a buffer - static List reallocateBuffer( - ParticleBuffer buffer, int oldCapacity, int newCapacity, bool deferred) { + static List reallocateBuffer(ParticleBuffer buffer, int oldCapacity, + int newCapacity, bool deferred) { assert(newCapacity > oldCapacity); return BufferUtils.reallocateBufferWithAllocDeferred( buffer.data, From 307cd654e141516b103277991b0dab73074c5f98 Mon Sep 17 00:00:00 2001 From: Filip Hracek Date: Mon, 4 Dec 2017 22:54:05 -0800 Subject: [PATCH 4/7] Make velocityThreshold mutable See https://github.com/jbox2d/jbox2d/pull/1 --- lib/src/settings.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/settings.dart b/lib/src/settings.dart index 78051fd2..3678d487 100644 --- a/lib/src/settings.dart +++ b/lib/src/settings.dart @@ -88,7 +88,7 @@ const int maxTOIContacts = 32; * A velocity threshold for elastic collisions. Any collision with a relative linear velocity * below this threshold will be treated as inelastic. */ -const double velocityThreshold = 1.0; +double velocityThreshold = 1.0; /** * The maximum linear position correction used when solving constraints. This helps to prevent From 586f627148c4213107705f293ded22d32498660f Mon Sep 17 00:00:00 2001 From: Filip Hracek Date: Mon, 4 Dec 2017 23:04:08 -0800 Subject: [PATCH 5/7] TimeOfImpact loop fixes See https://github.com/jbox2d/jbox2d/pull/36 --- lib/src/collision/time_of_impact.dart | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/lib/src/collision/time_of_impact.dart b/lib/src/collision/time_of_impact.dart index 8a5a6d89..8e6d466a 100644 --- a/lib/src/collision/time_of_impact.dart +++ b/lib/src/collision/time_of_impact.dart @@ -59,7 +59,8 @@ class TOIOutput { * @author daniel */ class TimeOfImpact { - static const int MAX_ITERATIONS = 1000; + static const int MAX_ITERATIONS = 20; + static const int MAX_ROOT_ITERATIONS = 50; static int toiCalls = 0; static int toiIters = 0; @@ -227,6 +228,9 @@ class TimeOfImpact { t = 0.5 * (a1 + a2); } + ++rootIterCount; + ++toiRootIters; + double s = _fcn.evaluate(_indexes[0], _indexes[1], t); if ((s - target).abs() < tolerance) { @@ -244,11 +248,7 @@ class TimeOfImpact { s2 = s; } - ++rootIterCount; - ++toiRootIters; - - // djm: whats with this? put in settings? - if (rootIterCount == 50) { + if (rootIterCount == MAX_ROOT_ITERATIONS) { break; } } @@ -257,7 +257,8 @@ class TimeOfImpact { ++pushBackIter; - if (pushBackIter == Settings.maxPolygonVertices) { + if (pushBackIter == Settings.maxPolygonVertices || + rootIterCount == MAX_ROOT_ITERATIONS) { break; } } @@ -490,10 +491,6 @@ class SeparationFunction { switch (type) { case SeparationFunctionType.POINTS: - Rot.mulTransUnsafeVec2(_xfa.q, axis, _axisA); - Rot.mulTransUnsafeVec2(_xfb.q, axis..negate(), _axisB); - axis.negate(); - _localPointA.setFrom(proxyA.getVertex(indexA)); _localPointB.setFrom(proxyB.getVertex(indexB)); @@ -507,9 +504,6 @@ class SeparationFunction { Rot.mulToOutUnsafe(_xfa.q, axis, _normal); Transform.mulToOutUnsafeVec2(_xfa, localPoint, _pointA); - Rot.mulTransUnsafeVec2(_xfb.q, _normal..negate(), _axisB); - _normal.negate(); - _localPointB.setFrom(proxyB.getVertex(indexB)); Transform.mulToOutUnsafeVec2(_xfb, _localPointB, _pointB); double separation = (_pointB..sub(_pointA)).dot(_normal); @@ -519,9 +513,6 @@ class SeparationFunction { Rot.mulToOutUnsafe(_xfb.q, axis, _normal); Transform.mulToOutUnsafeVec2(_xfb, localPoint, _pointB); - Rot.mulTransUnsafeVec2(_xfa.q, _normal..negate(), _axisA); - _normal.negate(); - _localPointA.setFrom(proxyA.getVertex(indexA)); Transform.mulToOutUnsafeVec2(_xfa, _localPointA, _pointA); From 64ec590147f25671941e92c27547fef229441ffc Mon Sep 17 00:00:00 2001 From: Filip Hracek Date: Mon, 4 Dec 2017 23:05:53 -0800 Subject: [PATCH 6/7] fix typo --- lib/src/common/sweep.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/common/sweep.dart b/lib/src/common/sweep.dart index 44a0b574..c1e649d1 100644 --- a/lib/src/common/sweep.dart +++ b/lib/src/common/sweep.dart @@ -26,7 +26,7 @@ part of box2d.common; /** * This describes the motion of a body/shape for TOI computation. Shapes are defined with respect to - * the body origin, which may no coincide with the center of mass. However, to support dynamics we + * the body origin, which may not coincide with the center of mass. However, to support dynamics we * must interpolate the center of mass position. */ class Sweep { From fcfd5b5b9cedc825031643b2869db3e113be34ad Mon Sep 17 00:00:00 2001 From: Filip Hracek Date: Mon, 4 Dec 2017 23:09:17 -0800 Subject: [PATCH 7/7] fix ropejoint pool leak bug See https://github.com/jbox2d/jbox2d/pull/59 --- lib/src/dynamics/joints/rope_joint.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/src/dynamics/joints/rope_joint.dart b/lib/src/dynamics/joints/rope_joint.dart index 46c47003..adb422b7 100644 --- a/lib/src/dynamics/joints/rope_joint.dart +++ b/lib/src/dynamics/joints/rope_joint.dart @@ -125,6 +125,8 @@ class RopeJoint extends Joint { _u.setZero(); _mass = 0.0; _impulse = 0.0; + pool.pushRot(2); + pool.pushVec2(1); return; }