Commit ca724ae1 authored by Viktor Lidholt's avatar Viktor Lidholt

Adds support for scaling sprite physics groups

parent 4187bcf9
...@@ -51,6 +51,8 @@ class TestBed extends NodeWithSize { ...@@ -51,6 +51,8 @@ class TestBed extends NodeWithSize {
TestBed() : super(new Size(1024.0, 1024.0)) { TestBed() : super(new Size(1024.0, 1024.0)) {
_physicsNode = new PhysicsWorld(new Offset(0.0, 100.0)); _physicsNode = new PhysicsWorld(new Offset(0.0, 100.0));
PhysicsGroup group = new PhysicsGroup();
_physicsNode.addChild(group);
_obstacle = new Sprite(_spriteSheet["ship.png"]); _obstacle = new Sprite(_spriteSheet["ship.png"]);
_obstacle.position = new Point(512.0, 800.0); _obstacle.position = new Point(512.0, 800.0);
...@@ -62,7 +64,7 @@ class TestBed extends NodeWithSize { ...@@ -62,7 +64,7 @@ class TestBed extends NodeWithSize {
friction: 0.5, friction: 0.5,
tag: "obstacle" tag: "obstacle"
); );
_physicsNode.addChild(_obstacle); group.addChild(_obstacle);
_physicsNode.addContactCallback(myCallback, "obstacle", "ship", PhysicsContactType.begin); _physicsNode.addContactCallback(myCallback, "obstacle", "ship", PhysicsContactType.begin);
// Animate obstacle // Animate obstacle
......
...@@ -277,15 +277,27 @@ class Node { ...@@ -277,15 +277,27 @@ class Node {
void set scale(double scale) { void set scale(double scale) {
assert(scale != null); assert(scale != null);
if (_physicsBody != null && parent is PhysicsWorld) { if (_physicsBody != null && (parent is PhysicsWorld || parent is PhysicsGroup)) {
PhysicsWorld physicsNode = parent; _updatePhysicsScale(physicsBody, scale, parent);
physicsNode._updateScale(this.physicsBody, scale);
} }
_scaleX = _scaleY = scale; _scaleX = _scaleY = scale;
invalidateTransformMatrix(); invalidateTransformMatrix();
} }
void _updatePhysicsScale(PhysicsBody body, double scale, Node physicsParent) {
if (physicsParent == null) return;
if (physicsParent is PhysicsWorld) {
PhysicsWorld world = physicsParent;
world._updateScale(body, scale);
} else if (physicsParent is PhysicsGroup) {
_updatePhysicsScale(body, scale * physicsParent.scale, physicsParent.parent);
} else {
assert(false);
}
}
/// The horizontal scale of this node relative its parent. /// The horizontal scale of this node relative its parent.
/// ///
/// myNode.scaleX = 5.0; /// myNode.scaleX = 5.0;
......
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