Skip to content

Commit 71bd610

Browse files
committed
Update to "Rename the Constraint class into Joint and do not perform collision detection between two sleeping bodies"
DanielChappuis/reactphysics3d@d4c7eee1755c1604 b4bc52bf9afb872226fb99ed
1 parent 85ff9e5 commit 71bd610

File tree

13 files changed

+252
-159
lines changed

13 files changed

+252
-159
lines changed

src/main/java/org/spout/physics/body/Body.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class Body {
3333
protected final int mID;
3434
protected boolean mIsAlreadyInIsland;
3535
protected boolean mIsAllowedToSleep;
36+
protected boolean mIsActive;
3637
protected boolean mIsSleeping;
3738
protected float mSleepTime;
3839

@@ -45,6 +46,7 @@ public Body(int id) {
4546
mID = id;
4647
mIsAlreadyInIsland = false;
4748
mIsAllowedToSleep = true;
49+
mIsActive = true;
4850
mIsSleeping = false;
4951
mSleepTime = 0;
5052
}
@@ -97,6 +99,24 @@ public void setIsAllowedToSleep(boolean isAllowedToSleep) {
9799
}
98100
}
99101

102+
/**
103+
* Returns true if the body is active, false if not.
104+
*
105+
* @return Whether or not the body is active
106+
*/
107+
public boolean isActive() {
108+
return mIsActive;
109+
}
110+
111+
/**
112+
* Sets the activity for this body. True for active, false for inactive.
113+
*
114+
* @param mIsActive True if this body is active, false if not
115+
*/
116+
public void setActive(boolean mIsActive) {
117+
this.mIsActive = mIsActive;
118+
}
119+
100120
/**
101121
* Returns whether or not the body is sleeping.
102122
*

src/main/java/org/spout/physics/body/CollisionBody.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class CollisionBody extends Body {
3939
protected final Transform mTransform;
4040
protected final Transform mOldTransform;
4141
protected float mInterpolationFactor;
42-
protected boolean mIsActive;
4342
protected boolean mIsMotionEnabled;
4443
protected boolean mIsCollisionEnabled;
4544
protected final AABB mAabb = new AABB();
@@ -60,7 +59,6 @@ public CollisionBody(Transform transform, CollisionShape collisionShape, int id)
6059
}
6160
mCollisionShape = collisionShape;
6261
mTransform = transform;
63-
mIsActive = true;
6462
mHasMoved = false;
6563
mIsMotionEnabled = true;
6664
mIsCollisionEnabled = true;
@@ -106,24 +104,6 @@ public void setCollisionShape(CollisionShape mCollisionShape) {
106104
this.mCollisionShape = mCollisionShape;
107105
}
108106

109-
/**
110-
* Returns true if the body is active, false if not.
111-
*
112-
* @return Whether or not the body is active
113-
*/
114-
public boolean isActive() {
115-
return mIsActive;
116-
}
117-
118-
/**
119-
* Sets the activity for this body. True for active, false for inactive.
120-
*
121-
* @param mIsActive True if this body is active, false if not
122-
*/
123-
public void setActive(boolean mIsActive) {
124-
this.mIsActive = mIsActive;
125-
}
126-
127107
/**
128108
* Returns an interpolated body from the old to the current transform, based on this body's interpolation factor.
129109
*

src/main/java/org/spout/physics/body/RigidBody.java

Lines changed: 78 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
package org.spout.physics.body;
2828

2929
import org.spout.physics.collision.shape.CollisionShape;
30-
import org.spout.physics.constraint.Constraint;
31-
import org.spout.physics.constraint.Constraint.JointListElement;
30+
import org.spout.physics.constraint.Joint;
31+
import org.spout.physics.constraint.Joint.JointListElement;
3232
import org.spout.physics.engine.Material;
3333
import org.spout.physics.math.Matrix3x3;
3434
import org.spout.physics.math.Transform;
@@ -168,42 +168,6 @@ public void setAngularVelocity(Vector3 angularVelocity) {
168168
mAngularVelocity.set(angularVelocity);
169169
}
170170

171-
/**
172-
* Gets the current external force on the body.
173-
*
174-
* @return The current external force
175-
*/
176-
public Vector3 getExternalForce() {
177-
return mExternalForce;
178-
}
179-
180-
/**
181-
* Sets the current external force on the body.
182-
*
183-
* @param force The external force to set
184-
*/
185-
public void setExternalForce(Vector3 force) {
186-
mExternalForce.set(force);
187-
}
188-
189-
/**
190-
* Gets the current external torque on the body.
191-
*
192-
* @return The current external torque
193-
*/
194-
public Vector3 getExternalTorque() {
195-
return mExternalTorque;
196-
}
197-
198-
/**
199-
* Sets the current external torque on the body.
200-
*
201-
* @param torque The external torque to set
202-
*/
203-
public void setExternalTorque(Vector3 torque) {
204-
mExternalTorque.set(torque);
205-
}
206-
207171
/**
208172
* Gets the local inertia tensor of the body (in body coordinates).
209173
*
@@ -325,7 +289,7 @@ public void setJointsList(JointListElement jointsList) {
325289
*
326290
* @param joint The joint to remove
327291
*/
328-
public void removeJointFromJointsList(Constraint joint) {
292+
public void removeJointFromJointsList(Joint joint) {
329293
if (joint == null) {
330294
throw new IllegalArgumentException("Joint cannot be null");
331295
}
@@ -358,4 +322,79 @@ public void setIsSleeping(boolean isSleeping) {
358322
}
359323
super.setIsSleeping(isSleeping);
360324
}
325+
326+
/**
327+
* Applies an external force to the body at its gravity center. If the body is sleeping, calling this method will wake it up. Note that the force will be added to the sum of the applied forces and
328+
* that this sum will be reset to zero at the end of each call of the {@link org.spout.physics.engine.DynamicsWorld#update()} method.
329+
*
330+
* @param force The force to apply
331+
*/
332+
public void applyForceToCenter(Vector3 force) {
333+
if (!mIsMotionEnabled) {
334+
return;
335+
}
336+
if (mIsSleeping) {
337+
setIsSleeping(false);
338+
}
339+
mExternalForce.add(force);
340+
}
341+
342+
/**
343+
* Applies an external force to the body at a given point (in world-coordinates). If the point is not at the center of gravity of the body, it will also generate some torque and therefore, change
344+
* the angular velocity of the body. If the body is sleeping, calling this method will wake it up. Note that the force will be added to the sum of the applied forces and that this sum will be
345+
* reset to zero at the end of each call of the {@link org.spout.physics.engine.DynamicsWorld#update()} method.
346+
*
347+
* @param force The force to apply
348+
* @param point The point to apply the force to
349+
*/
350+
public void applyForce(Vector3 force, Vector3 point) {
351+
if (!mIsMotionEnabled) {
352+
return;
353+
}
354+
if (mIsSleeping) {
355+
setIsSleeping(false);
356+
}
357+
mExternalForce.add(force);
358+
mExternalTorque.add(Vector3.subtract(point, mTransform.getPosition()).cross(force));
359+
}
360+
361+
/**
362+
* Applies an external torque to the body. If the body is sleeping, calling this method will wake it up. Note that the force will be added to the sum of the applied torques and that this sum will
363+
* be reset to zero at the end of each call of the {@link org.spout.physics.engine.DynamicsWorld#update()} method.
364+
*
365+
* @param torque The torque to apply
366+
*/
367+
public void applyTorque(Vector3 torque) {
368+
369+
// If it is a static body, do not apply any force
370+
if (!mIsMotionEnabled) {
371+
return;
372+
}
373+
374+
// Awake the body if it was sleeping
375+
if (mIsSleeping) {
376+
setIsSleeping(false);
377+
}
378+
379+
// Add the torque
380+
mExternalTorque.add(torque);
381+
}
382+
383+
/**
384+
* Returns the total external force.
385+
*
386+
* @return The external force
387+
*/
388+
public Vector3 getExternalForce() {
389+
return mExternalForce;
390+
}
391+
392+
/**
393+
* Returns the total external torque.
394+
*
395+
* @return The external torque
396+
*/
397+
public Vector3 getExternalTorque() {
398+
return mExternalTorque;
399+
}
361400
}

src/main/java/org/spout/physics/collision/CollisionDetection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ private void computeNarrowPhase() {
184184
if (mNoCollisionPairs.contains(pair.getBodiesIndexPair())) {
185185
continue;
186186
}
187+
if (body1.isSleeping() && body2.isSleeping()) {
188+
continue;
189+
}
187190
final NarrowPhaseAlgorithm narrowPhaseAlgorithm = selectNarrowPhaseAlgorithm(body1.getCollisionShape(), body2.getCollisionShape());
188191
narrowPhaseAlgorithm.setCurrentOverlappingPair(pair);
189192
if (narrowPhaseAlgorithm.testCollision(body1.getCollisionShape(), body1.getTransform(), body2.getCollisionShape(), body2.getTransform(), contactInfo)) {

src/main/java/org/spout/physics/constraint/BallAndSocketJoint.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
/**
3838
* This class represents a ball-and-socket joint that allows arbitrary rotation between two bodies.
3939
*/
40-
public class BallAndSocketJoint extends Constraint {
40+
public class BallAndSocketJoint extends Joint {
4141
private static final float BETA = 0.2f;
4242
private final Vector3 mLocalAnchorPointBody1;
4343
private final Vector3 mLocalAnchorPointBody2;
@@ -216,7 +216,7 @@ public void solvePositionConstraint(ConstraintSolverData constraintSolverData) {
216216
/**
217217
* This structure is used to gather the information needed to create a ball-and-socket joint. This structure will be used to create the actual ball-and-socket joint.
218218
*/
219-
public static class BallAndSocketJointInfo extends ConstraintInfo {
219+
public static class BallAndSocketJointInfo extends JointInfo {
220220
private final Vector3 anchorPointWorldSpace = new Vector3();
221221

222222
/**
@@ -227,7 +227,7 @@ public static class BallAndSocketJointInfo extends ConstraintInfo {
227227
* @param initAnchorPointWorldSpace The anchor point in world space
228228
*/
229229
public BallAndSocketJointInfo(RigidBody rigidBody1, RigidBody rigidBody2, Vector3 initAnchorPointWorldSpace) {
230-
super(rigidBody1, rigidBody2, ConstraintType.BALLSOCKETJOINT);
230+
super(rigidBody1, rigidBody2, JointType.BALLSOCKETJOINT);
231231
anchorPointWorldSpace.set(initAnchorPointWorldSpace);
232232
}
233233

src/main/java/org/spout/physics/constraint/ConstraintSolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void initializeForIsland(float dt, Island island) {
132132
mTimeStep = dt;
133133
mConstraintSolverData.setTimeStep(mTimeStep);
134134
mConstraintSolverData.setWarmStartingActive(mIsWarmStartingActive);
135-
final Constraint[] joints = island.getJoints();
135+
final Joint[] joints = island.getJoints();
136136
for (int i = 0; i < island.getNbJoints(); i++) {
137137
joints[i].initBeforeSolve(mConstraintSolverData);
138138
if (mIsWarmStartingActive) {
@@ -153,7 +153,7 @@ public void solveVelocityConstraints(Island island) {
153153
if (island.getNbJoints() <= 0) {
154154
throw new IllegalArgumentException("The number of joints in the island must be greater than zero");
155155
}
156-
final Constraint[] joints = island.getJoints();
156+
final Joint[] joints = island.getJoints();
157157
for (int i = 0; i < island.getNbJoints(); i++) {
158158
joints[i].solveVelocityConstraint(mConstraintSolverData);
159159
}
@@ -172,7 +172,7 @@ public void solvePositionConstraints(Island island) {
172172
//if (island.getNbJoints() <= 0) {
173173
// throw new IllegalArgumentException("The number of joints in the island must be greater than zero");
174174
//}
175-
final Constraint[] joints = island.getJoints();
175+
final Joint[] joints = island.getJoints();
176176
for (int i = 0; i < island.getNbJoints(); i++) {
177177
joints[i].solvePositionConstraint(mConstraintSolverData);
178178
}

0 commit comments

Comments
 (0)