I am trying to simulate the behaviour of a rigid body, for example a cube, while it settles on a flat surface. Let's say it lends on an edge or even on a corner, and now it has to settle and remain on the surface. I am modeling this by rotating the body around contact line or a contact point, but I have a problem when the body needs to stop moving.

At the moment, the effect I got is that when it lands on the surface, it is still not perfectly aligned with it, so in my collision detection I am detecting that I still have just one contact point, so I am trying to generate rotations around this contact point.

After this, another edge or corner becomes the contact point, so it starts to rotate around that one and soon things become frantic; the cube is dancing wildly and not settling.

To explain a bit more, every frame I am collecting data about which of the corners is closest to the surface of interest and storing this data in a C++ type, std::vector. If I have a result that all four points(corners) are at the same distance, I put them all inside a vector. After the collision detection method is called inside my physics engine code, I try to resolve it, and I do this by returning the data about collision points and corners etc.

Any advice on how to approach this particular problem?


1 Answer 1


My guess is that this could be an issue of numerical accuracy that could be solved with generous rounding. Numerically you will never reach a point where multiple corners of the cube are exactly the same distance from the surface so you the cube will never reach a rest position. An easy fix would be to define all corners that are less then some suitable small epsilon away from the surface are treated as on the surface. You probably need to experiment a bit to find a suitable epsilon that produces good visual effects.

  • \$\begingroup\$ Thanks for the answer, I guess that is one option. My idea was to just rotate the cube somehow to align it with the surface, for example get the surface normal and cube normal and do a rotation of cube. \$\endgroup\$ Feb 9 at 19:06
  • \$\begingroup\$ @ŽarkoTomičić Yes, I think you should continue doing that, my point was more that the aim should not be a perfect match between the surface normal and the cube normal (which will never happen numerically) but rather something sufficiently close to it. \$\endgroup\$
    – quarague
    Feb 9 at 19:18
  • \$\begingroup\$ Well, when I apply same rotation transformation to a cube as I do to a flat surface I get all points, that is why I was thinking in that direction, but the problem is you just can not do it in all situations. \$\endgroup\$ Feb 9 at 19:47
  • \$\begingroup\$ And one more thing, I was thinking, is it done this way usually? \$\endgroup\$ Feb 9 at 19:49
  • \$\begingroup\$ @ŽarkoTomičić First from abstract math, if one corner of a cube touches a surface there always exists a rotation that will keep the corner on the surface and move the cube to stand flat on the surface. Takes some vector calculus to find it. Second, if some complex computation gives an exact solution abstractly for a numerical solution you almost always have to do some rounding and work with a sufficiently good approximation. \$\endgroup\$
    – quarague
    Feb 10 at 7:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .