From 5ac4d1ea281c9d7dd872f439b17b7c1ce2062e4c Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 6 Oct 2015 09:59:16 -0700 Subject: [PATCH 1/2] Adds forwarding of properties to box2d in sprite physics --- .../flutter_sprites/lib/physics_body.dart | 76 ++++++++++++++++--- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/packages/flutter_sprites/lib/physics_body.dart b/packages/flutter_sprites/lib/physics_body.dart index d42fa8d98b..dac140513a 100644 --- a/packages/flutter_sprites/lib/physics_body.dart +++ b/packages/flutter_sprites/lib/physics_body.dart @@ -13,10 +13,10 @@ class PhysicsBody { double friction: 0.0, double restitution: 0.0, bool isSensor: false, - this.linearVelocity: Offset.zero, - this.angularVelocity: 0.0, + Offset linearVelocity: Offset.zero, + double angularVelocity: 0.0, this.linearDampening: 0.0, - this.angularDampening: 0.0, + double awakeangularDampening: 0.0, this.allowSleep: true, this.awake: true, this.fixedRotation: false, @@ -28,11 +28,15 @@ class PhysicsBody { this.friction = friction; this.restitution = restitution; this.isSensor = isSensor; + + this.linearVelocity = linearVelocity; + this.angularVelocity = angularVelocity; + this.angularDampening = angularDampening; } Object tag; - PhysicsShape shape; + final PhysicsShape shape; PhysicsBodyType type; @@ -45,7 +49,7 @@ class PhysicsBody { if (_body == null) return; - for(box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) { + for (box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) { f.setDensity(density); } } @@ -59,7 +63,7 @@ class PhysicsBody { if (_body == null) return; - for(box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) { + for (box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) { f.setFriction(friction); } } @@ -73,7 +77,7 @@ class PhysicsBody { if (_body == null) return; - for(box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) { + for (box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) { f.setRestitution(restitution); } } @@ -87,17 +91,65 @@ class PhysicsBody { if (_body == null) return; - for(box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) { + for (box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) { f.setSensor(isSensor); } } - Offset linearVelocity; - double angularVelocity; + Offset _linearVelocity; - double linearDampening; + Offset get linearVelocity { + if (_body == null) + return _linearVelocity; + else { + double dx = _body.linearVelocity.x * _physicsNode.b2WorldToNodeConversionFactor; + double dy = _body.linearVelocity.y * _physicsNode.b2WorldToNodeConversionFactor; + return new Offset(dx, dy); + } + } - double angularDampening; + set linearVelocity(Offset linearVelocity) { + _linearVelocity = linearVelocity; + + if (_body != null) { + Vector2 vec = new Vector2( + linearVelocity.dx / _physicsNode.b2WorldToNodeConversionFactor, + linearVelocity.dy / _physicsNode.b2WorldToNodeConversionFactor + ); + _body.linearVelocity = vec; + } + } + + double _angularVelocity; + + double get angularVelocity { + if (_body == null) + return _angularVelocity; + else + return _body.angularVelocity; + } + + set angularVelocity(double angularVelocity) { + _angularVelocity = angularVelocity; + + if (_body != null) { + _body.angularVelocity = angularVelocity; + } + } + + // TODO: Should this be editable in box2d.Body ? + final double linearDampening; + + double _angularDampening; + + double get angularDampening => _angularDampening; + + set angularDampening(double angularDampening) { + _angularDampening = angularDampening; + + if (_body != null) + _body.angularDamping = angularDampening; + } bool allowSleep; From b6e12ca9aa809a9f67633fee0367bd34c1114b0c Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 6 Oct 2015 10:39:26 -0700 Subject: [PATCH 2/2] Forwards physics body properties to box2d in sprite physics --- .../flutter_sprites/lib/physics_body.dart | 82 ++++++++++++++++--- 1 file changed, 72 insertions(+), 10 deletions(-) diff --git a/packages/flutter_sprites/lib/physics_body.dart b/packages/flutter_sprites/lib/physics_body.dart index dac140513a..0d12febef5 100644 --- a/packages/flutter_sprites/lib/physics_body.dart +++ b/packages/flutter_sprites/lib/physics_body.dart @@ -17,11 +17,11 @@ class PhysicsBody { double angularVelocity: 0.0, this.linearDampening: 0.0, double awakeangularDampening: 0.0, - this.allowSleep: true, - this.awake: true, - this.fixedRotation: false, - this.bullet: false, - this.active: true, + bool allowSleep: true, + bool awake: true, + bool fixedRotation: false, + bool bullet: false, + bool active: true, this.gravityScale: 1.0 }) { this.density = density; @@ -32,6 +32,12 @@ class PhysicsBody { this.linearVelocity = linearVelocity; this.angularVelocity = angularVelocity; this.angularDampening = angularDampening; + + this.allowSleep = allowSleep; + this.awake = awake; + this.fixedRotation = fixedRotation; + this.bullet = bullet; + this.active = active; } Object tag; @@ -151,15 +157,71 @@ class PhysicsBody { _body.angularDamping = angularDampening; } - bool allowSleep; + bool _allowSleep; - bool awake; + bool get allowSleep => _allowSleep; - bool fixedRotation; + set allowSleep(bool allowSleep) { + _allowSleep = allowSleep; - bool bullet; + if (_body != null) + _body.setSleepingAllowed(allowSleep); + } - bool active; + bool _awake; + + bool get awake { + if (_body != null) + return _body.isAwake(); + else + return _awake; + } + + set awake(bool awake) { + _awake = awake; + + if (_body != null) + _body.setAwake(awake); + } + + bool _fixedRotation; + + bool get fixedRotation => _fixedRotation; + + set fixedRotation(bool fixedRotation) { + _fixedRotation = fixedRotation; + + if (_body != null) + _body.setFixedRotation(fixedRotation); + } + + bool _bullet; + + bool get bullet => _bullet; + + set bullet(bool bullet) { + _bullet = bullet; + + if (_body != null) { + _body.setBullet(bullet); + } + } + + bool _active; + + bool get active { + if (_body != null) + return _body.isActive(); + else + return _active; + } + + set active(bool active) { + _active = active; + + if (_body != null) + _body.setActive(active); + } double gravityScale;