SDL-Demos (mostly for developers)

In order to help Delphi and pascal developers getting started with the Newton Game Dynamics Engine I wrote some demos that come with full source code so you can see how different things can be achieved when using Newton. The demos use the Simple Direct Medialayer so that, when using a cross-platform pascal compiler, you can use Newton with all operating systems that are supported by Newton itself (currently Windows, Linux and Mac) . What you need to compile and run them :

  • A compatible pascal compiler, tested with Free Pascal 2.0.0 and Delphi
  • The Newton headers.  Choose the headers depending on which version of the demo you want to compile. The demos that use 2.00 work with the beta headers.
  • Pascal translation of the SDL-headers, called JEDI SDL
  • Newton’s shared library for your operating system, included in the Newton SDK, placed somewhere where the application can find it
  • SDL’s shared library for your operating system, placed somewhere where the application can find it
Newton SDL 01 01 – Basic Demo
This is as basic as it can get. If you want to get into the usage of Newton, take a look at this demo. It shows how to create some spinning boxes and also shows you how correct physics timing is done using “accumulative timeslicing”.

Newton SDL 02 02 – Joints Demo
This demo shows off all different joint types that Newton has to offer : Ball and Socket, Hinge, Slider, Corkscrew and the universal joint. It also shows how to use newton’s raycast-functions to grab and influence bodies. Also shows how to display debug lines for physics.

Newton SDL 03 03 – Vehicle Demo
This demo shows how to use the vehicle container in Newton. It isn’t looking pretty, but the source is fully documented and it should be easy to get vehicles to run in your own applications.

  • Source (Newton 1.53)
  • No Newton 2.00 source yet. Vehicle functions are missing from current NGD 2.xx beta!
Newton SDL 04 04 – Buoyancy Demo
This demonstration shows you how to implement fluids (and similar things) that bodies are immersed in using newton’s buoyancy features.

Newton SDL 05 05 – Ragdoll Demo
This demo shows the usage of NGD’s RagDoll-container to create humanoid ragdolls that bend and twist like real corpses do. The demo let’s you drop them down on an environment and also let’s you grab and throw them around with your mouse.

  • Source (Newton 1.53)
  • No Newton 2.00 source yet. Ragdoll functions are still subject to change in NGD 2.xx beta!
Newton SDL 06 06 – Character Controller
When pepople think of physics engines like NGD, they first think about throwing objects around. But that’s not the only thing such a physics engine can be used for. Modern FPS-games need to have robust collisions with realistic movement and collision reaction. This is usually a hard thing to code, but Newton can easily be used to implement such a character controller. This demo shows how to do this and let’s you run around in a small FPS environment where you can even interact with some dynamic objects.

Newton SDL 07 07 – Simple explosion
This demo shows how to create (simple explosions) by adding impulse to all bodies in an explosions perimeter depending on their distance to the explosions origin.

Newton SDL 08 08 – Contact process callback
Newton’s material system is very sophisticated and offers a lot possibilites. One of these is the contact process callback, a callback called from inside newton when two materials generate contacts. In this callback you can get the force of a contact and use it to apply damage, spawn particles, or play sounds depending on the strength of that contact, which is shown in this demo.

Newton SDL 09 09 – Moving static bodies
Sometimes it may be necessary to move a static body, for example if you have a scripted object that only moves when a certain action is triggered (think of an elevator for example). Just overriding the matrix of this body with animation information won’t work as interaction with rigid bodies won’t be correct. This demo shows how to move static objects the correct way.

37 Comments

  1. Nice work!
    The second example (Joints demo) produces an acces-violation when I press the left mouse button. Both the shipped .exe and .exe I compiled using Delphi 7 give this error. Newest stable versions of all required .dll’s in use (just started with newton 🙂 ).

  2. There seems to be a conflict between the demo and the most recent newton.dll (the one used while writing the demos was an older one) as I also could reproduce this. I’ll see what causes this and update the joints demo.

  3. hey Sascha
    did you write these programs in C++ and make it compatable with many compilers? or did you write them in delphi?

  4. I wrote them with Delphi, so they’re pascal. I also tested them with FPC, so they work with the most widely used pascal compilers. No C/C++ involved in there, everything on this page is either written with Delphi or Free Pascal.

  5. oh and 1 more thing, do you know of any tutorial anywhere that teaches how to do the code in delphi, because im quite fluent in delphi but have no idea what is happening when newtonphysics comes into play. did you teach yourself?

  6. Hi Sascha,

    I took a look at the source of your Joints Demo and found the function (procedure in Delphi 🙂 ) PhysicsApplyPickForce.

    Could you please explain how this proc does what it does?

    Thank you very much,

    Xander

    1. That procedure is nothing special. It applies a (non-limited) spring-force between the attachment point on the body (that’s the point where you grab the body) and the viewer’s position. If you need more information, refer to the newton forum, there are plenty of threads on how to apply spring forces (basically it’s just “Fs = -k*x”).

  7. Hi Sascha,

    i tried to compile your “01 – Basic Demo” but it seems to be in the old Newton version.

    I now use Linux, and have to use Newton2.0

    could you please, update this “demo” so that i can see what is necessary to get things under nweton2.0 done ?

  8. really thanks , this gonna be a great reference , but why i get much fps with ode comaping with newton , while newton looks more optimized than ode .

    keep up the great coding

  9. Hello Sasha,
    I tried to compile “08 – Contact process callback” but I get an error on
    NewtonMaterialGetContactPositionAndNormal(Material, @Pos, @Norm); This function also requires a body…
    I checked my header file and it is: Current SDK version 2.33 (Beta)(Revision #1)

    Any idea?

    1. That’s odd. I just compiled it again myself and it works fine. What IDE/Language are you using? I compiled it with FPC (Lazarus) and Delphi, both under Win32.

      1. I work under Delphi 2006…

        The line in NewtomImport.pas states:
        procedure NewtonMaterialGetContactPositionAndNormal(const Material : PNewtonMaterial; const Body : PNewtonBody; const Posit, Normal : PFloat); cdecl; external{$IFDEF __GPC__}name ‘NewtonMaterialGetContactPositionAndNormal'{$ELSE}NewtonDLL{$ENDIF __GPC__};

        When I edit this line, remove the body part, the code compiles and runs, but…. when the first cube hits the floor the background becomes black (mostly) and only one cube is visible. Audio continues.

        1. I noticed that FPC/Laz did not recompile the needed .o file for the latest headers, and in 2.33 that function now needs an additonal body passed as a parameter. So to fix this simply replace the troubling line with this :

          NewtonMaterialGetContactPositionAndNormal(Material, Box.NewtonBody, @Pos, @Norm);

          This should compile and work.

  10. hi sir sascha Willems
    thanks for the Header file and good demos
    please create Vehicle demo for newton 2.33
    i create a car with newton 2.33 but i cant control the car
    i cant pull car
    i pull the body with the NewtonBodyAddForce command but the car fly ???

  11. hi sir sascha Willems
    thanks for the Header file and good demos
    please create Vehicle demo for newton 2.33
    i create a car with newton 2.33 but i cant control the car
    i cant pull car
    i pull the body with the NewtonBodyAddForce command but the car fly ???
    please help me
    thanks again for help
    bye

    1. What car do you mean? The raycast one (which is a custom joint) or the built-in rigid-body vehicles? If you’re talking about the later one (which I guess, as the raycast-one is not available for Delphi) then you’re out of luck. The built-in rigid-body vehicle isnt’ fully implemented in Newton 2.33 yet, so acutally I can’t create a demo for it until Julio has released a Newton Version where the vehicle implementation is finished and working.

  12. Hi! I’m from Ukraine and I know English bad. I tried to compile ’02 – Joints Demo’ but the compiler generates an error:’Access violation at address 00000000. Read of address 00000000.’. What should I do?

    1. An AV at address 0x0000000 means OpenGL isn’t initialized / couldn’t be loaded and that rendering isn’t working. Install graphics cards drivers that support OpenGL. If that won’t help, check if OpenGL was initialized successfully by checking if OpenGL function pointers are not NIL.

  13. So, problem in rendering objects? ok, i check it.
    And one more question, you write in this project ‘pEvent.button = SDL_BUTTON_WHEELUP’, but compiler say:’Undeclared identifier SDL_BUTTON_WHEELUP’. When I can download right SDL pascal headers? I think problem in this.

  14. I checked programm and error in file ‘glPrimitives.pas’ in line №564:
    ‘Q:=gluNewQuadric;’
    What should I do? Please, help me!!!!!

    1. What error does your compiler show you in that line? For Delphi thats correct syntax, and if you use FPC you may have to enable Delphi compatibility mode via a compiler switch.

        1. function ScreenToWorld(ScreenX, ScreenY, ScreenZ : Single) : TVector3f;
          var
          Viewport : TViewPortArray;
          MVMat : T16dArray;
          PJMat : T16dArray;
          WinX : Double;
          WinY : Double;
          WinZ : Double;
          ObjX : Double;
          ObjY : Double;
          ObjZ : Double;
          begin
          // Retrieves the viewport
          glGetIntegerv(GL_VIEWPORT, @Viewport);
          // Retrieve the matrices
          glGetDoublev(GL_MODELVIEW_MATRIX, @MVMat[0]);
          glGetDoublev(GL_PROJECTION_MATRIX, @PJMat[0]);
          WinX := ScreenX;
          WinY := ScreenY;
          WinZ := ScreenZ;
          WinY := Viewport[3] – WinY;
          // Unproject screen coordinates
          gluUnProject(WinX,WinY,WinZ, MVMat, PJMat, Viewport, @objx, @objy, @objz);
          Result := AffineVectorMake(ObjX, ObjY, ObjZ);
          end;

          1. That’s just a standard GLU unprojection that transfors screen coordinates into OpenGL world coordinates using the currently set matrix.

  15. I am working in Delphi+GLScene and I don’t know how to convert Scren coordinates to World. I want to understand the principle of this function.

    1. It’s pretty simple. You just get your current matrices (glGetDoublev), and pass them to OpenGL’s GLU function gluUnproject. This function does all the work of converting your screen coordiantes to world coordinates. And dont’ forget that OpenGLs origin is in the lower left corner, contrary to the windows’ one in the upper left corner, so before you pass your screen coordinates (WinX, WinZ, WinY) to gluUnproject you need to adjust WinY to start from the bottom of the current viewport. After gluUnproject, ObjX, ObjY and ObjZ will contain your world coordiantes within your currently set matrices.

  16. you can make the movement much smoother

    const
    CONST_FRAME_INTERVAL = 0.016666;
    var
    // Physics timing
    g_performanceFrequency : int64;
    g_startTime : int64;
    g_endTime : int64;
    g_fFrameTime : single;
    g_fTimeSinceLastUpdate : single;

    // init newton
    QueryPerformanceFrequency( g_performanceFrequency );
    QueryPerformanceCounter( g_startTime );

    //—–Update physics
    QueryPerformanceCounter( g_endTime );
    g_fFrameTime := (g_endTime – g_startTime ) / g_performanceFrequency;
    g_fTimeSinceLastUpdate := g_fTimeSinceLastUpdate + g_fFrameTime;
    // If the time is greater than 1/60th of a second update the physics
    if g_fTimeSinceLastUpdate > CONST_FRAME_INTERVAL then
    begin
    NewtonUpdate( NewtonWorld,CONST_FRAME_INTERVAL);
    g_fTimeSinceLastUpdate := g_fTimeSinceLastUpdate – CONST_FRAME_INTERVAL;
    end;
    QueryPerformanceCounter( g_startTime );

  17. Downloads not working?

    Warning: mysql_real_escape_string(): Access denied for user ”@’localhost’ (using password: NO)

    Please fix when you get a chance.

Leave a Reply

Your email address will not be published. Required fields are marked *