Skip to content

Commit 85ff9e5

Browse files
committed
Update to "Continue to implement the sleeping technique"
DanielChappuis/reactphysics3d@8db7823433deac41 c7ca47212fbc72786beafc17
1 parent d2c8011 commit 85ff9e5

File tree

8 files changed

+269
-75
lines changed

8 files changed

+269
-75
lines changed

src/main/java/org/spout/physics/ReactDefaults.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class ReactDefaults {
5555
*/
5656
public static final float DEFAULT_BOUNCINESS = 0.5f;
5757
/**
58-
* True if the deactivation (sleeping) of inactive bodies is enabled. Default: true
58+
* True if the sleeping technique is enabled. Default: true
5959
*/
6060
public static final boolean SLEEPING_ENABLED = true;
6161
/**
@@ -78,6 +78,18 @@ public class ReactDefaults {
7878
* Number of iterations when solving the position constraints of the Sequential Impulse technique. Default: 5
7979
*/
8080
public static final int DEFAULT_POSITION_SOLVER_NB_ITERATIONS = 5;
81+
/**
82+
* Time (in seconds) that a body must stay still to be considered sleeping. Default: 1
83+
*/
84+
public static final float DEFAULT_TIME_BEFORE_SLEEP = 1.0f;
85+
/**
86+
* A body with a linear velocity smaller than the sleep linear velocity (in m/s) might enter sleeping mode. Default: 0.02
87+
*/
88+
public static final float DEFAULT_SLEEP_LINEAR_VELOCITY = 0.02f;
89+
/**
90+
* A body with angular velocity smaller than the sleep angular velocity (in rad/s) might enter sleeping mode. Default: 3pi/180
91+
*/
92+
public static final float DEFAULT_SLEEP_ANGULAR_VELOCITY = (float) (3 * (Math.PI / 180));
8193
/**
8294
* The linked phase AABB scaling factor. Default: 2
8395
*/

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class Body {
3434
protected boolean mIsAlreadyInIsland;
3535
protected boolean mIsAllowedToSleep;
3636
protected boolean mIsSleeping;
37+
protected float mSleepTime;
3738

3839
/**
3940
* Construct a new body from its ID.
@@ -42,6 +43,10 @@ public class Body {
4243
*/
4344
public Body(int id) {
4445
mID = id;
46+
mIsAlreadyInIsland = false;
47+
mIsAllowedToSleep = true;
48+
mIsSleeping = false;
49+
mSleepTime = 0;
4550
}
4651

4752
/**
@@ -87,6 +92,9 @@ public boolean isAllowedToSleep() {
8792
*/
8893
public void setIsAllowedToSleep(boolean isAllowedToSleep) {
8994
mIsAllowedToSleep = isAllowedToSleep;
95+
if (!mIsAllowedToSleep) {
96+
setIsSleeping(false);
97+
}
9098
}
9199

92100
/**
@@ -104,9 +112,34 @@ public boolean isSleeping() {
104112
* @param isSleeping Whether or not the body is sleeping
105113
*/
106114
public void setIsSleeping(boolean isSleeping) {
115+
if (isSleeping) {
116+
mSleepTime = 0;
117+
} else {
118+
if (mIsSleeping) {
119+
mSleepTime = 0;
120+
}
121+
}
107122
mIsSleeping = isSleeping;
108123
}
109124

125+
/**
126+
* Returns the sleep time.
127+
*
128+
* @return The sleep time
129+
*/
130+
public float getSleepTime() {
131+
return mSleepTime;
132+
}
133+
134+
/**
135+
* Sets the sleep time.
136+
*
137+
* @param sleepTime The sleep time
138+
*/
139+
public void setSleepTime(float sleepTime) {
140+
mSleepTime = sleepTime;
141+
}
142+
110143
/**
111144
* Returns true this body's ID is smaller than the other body's ID, false if not.
112145
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,15 @@ public void removeJointFromJointsList(Constraint joint) {
347347
}
348348
}
349349
}
350+
351+
@Override
352+
public void setIsSleeping(boolean isSleeping) {
353+
if (isSleeping) {
354+
mLinearVelocity.setToZero();
355+
mAngularVelocity.setToZero();
356+
mExternalForce.setToZero();
357+
mExternalTorque.setToZero();
358+
}
359+
super.setIsSleeping(isSleeping);
360+
}
350361
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ public void solvePositionConstraints(Island island) {
168168
if (island == null) {
169169
throw new IllegalArgumentException("Island cannot be null");
170170
}
171-
if (island.getNbJoints() <= 0) {
172-
throw new IllegalArgumentException("The number of joints in the island must be greater than zero");
173-
}
171+
// TODO: this doesn't make sense
172+
//if (island.getNbJoints() <= 0) {
173+
// throw new IllegalArgumentException("The number of joints in the island must be greater than zero");
174+
//}
174175
final Constraint[] joints = island.getJoints();
175176
for (int i = 0; i < island.getNbJoints(); i++) {
176177
joints[i].solvePositionConstraint(mConstraintSolverData);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ public void enableLimit(boolean isLimitEnabled) {
509509
public void enableMotor(boolean isMotorEnabled) {
510510
mIsMotorEnabled = isMotorEnabled;
511511
mImpulseMotor = 0;
512-
// TODO : Wake up the bodies of the joint here when sleeping is implemented
512+
mBody1.setIsSleeping(false);
513+
mBody2.setIsSleeping(false);
513514
}
514515

515516
/**
@@ -546,7 +547,8 @@ public void setMaxAngleLimit(float upperLimit) {
546547
private void resetLimits() {
547548
mImpulseLowerLimit = 0;
548549
mImpulseUpperLimit = 0;
549-
// TODO : Wake up the bodies of the joint here when sleeping is implemented
550+
mBody1.setIsSleeping(false);
551+
mBody2.setIsSleeping(false);
550552
}
551553

552554
/**
@@ -557,7 +559,8 @@ private void resetLimits() {
557559
public void setMotorSpeed(float motorSpeed) {
558560
if (motorSpeed != mMotorSpeed) {
559561
mMotorSpeed = motorSpeed;
560-
// TODO : Wake up the bodies of the joint here when sleeping is implemented
562+
mBody1.setIsSleeping(false);
563+
mBody2.setIsSleeping(false);
561564
}
562565
}
563566

@@ -572,7 +575,8 @@ public void setMaxMotorForce(float maxMotorForce) {
572575
}
573576
if (maxMotorForce != mMaxMotorTorque) {
574577
mMaxMotorTorque = maxMotorForce;
575-
// TODO : Wake up the bodies of the joint here when sleeping is implemented
578+
mBody1.setIsSleeping(false);
579+
mBody2.setIsSleeping(false);
576580
}
577581
}
578582

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ public void enableLimit(boolean isLimitEnabled) {
549549
public void enableMotor(boolean isMotorEnabled) {
550550
mIsMotorEnabled = isMotorEnabled;
551551
mImpulseMotor = 0;
552-
// TODO : Wake up the bodies of the joint here when sleeping is implemented
552+
mBody1.setIsSleeping(false);
553+
mBody2.setIsSleeping(false);
553554
}
554555

555556
/**
@@ -586,7 +587,8 @@ public void setMaxTranslationLimit(float upperLimit) {
586587
private void resetLimits() {
587588
mImpulseLowerLimit = 0;
588589
mImpulseUpperLimit = 0;
589-
// TODO : Wake up the bodies of the joint here when sleeping is implemented
590+
mBody1.setIsSleeping(false);
591+
mBody2.setIsSleeping(false);
590592
}
591593

592594
/**
@@ -597,7 +599,8 @@ private void resetLimits() {
597599
public void setMotorSpeed(float motorSpeed) {
598600
if (motorSpeed != mMotorSpeed) {
599601
mMotorSpeed = motorSpeed;
600-
// TODO : Wake up the bodies of the joint here when sleeping is implemented
602+
mBody1.setIsSleeping(false);
603+
mBody2.setIsSleeping(false);
601604
}
602605
}
603606

@@ -612,7 +615,8 @@ public void setMaxMotorForce(float maxMotorForce) {
612615
}
613616
if (maxMotorForce != mMaxMotorForce) {
614617
mMaxMotorForce = maxMotorForce;
615-
// TODO : Wake up the bodies of the joint here when sleeping is implemented
618+
mBody1.setIsSleeping(false);
619+
mBody2.setIsSleeping(false);
616620
}
617621
}
618622

src/main/java/org/spout/physics/engine/ContactSolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public void initializeForIsland(float dt, Island island) {
219219
for (int i = 0; i < mNbContactManifolds; i++) {
220220
final ContactManifold externalManifold = contactManifolds[i];
221221
final ContactManifoldSolver internalManifold = new ContactManifoldSolver();
222-
mContactConstraints[i] = new ContactManifoldSolver();
222+
mContactConstraints[i] = internalManifold;
223223
if (externalManifold.getNbContactPoints() <= 0) {
224224
throw new IllegalStateException("external manifold must have at least one contact point");
225225
}

0 commit comments

Comments
 (0)