I have a loop, which updates all the physics, inside the loop I'm updating the position of the objects, first thing is the gravity, which pulls all the objects to the bottom. Now my question is, how do I manage the sleep/awake state of the object, let's say the object reached some static body, like ground. So it won't continue falling down because of the gravitation pull?
-
\$\begingroup\$ You could use a boolean flag on your objects, and just skip over them if they're supposed to be sleeping \$\endgroup\$– ThorinIIOct 21, 2013 at 9:16
-
\$\begingroup\$ I was thinking about it, though if an object is sleeping, it will react differently to other objects. Let's say the object is on some surface sleeping, and another box is falling on it. the sleeping object should awake and react to the collision and remain above the surface. A more precise question is, how to maintain the object above surface and prohibit penetration of the surface? \$\endgroup\$– AngryChickenOct 21, 2013 at 9:23
-
\$\begingroup\$ I suppose then, you'll need a collision response system \$\endgroup\$– ThorinIIOct 21, 2013 at 9:27
-
\$\begingroup\$ The ground will have to exert a force/impulse on the objects to stop them from penetrating it - and that should naturally extend to object-object collisions \$\endgroup\$– ThorinIIOct 21, 2013 at 9:28
-
2\$\begingroup\$ You've asked the wrong question. First you must learn about collision resolution, and then you can learn about sleeping. \$\endgroup\$– RandyGaulOct 21, 2013 at 9:28
1 Answer
You need collision resolution to handle object-ground collisions.
A basic example: check if the object is below/colliding with the floor, move it into a position where it does not collide, and set its velocity to make it either sit there, or bounce off (depending on how fast the object is moving downwards).
If you need more accurate physics Google will help you.
Sleeping is an optimisation for the engine such that it does not have to simulate the objects that are not doing anything interesting.
-
\$\begingroup\$ Thanks mate for your answer, I am going to check the proposed approach \$\endgroup\$ Oct 21, 2013 at 9:40
-
\$\begingroup\$ That's fine - do you have the link to Randy's tut? \$\endgroup\$– ThorinIIOct 21, 2013 at 9:41
-
1\$\begingroup\$ @ThorinII gamedev.tutsplus.com/tutorials/implementation/… \$\endgroup\$ Oct 21, 2013 at 9:47