From ed999dca028bf7082b90499a8b51bca58d0f6604 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 8 Oct 2015 16:13:56 -0700 Subject: [PATCH] Updates sprite physics test --- examples/game/test_physics.dart | 51 ++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/examples/game/test_physics.dart b/examples/game/test_physics.dart index aabb773bd..137a3257c 100644 --- a/examples/game/test_physics.dart +++ b/examples/game/test_physics.dart @@ -45,24 +45,11 @@ main() async { } class TestBed extends NodeWithSize { - Sprite _ship; Sprite _obstacle; + PhysicsNode _physicsNode; TestBed() : super(new Size(1024.0, 1024.0)) { - PhysicsNode physicsNode = new PhysicsNode(new Offset(0.0, 100.0)); - - _ship = new Sprite(_spriteSheet["ship.png"]); - _ship.position = new Point(512.0, 512.0); - _ship.size = new Size(64.0, 64.0); - _ship.physicsBody = new PhysicsBody( - new PhysicsShapeGroup([ - new PhysicsShapeCircle(Point.origin, 32.0), - new PhysicsShapePolygon([new Point(0.0, 0.0), new Point(50.0, 0.0), new Point(50.0, 50.0), new Point(0.0, 50.0)]) - ]), - friction: 0.5, - tag: "ship" - ); - physicsNode.addChild(_ship); + _physicsNode = new PhysicsNode(new Offset(0.0, 100.0)); _obstacle = new Sprite(_spriteSheet["ship.png"]); _obstacle.position = new Point(532.0, 800.0); @@ -73,23 +60,47 @@ class TestBed extends NodeWithSize { friction: 0.5, tag: "obstacle" ); - physicsNode.addChild(_obstacle); - physicsNode.addContactCallback(myCallback, "obstacle", "ship", PhysicsContactType.begin); + _physicsNode.addChild(_obstacle); + _physicsNode.addContactCallback(myCallback, "obstacle", "ship", PhysicsContactType.begin); - addChild(physicsNode); + addChild(_physicsNode); userInteractionEnabled = true; } void myCallback(PhysicsContactType type, PhysicsContact contact) { print("CONTACT type: $type"); - contact.nodeB.removeFromParent(); } bool handleEvent(SpriteBoxEvent event) { if (event.type == "pointerdown") { Point pos = convertPointToNodeSpace(event.boxPosition); - _ship.position = pos; + + Sprite shipA; + shipA = new Sprite(_spriteSheet["ship.png"]); + 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, + tag: "ship" + ); + _physicsNode.addChild(shipA); + shipA.physicsBody.applyLinearImpulse( + new Offset(randomSignedDouble() * 5.0, randomSignedDouble() * 5.0), + shipA.position + ); + + Sprite shipB; + shipB = new Sprite(_spriteSheet["ship.png"]); + 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, + tag: "ship" + ); + _physicsNode.addChild(shipB); + + new PhysicsJointWeld(shipA.physicsBody, shipB.physicsBody); } return true; } -- GitLab