Commit 333c8f5f authored by Viktor Lidholt's avatar Viktor Lidholt

Updates sprite physics test

parent 95a0acea
...@@ -45,24 +45,11 @@ main() async { ...@@ -45,24 +45,11 @@ main() async {
} }
class TestBed extends NodeWithSize { class TestBed extends NodeWithSize {
Sprite _ship;
Sprite _obstacle; Sprite _obstacle;
PhysicsNode _physicsNode;
TestBed() : super(new Size(1024.0, 1024.0)) { TestBed() : super(new Size(1024.0, 1024.0)) {
PhysicsNode physicsNode = new PhysicsNode(new Offset(0.0, 100.0)); _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);
_obstacle = new Sprite(_spriteSheet["ship.png"]); _obstacle = new Sprite(_spriteSheet["ship.png"]);
_obstacle.position = new Point(532.0, 800.0); _obstacle.position = new Point(532.0, 800.0);
...@@ -73,23 +60,47 @@ class TestBed extends NodeWithSize { ...@@ -73,23 +60,47 @@ class TestBed extends NodeWithSize {
friction: 0.5, friction: 0.5,
tag: "obstacle" tag: "obstacle"
); );
physicsNode.addChild(_obstacle); _physicsNode.addChild(_obstacle);
physicsNode.addContactCallback(myCallback, "obstacle", "ship", PhysicsContactType.begin); _physicsNode.addContactCallback(myCallback, "obstacle", "ship", PhysicsContactType.begin);
addChild(physicsNode); addChild(_physicsNode);
userInteractionEnabled = true; userInteractionEnabled = true;
} }
void myCallback(PhysicsContactType type, PhysicsContact contact) { void myCallback(PhysicsContactType type, PhysicsContact contact) {
print("CONTACT type: $type"); print("CONTACT type: $type");
contact.nodeB.removeFromParent();
} }
bool handleEvent(SpriteBoxEvent event) { bool handleEvent(SpriteBoxEvent event) {
if (event.type == "pointerdown") { if (event.type == "pointerdown") {
Point pos = convertPointToNodeSpace(event.boxPosition); 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; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment