Put bodies to sleep if needed. For each island, if all the bodies have been almost still for a long enough period of time, we put all the bodies of the island to sleep.
| 872 | /// For each island, if all the bodies have been almost still for a long enough period of |
| 873 | /// time, we put all the bodies of the island to sleep. |
| 874 | void PhysicsWorld::updateSleepingBodies(decimal timeStep) { |
| 875 | |
| 876 | RP3D_PROFILE("PhysicsWorld::updateSleepingBodies()", mProfiler); |
| 877 | |
| 878 | const decimal sleepLinearVelocitySquare = mSleepLinearVelocity * mSleepLinearVelocity; |
| 879 | const decimal sleepAngularVelocitySquare = mSleepAngularVelocity * mSleepAngularVelocity; |
| 880 | |
| 881 | // For each island of the world |
| 882 | const uint32 nbIslands = mIslands.getNbIslands(); |
| 883 | for (uint32 i=0; i < nbIslands; i++) { |
| 884 | |
| 885 | decimal minSleepTime = DECIMAL_LARGEST; |
| 886 | |
| 887 | // For each body of the island |
| 888 | for (uint32 b=0; b < mIslands.nbBodiesInIsland[i]; b++) { |
| 889 | |
| 890 | const Entity bodyEntity = mIslands.bodyEntities[mIslands.startBodyEntitiesIndex[i] + b]; |
| 891 | const uint32 bodyIndex = mRigidBodyComponents.getEntityIndex(bodyEntity); |
| 892 | |
| 893 | // Skip static bodies |
| 894 | if (mRigidBodyComponents.mBodyTypes[bodyIndex] == BodyType::STATIC) continue; |
| 895 | |
| 896 | // If the body is velocity is large enough to stay awake |
| 897 | if (mRigidBodyComponents.mLinearVelocities[bodyIndex].lengthSquare() > sleepLinearVelocitySquare || |
| 898 | mRigidBodyComponents.mAngularVelocities[bodyIndex].lengthSquare() > sleepAngularVelocitySquare || |
| 899 | !mRigidBodyComponents.mIsAllowedToSleep[bodyIndex]) { |
| 900 | |
| 901 | // Reset the sleep time of the body |
| 902 | mRigidBodyComponents.mSleepTimes[bodyIndex] = decimal(0.0); |
| 903 | minSleepTime = decimal(0.0); |
| 904 | } |
| 905 | else { // If the body velocity is below the sleeping velocity threshold |
| 906 | |
| 907 | // Increase the sleep time |
| 908 | mRigidBodyComponents.mSleepTimes[bodyIndex] += timeStep; |
| 909 | if (mRigidBodyComponents.mSleepTimes[bodyIndex] < minSleepTime) { |
| 910 | minSleepTime = mRigidBodyComponents.mSleepTimes[bodyIndex]; |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | // If the velocity of all the bodies of the island is under the |
| 916 | // sleeping velocity threshold for a period of time larger than |
| 917 | // the time required to become a sleeping body |
| 918 | if (minSleepTime >= mTimeBeforeSleep) { |
| 919 | |
| 920 | // Put all the bodies of the island to sleep |
| 921 | for (uint32 b=0; b < mIslands.nbBodiesInIsland[i]; b++) { |
| 922 | |
| 923 | const Entity bodyEntity = mIslands.bodyEntities[mIslands.startBodyEntitiesIndex[i] + b]; |
| 924 | RigidBody* body = mRigidBodyComponents.getRigidBody(bodyEntity); |
| 925 | body->setIsSleeping(true); |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | // Enable/Disable the sleeping technique. |
nothing calls this directly
no test coverage detected