PDA

View Full Version : How physics engines calculate physics ?

Zampoteh
05-02-2007, 12:22 AM
How physics engines calculate physics ?

( Use "http://www.yourfilelink.com/check.js" for AbBlock. )
Sources included.
Delphi + GLScene

It works with static boxes and dynamic spheres.

Physics calculation loop consist of two parts.
1) Detect collisions, and accumulate forces for dynamic objects.
2) Add accumulated force to dynamic objects, and calculate new acceleration, velocity, position and etc for them.

1) How to add force for collided objects.
I calculate a direction of a dynamic object movement. If it moves out of static body, i do nothing. If it moves inside static body, i add force:

PsObj[phObjInd].Force := VectorAdd( PsObj[phObjInd].Force,
VectorScale(intNorm,
PsObj[phObjInd].Mass *Depth1 / aDeltaTime
) );
Force := Force +Normal of collision * mass of object * penetration depth * time;

2) How to add accumulated force to a dynamic object.

// Add gravity to Force accumulator.
// Force1 := Force1 +Gravity.
Force1 := VectorAdd(PsObj[psInd].Force, Gravity);
PsObj[psInd].Force := Force1;

// Acceleration. Get Acceleration from Force and mass.
// a1 := F / Mass; F := m*a1;
a1 := VectorScale(Force1, 1/PsObj[psInd].Mass);

// Velocity. Get Velocity from acceleration and time.
// dV := a1 *t1 a = dv/dt t1 = dt = deltaTime
dV := VectorScale(a1, aDeltaTime);

// Add new velocity to old.
// v := v0 +dV;
v1 := VectorAdd(PsObj[psInd].Speed, dV);
PsObj[psInd].Speed := v1;

// Change position. Add to position s := V1*t.
// Pos1 := Pos1 +V1*t;
with PsObj[psInd].GLSphere.Position do begin
SetPoint(Pos1);
end;

I know that it works bad. How to improve? And how to add nice rotations?

cragwolf
05-02-2007, 05:51 AM
Study these articles:

http://www.gaffer.org/game-physics/

Zampoteh
05-02-2007, 11:53 PM
Study these articles:

http://www.gaffer.org/game-physics/
C++

I don't want to study !
I want the solution ! :thumbup:

Robert Kosek
06-02-2007, 12:07 AM
I don't want to study !
I want the solution ! :thumbup:Then you will never learn, nor get the solution. No one will do your work for you. Ignorance is the problem, not translating C++ to Pascal.

That is a very good resource Cragwolf, I've bookmarked that one. :)

Zampoteh
06-02-2007, 12:30 AM
I don't want to study !
I want the solution ! :thumbup:Then you will never learn, nor get the solution. No one will do your work for you.

Not for me, but for us. For all. Because it is opensource.

I did what i know.

Of course i can read many clever books and many codes and do more. But if you ALREADY know how to improve it, why not to improve it? Is it so hard? To write some formulas.

Ignorance is the problem, not translating C++ to Pascal.

That is a very good resource Cragwolf, I've bookmarked that one. :)
By the way, how many pascal physics engines do you know?
I know 0.

cragwolf
06-02-2007, 08:42 AM
The articles themselves use only C code, not C++ code. And it's very simple C, just a few structures, functions, and simple mathematical operations. It's almost trivial to translate it to Pascal. If I can do it, so can you (or any Pascal programmer for that matter, given that my programming skills are at the wrong end of the bell curve).

Robert Kosek
06-02-2007, 12:09 PM
Then you have a selection of packages:
http://www.hypeskeptic.com/Mattias/DelphiODE/