• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Ridgid body simulator

This topic is 6546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I''m having serious problems with my 2d ridgid body simulation. Image a sideways view of a box in contact with the floor by just one of it''s corners, with one side raised up. As gravity pulls it down, it should rotate slightly, move towards the floor and eventually come to rest with it''s bottom parallel to it. Frictional force are not acting. Also, I''m using very low values of e (including 0) so I can see my box sliding along the ground like it''s on ice, like it should, before I add friction. My update loop for the box is as follows: Calculate gravitational acceleration Move the box forward by current time step If the box is colliding with anything Calculate new velocities from collisions Move the box back to where is was before Move the box forward by current time step If the box is colliding with anything now Move the box back to where is was before The problem is that as the box rotates a small angle and moves down, it eventually gets into a state where the velocity of the contact point will *always* collide with the floor. Basically, it just gets stuck and won''t move unless it''s shoved around a bit. I have tried many attempts at changing the update loop but always end up with the same problem. I''m quite sure my new velocities after collision with the floor are correct, because energy is conserved and it looks right in all other cases. The reason for moving it back to where it was before is to avoid interpenetration. I have read I should simulate backwards and forwards through time to find the exact time before the collision, but even if I make the timestep tiny (e.g. 0.0001s) the problem still occurs. If anyone has even the slightess idea what the matter is (bad integrator, bad collision detection, wrong collision velocities, incorrect update loop etc.), please tell me. I have been stuck on this single problem for about two weeks now and I have to get this finished. It''s driving me crazy. Thanks in advance, Sean Wilson

##### Share on other sites
I understand your reason for moving the box back to its initial position after the 2nd collision test -- to avoid interpenetration -- but when you do this you also undo any lateral movement over that period... the box isn''t able to slide until it somehow manages to get airborn for a frame or two; this is not realistic Instead of moving the box back, maybe you should just move it "out" of the ground, i.e. push it up in the direction of the ground normal. Also, perhaps you should try what you mentioned, finding the exact time of collision... then you wouldn''t have to move the box all the way back to its initial position to avoid the interpenetration.

##### Share on other sites
What do you mean by "undo any lateral movement over that period"?

I have tried the "moving it out of the ground" approach, but if there are stacked objects or objects in close proximity, it will cause another collision. What would I do then?

The reason I said the problem still happens even at very small timesteps is because all you are doing when finding the exact time of the collision is using a smaller timestep than you were when the collision happens. I could only conceivabley search for the time of collision within a certain accuracy so this is unlikely to be the problem.

Sean Wilson

##### Share on other sites
quote:

What do you mean by "undo any lateral movement over that period"?

I was referring to this part of your loop:
quote:
If the box is colliding with anything now
Move the box back to where is was before

I'm guessing your box, whenever it tries to "rest" on the ground, ends up hitting this condition every cycle... you're undoing it's movement every frame, so the box can't interpenetrate, but neither can it even slide sideways a little ("lateral movement").

I'm very interested in your problem here, because I'm starting on a rigid body simulator myself. I knew that getting objects to rest on the ground (or on each other) was going to be tricky. I'm thinking that it might be necessary to put objects in a special state for this, rather then just letting them collide every single (or every other) frame.

quote:

I have tried the "moving it out of the ground" approach, but if there are stacked objects or objects in close proximity, it will cause another collision. What would I do then?

Well it's probably not that good of an approach anyway, but here's one idea off the top of my head:

1) Looping thru your objects, you find that obj A and B are interpenetrated...
2) Separate them with the "moving out of the ground" approach, i.e. directly adjust their positions.
3) Add obj A to a linked list within B (call it B's "dependency list"). Also add B to A's dependency list.
4) Now, you continue looping thru your objects, and you find that obj B and C are interpenetrated...
5) Separate B and C with the "moving out of the ground" approach.
6) Add B and C to each other's dependency lists.
7) Traverse B's dependency list, redoing collision detection with the objects in the list. So you'll end up redoing the collision with A and separating them again.
8) Now I'm a little hazy on what to do, but basically you should be able to keep traversing everyone's dependency lists until eventually nobody is interpenetrating. This should completely resolve situations where several objects are stacked atop one another (albeit rather inefficiently).

Edited by - Eric on February 17, 2001 11:58:25 PM

##### Share on other sites
A few tips you may want to remember. My examples and methods are really simple, for relatively simple objects, but it should work for anything, given enough imagination...

1. Objects behave more-or-less from their center-of-mass. Gravity is determined using the distance from the center of mass of two objects.

2. Pull an object down, from it''s center. Check it''s bounding-box corners for collisions. If they are inside an object, rotate those corners (One at a time) "backwards", towards where the object was in the previous "frame", until they are no longer colliding.

3. If an entire face (4 corners) is collided, whether on initial detect, or during the rotation in note #2, then the object should be "bounced" upwards.

4. If that upwards movement is less than it''s "elasticity" rating, then it will come to a rest, with the "collided" face paralell and coplanar to the surface that it hit.

I hope these help!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 31
• 16
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
634116
• Total Posts
3015598
×