PDA

View Full Version : How physics engines calculate physics ?



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

Demo: http://www.yourfilelink.com/get.php?fid=278491
( 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
Pos1 := VectorAdd(AsAffineVector, VectorScale(v1, aDeltaTime) );
SetPoint(Pos1);
end;

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

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

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

Zampoteh
06-02-2007, 12:53 AM
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, 01: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, 01: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, 09: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, 01:09 PM
Then you have a selection of packages:
http://www.hypeskeptic.com/Mattias/DelphiODE/
http://www.newtondynamics.com/downloads.html - Sascha Willems maintains the headers for Delphi.
http://www.partow.net/projects/fastgeo/index.html



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. Assuming someone has the time to put the effort into it, then no it isn't hard. Physics engines are powerful tools and to create or find an opensource one is almost as hard as making it yourself! That's why they sell them, because they make it as optimized as possible and as flexible as possible for your games.

I don't really know much about physics, but all the work I have done with vectors, color transformations, and even a small game have been opensource under the MPL. But I can learn, and it doesn't intimidate me.