Results 1 to 7 of 7

Thread: How physics engines calculate physics ?

  1. #1

    How physics engines calculate physics ?

    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:

    [pascal]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;[/pascal]

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

    [pascal] // 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;[/pascal]

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

  2. #2

    How physics engines calculate physics ?

    Study these articles:

    http://www.gaffer.org/game-physics/
    [size=10px]"In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite." -- Paul Dirac[/size]

  3. #3

    How physics engines calculate physics ?

    Quote Originally Posted by cragwolf
    C++

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

  4. #4

    How physics engines calculate physics ?

    Quote Originally Posted by Zampoteh
    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.

  5. #5

    How physics engines calculate physics ?

    Quote Originally Posted by Robert Kosek
    Quote Originally Posted by Zampoteh
    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.


    Quote Originally Posted by Robert Kosek
    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.

  6. #6

    How physics engines calculate physics ?

    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).
    [size=10px]"In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite." -- Paul Dirac[/size]

  7. #7

    How physics engines calculate physics ?

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •