In what order should I place the following physics simulation methods?

Started by
6 comments, last by Buckeye 14 years, 2 months ago
If I am creating a 2D physics engine, in what order should I place the following methods when updating the physics simulation each frame: - Collision detection - Correct overlapping positions - Apply impulses - Apply forces (gravity, impose drag, etc.) - Integrate velocity - Integrate position Are there any processes I have missed out? Should integration of velocity and position occur at the same time in the same process? Thank you for any help and ideas you may have.
Advertisement
Quote:- Collision detection

Do you collect collision data for all objects in this step (before any of your physics steps)? It's not clear from your description whether you're trying to apply collisions one-at-a-time (not so good) or you're determining all collisions among all objects for the time frame (better).
Quote:- Correct overlapping positions

It depends on what you're actually trying to simulate or how accurately you want to simulate the "real" world, but that step isn't consistent with the rest of your process. Positions, linear velocities and angular velocities should be the result of applied forces. Your simulation may not work well if you manually change positions of colliding objects and then apply forces to them to determine position. You'll get errors in the displacement, etc.

If you're doing one-at-a-time collisions (a single collision between 2 objects), you're almost guaranteed not to get what you expect.
Quote:Are there any processes I have missed out?

Do you want to consider surface friction between objects? If so, do you want to consider angular velocities?
Quote:Should integration of velocity and position occur at the same time in the same process?

Depends on the answers to the above questions. If you collect all collisions for a single object, then you should have all the applied forces and, yes, you can calculate the velocity and (from the velocity) position.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

[Collision detection]

I use a quadtree which detects all collisions between objects in the same node for the time frame.

Each collision is stored as a data stuct in a list. Collision data contains the following:

Contact normal
Contact location
Penetration depth
t0
t1
Body associated with collision

Is there anything else I would require?

[Correct overlapping positions]
If I apply an impulse along the direction of the collision normal will that work better than just correct the positions?

At what stage would friction be applied? Would it not be applied during the Apply impulses stage?

Thanks again for the help.
In your collision structure:
Quote:Body associated with collision

Do you mean "bodies," plural? Or do you store a separate structure for each body?
Quote:If I apply an impulse along the direction of the collision normal will that work better than just correct the positions?

Why "correct" the position at all? The applied forces to each object determine their positions and velocities.

If your concern is that object geometries may "overlap" when rendered that, unfortunately, is a limitation of "rigid body" simulation in discrete time steps. In the real world, objects deform on collision and would "appear" to be inter-penetrating.

Quote:At what stage would friction be applied? Would it not be applied during the Apply impulses stage?

I had assumed your "impulses" stage would be for impulses resulting from object behavior. However, friction can be applied as just another force resulting from the collision. Because it's tangential, it'll result in a torque about the center-of-gravity of each colliding object. That torque can be resolved into a force through the center-of-gravity (parallel with the tangent) and an additional mass-dependent angular velocity.

You don't describe how you simulate different materials or how you determine the forces of the collision - e.g., soft, "bouncy." You might want to add a surface parameters structure to the collision structure so the appropriate forces can be applied to each object in that collision.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Do you mean "bodies," plural? Or do you store a separate structure for each body?


Apologies, I did mean bodies plural. The 2 bodies involved in the collision pair are stored.

Quote:
I had assumed your "impulses" stage would be for impulses resulting from object behavior.


Can you explain, I'm not sure I understand?

Quote:
However, friction can be applied as just another force resulting from the collision. Because it's tangential, it'll result in a torque about the center-of-gravity of each colliding object. That torque can be resolved into a force through the center-of-gravity (parallel with the tangent) and an additional mass-dependent angular velocity.


So I can apply friction (as an impulse?) when calculating and applying impulses?

The update process will now look like the following:

- Collision detection
- Apply impulses (resolves collisions)
- Apply forces (gravity, drag etc.)
- Integrate

Is this looking sensible?

Thanks Buckeye.
Quote:- Collision detection
- Apply impulses (resolves collisions)
- Apply forces (gravity, drag etc.)
- Integrate

Looks like you're getting close. Not sure what you mean by "resolves collision," though, or, actually, what "impulses" you may be considering.

A collision will result in forces (including friction, gravity, drag, etc.) being applied to a body. If there's another collision in the same time frame, that second collision will apply additional forces to the body.

When all forces for all collisions in a single timeframe for a single body have been determined, then add up the forces and go on to your velocity, angular velocity and position calcs.

More like:
- Collision detection
For each body:
- for each collision, calculate forces.
- sum the forces and angular velocities, etc.
- calculate velocity and (from the velocity) position

EDIT: Ah. Maybe "resolves collision" is just the calc of all the collision related forces so you can apply gravity, drag, etc., separately?

If so, then, yes, friction would be a per-collision force, and gravity, drag, etc. would be applied per-object - drag dependent on the final velocity/angular-velocity.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:
EDIT: Ah. Maybe "resolves collision" is just the calc of all the collision related forces so you can apply gravity, drag, etc., separately?

If so, then, yes, friction would be a per-collision force.


That's correct. So friction can be applied to all colliding objects as an impulse?
You might want to be careful about the term "impulse." The forces I'm talking about are not delta-time-dependent. They're instantaneous (along with the acceleration and angular acceleration.) The velocity, angular velocity and position calcs would be delta-time-dependent, but not the calculated forces, torques, etc.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement