Merge pull request #1588 from vlidholt/master
Adds check for breaking forces in sprite physics joints
This commit is contained in:
commit
106d3c4bd1
@ -1,13 +1,14 @@
|
|||||||
part of skysprites;
|
part of skysprites;
|
||||||
|
|
||||||
abstract class PhysicsJoint {
|
abstract class PhysicsJoint {
|
||||||
PhysicsJoint(this.bodyA, this.bodyB) {
|
PhysicsJoint(this.bodyA, this.bodyB, this.breakingForce) {
|
||||||
bodyA._joints.add(this);
|
bodyA._joints.add(this);
|
||||||
bodyB._joints.add(this);
|
bodyB._joints.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
final PhysicsBody bodyA;
|
final PhysicsBody bodyA;
|
||||||
final PhysicsBody bodyB;
|
final PhysicsBody bodyB;
|
||||||
|
final double breakingForce;
|
||||||
|
|
||||||
bool _active = true;
|
bool _active = true;
|
||||||
box2d.Joint _joint;
|
box2d.Joint _joint;
|
||||||
@ -24,6 +25,7 @@ abstract class PhysicsJoint {
|
|||||||
if (_joint == null) {
|
if (_joint == null) {
|
||||||
_physicsNode = physicsNode;
|
_physicsNode = physicsNode;
|
||||||
_joint = _createB2Joint(physicsNode);
|
_joint = _createB2Joint(physicsNode);
|
||||||
|
_physicsNode._joints.add(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,11 +33,31 @@ abstract class PhysicsJoint {
|
|||||||
if (_joint != null && _active) {
|
if (_joint != null && _active) {
|
||||||
_physicsNode.b2World.destroyJoint(_joint);
|
_physicsNode.b2World.destroyJoint(_joint);
|
||||||
_joint = null;
|
_joint = null;
|
||||||
|
_physicsNode._joints.remove(this);
|
||||||
}
|
}
|
||||||
_active = false;
|
_active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
box2d.Joint _createB2Joint(PhysicsNode physicsNode);
|
box2d.Joint _createB2Joint(PhysicsNode physicsNode);
|
||||||
|
|
||||||
|
void destroy() {
|
||||||
|
_detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _checkBreakingForce(double dt) {
|
||||||
|
if (breakingForce == null) return;
|
||||||
|
|
||||||
|
if (_joint != null && _active) {
|
||||||
|
Vector2 reactionForce = new Vector2.zero();
|
||||||
|
_joint.getReactionForce(1.0 / dt, reactionForce);
|
||||||
|
|
||||||
|
if (breakingForce * breakingForce < reactionForce.length2) {
|
||||||
|
// TODO: Add callback
|
||||||
|
|
||||||
|
destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PhysicsJointRevolute extends PhysicsJoint {
|
class PhysicsJointRevolute extends PhysicsJoint {
|
||||||
@ -45,8 +67,9 @@ class PhysicsJointRevolute extends PhysicsJoint {
|
|||||||
this.worldAnchor, {
|
this.worldAnchor, {
|
||||||
this.lowerAngle: 0.0,
|
this.lowerAngle: 0.0,
|
||||||
this.upperAngle: 0.0,
|
this.upperAngle: 0.0,
|
||||||
this.enableLimit: false
|
this.enableLimit: false,
|
||||||
}) : super(bodyA, bodyB) {
|
double breakingForce
|
||||||
|
}) : super(bodyA, bodyB, breakingForce) {
|
||||||
_completeCreation();
|
_completeCreation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,8 +100,10 @@ class PhysicsJointPrismatic extends PhysicsJoint {
|
|||||||
PhysicsJointPrismatic(
|
PhysicsJointPrismatic(
|
||||||
PhysicsBody bodyA,
|
PhysicsBody bodyA,
|
||||||
PhysicsBody bodyB,
|
PhysicsBody bodyB,
|
||||||
this.axis
|
this.axis, {
|
||||||
) : super(bodyA, bodyB) {
|
double breakingForce
|
||||||
|
}
|
||||||
|
) : super(bodyA, bodyB, breakingForce) {
|
||||||
_completeCreation();
|
_completeCreation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,8 +119,10 @@ class PhysicsJointPrismatic extends PhysicsJoint {
|
|||||||
class PhysicsJointWeld extends PhysicsJoint {
|
class PhysicsJointWeld extends PhysicsJoint {
|
||||||
PhysicsJointWeld(
|
PhysicsJointWeld(
|
||||||
PhysicsBody bodyA,
|
PhysicsBody bodyA,
|
||||||
PhysicsBody bodyB
|
PhysicsBody bodyB, {
|
||||||
) : super(bodyA, bodyB) {
|
double breakingForce
|
||||||
|
}
|
||||||
|
) : super(bodyA, bodyB, breakingForce) {
|
||||||
_completeCreation();
|
_completeCreation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,8 +145,10 @@ class PhysicsJointPulley extends PhysicsJoint {
|
|||||||
this.groundAnchorB,
|
this.groundAnchorB,
|
||||||
this.anchorA,
|
this.anchorA,
|
||||||
this.anchorB,
|
this.anchorB,
|
||||||
this.ratio
|
this.ratio, {
|
||||||
) : super(bodyA, bodyB) {
|
double breakingForce
|
||||||
|
}
|
||||||
|
) : super(bodyA, bodyB, breakingForce) {
|
||||||
_completeCreation();
|
_completeCreation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,8 @@ class PhysicsNode extends Node {
|
|||||||
|
|
||||||
_ContactHandler _contactHandler;
|
_ContactHandler _contactHandler;
|
||||||
|
|
||||||
|
List<PhysicsJoint> _joints = [];
|
||||||
|
|
||||||
List<box2d.Body> _bodiesScheduledForDestruction = [];
|
List<box2d.Body> _bodiesScheduledForDestruction = [];
|
||||||
|
|
||||||
double b2WorldToNodeConversionFactor = 10.0;
|
double b2WorldToNodeConversionFactor = 10.0;
|
||||||
@ -77,6 +79,11 @@ class PhysicsNode extends Node {
|
|||||||
body._node._setRotationFromPhysics(degrees(b2Body.getAngle()));
|
body._node._setRotationFromPhysics(degrees(b2Body.getAngle()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Break joints
|
||||||
|
for (PhysicsJoint joint in _joints) {
|
||||||
|
joint._checkBreakingForce(dt);
|
||||||
|
}
|
||||||
|
|
||||||
// Remove bodies that were marked for destruction during the simulation
|
// Remove bodies that were marked for destruction during the simulation
|
||||||
_removeBodiesScheduledForDestruction();
|
_removeBodiesScheduledForDestruction();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user