diff --git a/examples/game/test_physics.dart b/examples/game/test_physics.dart index a3915a40a8..327b054594 100644 --- a/examples/game/test_physics.dart +++ b/examples/game/test_physics.dart @@ -78,29 +78,33 @@ class TestBed extends NodeWithSize { Sprite shipA; shipA = new Sprite(_spriteSheet["ship.png"]); + shipA.opacity = 0.3; shipA.position = new Point(pos.x - 40.0, pos.y); shipA.size = new Size(64.0, 64.0); shipA.physicsBody = new PhysicsBody(new PhysicsShapeCircle(Point.origin, 32.0), friction: 0.5, + restitution: 0.5, tag: "ship" ); _physicsNode.addChild(shipA); shipA.physicsBody.applyLinearImpulse( - new Offset(randomSignedDouble() * 5.0, randomSignedDouble() * 5.0), + new Offset(randomSignedDouble() * 5000.0, randomSignedDouble() * 5000.0), shipA.position ); Sprite shipB; shipB = new Sprite(_spriteSheet["ship.png"]); + shipB.opacity = 0.3; shipB.position = new Point(pos.x + 40.0, pos.y); shipB.size = new Size(64.0, 64.0); shipB.physicsBody = new PhysicsBody(new PhysicsShapePolygon([new Point(-25.0, -25.0), new Point(25.0, -25.0), new Point(25.0, 25.0), new Point(-25.0, 25.0)]), friction: 0.5, + restitution: 0.5, tag: "ship" ); _physicsNode.addChild(shipB); - new PhysicsJointWeld(shipA.physicsBody, shipB.physicsBody); + new PhysicsJointRevolute(shipA.physicsBody, shipB.physicsBody, pos); } return true; } diff --git a/packages/flutter_sprites/lib/physics_joint.dart b/packages/flutter_sprites/lib/physics_joint.dart index cecdd5c581..7eaa5cec30 100644 --- a/packages/flutter_sprites/lib/physics_joint.dart +++ b/packages/flutter_sprites/lib/physics_joint.dart @@ -4,10 +4,6 @@ abstract class PhysicsJoint { PhysicsJoint(this.bodyA, this.bodyB) { bodyA._joints.add(this); bodyB._joints.add(this); - - if (bodyA._attached && bodyB._attached) { - _attach(bodyA._physicsNode); - } } PhysicsBody bodyA; @@ -18,6 +14,12 @@ abstract class PhysicsJoint { PhysicsNode _physicsNode; + void _completeCreation() { + if (bodyA._attached && bodyB._attached) { + _attach(bodyA._physicsNode); + } + } + void _attach(PhysicsNode physicsNode) { if (_joint == null) { _physicsNode = physicsNode; @@ -40,7 +42,7 @@ class PhysicsJointRevolute extends PhysicsJoint { PhysicsJointRevolute( PhysicsBody bodyA, PhysicsBody bodyB, - this.anchorWorld, { + this.worldAnchor, { double lowerAngle: 0.0, double upperAngle: 0.0, bool enableLimit: false @@ -48,9 +50,11 @@ class PhysicsJointRevolute extends PhysicsJoint { this.lowerAngle = lowerAngle; this.upperAngle = upperAngle; this.enableLimit = enableLimit; + + _completeCreation(); } - Point anchorWorld; + Point worldAnchor; double lowerAngle; double upperAngle; bool enableLimit; @@ -58,8 +62,8 @@ class PhysicsJointRevolute extends PhysicsJoint { box2d.Joint _createB2Joint(PhysicsNode physicsNode) { // Create Joint Definition Vector2 vecAnchor = new Vector2( - anchorWorld.x / physicsNode.b2WorldToNodeConversionFactor, - anchorWorld.y / physicsNode.b2WorldToNodeConversionFactor + worldAnchor.x / physicsNode.b2WorldToNodeConversionFactor, + worldAnchor.y / physicsNode.b2WorldToNodeConversionFactor ); box2d.RevoluteJointDef b2Def = new box2d.RevoluteJointDef(); @@ -73,18 +77,37 @@ class PhysicsJointRevolute extends PhysicsJoint { } } +class PhysicsJointPrismatic extends PhysicsJoint { + PhysicsJointPrismatic( + PhysicsBody bodyA, + PhysicsBody bodyB, + this.axis) : super(bodyA, bodyB) { + _completeCreation(); + } + + Offset axis; + + box2d.Joint _createB2Joint(PhysicsNode physicsNode) { + box2d.PrismaticJointDef b2Def = new box2d.PrismaticJointDef(); + b2Def.initialize(bodyA._body, bodyB._body, bodyA._body.position, new Vector2(axis.dx, axis.dy)); + return physicsNode.b2World.createJoint(b2Def); + } +} + class PhysicsJointWeld extends PhysicsJoint { PhysicsJointWeld( PhysicsBody bodyA, - PhysicsBody bodyB) : super(bodyA, bodyB); + PhysicsBody bodyB) : super(bodyA, bodyB) { + _completeCreation(); + } - box2d.Joint _createB2Joint(PhysicsNode physicsNode) { - box2d.WeldJointDef b2Def = new box2d.WeldJointDef(); - Vector2 middle = new Vector2( - (bodyA._body.position.x + bodyB._body.position.x) / 2.0, - (bodyA._body.position.y + bodyB._body.position.y) / 2.0 - ); - b2Def.initialize(bodyA._body, bodyB._body, middle); - return physicsNode.b2World.createJoint(b2Def); - } + box2d.Joint _createB2Joint(PhysicsNode physicsNode) { + box2d.WeldJointDef b2Def = new box2d.WeldJointDef(); + Vector2 middle = new Vector2( + (bodyA._body.position.x + bodyB._body.position.x) / 2.0, + (bodyA._body.position.y + bodyB._body.position.y) / 2.0 + ); + b2Def.initialize(bodyA._body, bodyB._body, middle); + return physicsNode.b2World.createJoint(b2Def); + } } diff --git a/packages/flutter_sprites/lib/physics_node.dart b/packages/flutter_sprites/lib/physics_node.dart index 659fff346a..fb76b21781 100644 --- a/packages/flutter_sprites/lib/physics_node.dart +++ b/packages/flutter_sprites/lib/physics_node.dart @@ -33,7 +33,7 @@ class PhysicsNode extends Node { List _bodiesScheduledForDestruction = []; - double b2WorldToNodeConversionFactor = 500.0; + double b2WorldToNodeConversionFactor = 10.0; Offset get gravity { Vector2 g = b2World.getGravity(); @@ -282,7 +282,7 @@ class PhysicsNode extends Node { // Draw the joint depending on type box2d.JointType type = joint.getType(); - if (type == box2d.JointType.WELD) { + if (type == box2d.JointType.WELD || type == box2d.JointType.REVOLUTE) { // Draw weld joint canvas.drawCircle(ptAnchorA, 5.0, shapePaint);