Commit 8f65e0cb authored by Viktor Lidholt's avatar Viktor Lidholt

Add callback when physics joints break

parent 2feb51d6
part of skysprites; part of skysprites;
typedef void PhysicsJointBreakCallback(PhysicsJoint joint);
abstract class PhysicsJoint { abstract class PhysicsJoint {
PhysicsJoint(this._bodyA, this._bodyB, this.breakingForce) { PhysicsJoint(this._bodyA, this._bodyB, this.breakingForce, this.breakCallback) {
bodyA._joints.add(this); bodyA._joints.add(this);
bodyB._joints.add(this); bodyB._joints.add(this);
} }
...@@ -16,6 +18,8 @@ abstract class PhysicsJoint { ...@@ -16,6 +18,8 @@ abstract class PhysicsJoint {
final double breakingForce; final double breakingForce;
final PhysicsJointBreakCallback breakCallback;
bool _active = true; bool _active = true;
box2d.Joint _joint; box2d.Joint _joint;
...@@ -58,9 +62,12 @@ abstract class PhysicsJoint { ...@@ -58,9 +62,12 @@ abstract class PhysicsJoint {
_joint.getReactionForce(1.0 / dt, reactionForce); _joint.getReactionForce(1.0 / dt, reactionForce);
if (breakingForce * breakingForce < reactionForce.length2) { if (breakingForce * breakingForce < reactionForce.length2) {
// TODO: Add callback // Destroy the joint
destroy(); destroy();
// Notify any observer
if (breakCallback != null)
breakCallback(this);
} }
} }
} }
...@@ -74,8 +81,9 @@ class PhysicsJointRevolute extends PhysicsJoint { ...@@ -74,8 +81,9 @@ class PhysicsJointRevolute extends PhysicsJoint {
this.lowerAngle: 0.0, this.lowerAngle: 0.0,
this.upperAngle: 0.0, this.upperAngle: 0.0,
this.enableLimit: false, this.enableLimit: false,
PhysicsJointBreakCallback breakCallback,
double breakingForce double breakingForce
}) : super(bodyA, bodyB, breakingForce) { }) : super(bodyA, bodyB, breakingForce, breakCallback) {
_completeCreation(); _completeCreation();
} }
...@@ -107,9 +115,10 @@ class PhysicsJointPrismatic extends PhysicsJoint { ...@@ -107,9 +115,10 @@ class PhysicsJointPrismatic extends PhysicsJoint {
PhysicsBody bodyA, PhysicsBody bodyA,
PhysicsBody bodyB, PhysicsBody bodyB,
this.axis, { this.axis, {
double breakingForce double breakingForce,
PhysicsJointBreakCallback breakCallback
} }
) : super(bodyA, bodyB, breakingForce) { ) : super(bodyA, bodyB, breakingForce, breakCallback) {
_completeCreation(); _completeCreation();
} }
...@@ -127,10 +136,11 @@ class PhysicsJointWeld extends PhysicsJoint { ...@@ -127,10 +136,11 @@ class PhysicsJointWeld extends PhysicsJoint {
PhysicsBody bodyA, PhysicsBody bodyA,
PhysicsBody bodyB, { PhysicsBody bodyB, {
double breakingForce, double breakingForce,
PhysicsJointBreakCallback breakCallback,
this.dampening: 0.0, this.dampening: 0.0,
this.frequency: 0.0 this.frequency: 0.0
} }
) : super(bodyA, bodyB, breakingForce) { ) : super(bodyA, bodyB, breakingForce, breakCallback) {
_completeCreation(); _completeCreation();
} }
...@@ -159,9 +169,10 @@ class PhysicsJointPulley extends PhysicsJoint { ...@@ -159,9 +169,10 @@ class PhysicsJointPulley extends PhysicsJoint {
this.anchorA, this.anchorA,
this.anchorB, this.anchorB,
this.ratio, { this.ratio, {
double breakingForce double breakingForce,
PhysicsJointBreakCallback breakCallback
} }
) : super(bodyA, bodyB, breakingForce) { ) : super(bodyA, bodyB, breakingForce, breakCallback) {
_completeCreation(); _completeCreation();
} }
...@@ -191,9 +202,10 @@ class PhysicsJointGear extends PhysicsJoint { ...@@ -191,9 +202,10 @@ class PhysicsJointGear extends PhysicsJoint {
PhysicsBody bodyA, PhysicsBody bodyA,
PhysicsBody bodyB, { PhysicsBody bodyB, {
double breakingForce, double breakingForce,
PhysicsJointBreakCallback breakCallback,
this.ratio: 0.0 this.ratio: 0.0
} }
) : super(bodyA, bodyB, breakingForce) { ) : super(bodyA, bodyB, breakingForce, breakCallback) {
_completeCreation(); _completeCreation();
} }
...@@ -216,11 +228,12 @@ class PhysicsJointDistance extends PhysicsJoint { ...@@ -216,11 +228,12 @@ class PhysicsJointDistance extends PhysicsJoint {
this.anchorA, this.anchorA,
this.anchorB, { this.anchorB, {
double breakingForce, double breakingForce,
PhysicsJointBreakCallback breakCallback,
this.length, this.length,
this.dampening: 0.0, this.dampening: 0.0,
this.frequency: 0.0 this.frequency: 0.0
} }
) : super(bodyA, bodyB, breakingForce) { ) : super(bodyA, bodyB, breakingForce, breakCallback) {
_completeCreation(); _completeCreation();
} }
...@@ -254,10 +267,11 @@ class PhysicsJointWheel extends PhysicsJoint { ...@@ -254,10 +267,11 @@ class PhysicsJointWheel extends PhysicsJoint {
this.anchor, this.anchor,
this.axis, { this.axis, {
double breakingForce, double breakingForce,
PhysicsJointBreakCallback breakCallback,
this.dampening: 0.0, this.dampening: 0.0,
this.frequency: 0.0 this.frequency: 0.0
} }
) : super(bodyA, bodyB, breakingForce) { ) : super(bodyA, bodyB, breakingForce, breakCallback) {
_completeCreation(); _completeCreation();
} }
...@@ -287,10 +301,11 @@ class PhysicsJointFriction extends PhysicsJoint { ...@@ -287,10 +301,11 @@ class PhysicsJointFriction extends PhysicsJoint {
PhysicsBody bodyB, PhysicsBody bodyB,
this.anchor, { this.anchor, {
double breakingForce, double breakingForce,
PhysicsJointBreakCallback breakCallback,
this.maxForce: 0.0, this.maxForce: 0.0,
this.maxTorque: 0.0 this.maxTorque: 0.0
} }
) : super(bodyA, bodyB, breakingForce) { ) : super(bodyA, bodyB, breakingForce, breakCallback) {
_completeCreation(); _completeCreation();
} }
...@@ -315,10 +330,11 @@ class PhysicsJointConstantVolume extends PhysicsJoint { ...@@ -315,10 +330,11 @@ class PhysicsJointConstantVolume extends PhysicsJoint {
PhysicsJointConstantVolume( PhysicsJointConstantVolume(
this.bodies, { this.bodies, {
double breakingForce, double breakingForce,
PhysicsJointBreakCallback breakCallback,
this.dampening, this.dampening,
this.frequency this.frequency
} }
) : super(null, null, breakingForce) { ) : super(null, null, breakingForce, breakCallback) {
assert(bodies.length > 2); assert(bodies.length > 2);
_bodyA = bodies[0]; _bodyA = bodies[0];
_bodyB = bodies[1]; _bodyB = bodies[1];
......
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