Using the Newton Physics Engine in GLScene

See the first page in this section for the set-up of GLScene in Lazarus. There are five Newton samples, with useful notes, completed in 2011 by Franck Papouin. In order to view the form of each program in Lazarus, you need to install the package GLSceneLCL_NGD (which you have already compiled).
  1. Copy newton.dll and dJointLibrary.dll from GLSceneLCL\external folder to the directory containing lazarus.exe.
  2. Select menu item Package > Open Package File (.lpk)... and then select GLSceneLCL_NGD.
  3. Select menu item Use >> > Install and accept both invitations to continue.

These points apply to all five Newton samples.

  1. Use a recent 32-bit version of Lazarus.
  2. Copy newton.dll and dJointLibrary.dll from GLSceneLCL\external to the project folder.
  3. View the .lpi file of the sample and remove any spaces before the names of the required packages. (You must be able to see GLSceneLCL_RunTime as a required package in the Property Inspector before compiling). Change any of the remaining old names such as GLS_NGD to the corresponding new names such as GLSceneLCL_NGD.
  4. Accept any invitations to continue when loading.
  5. When executing the application, you may receive an error message that WGL_ARB_pbuffer support is required. Check the box with caption Ignore this exception type and press the button to continue.

NewtonDensity

This example illustrates the density and viscosity of a fluid.

We changed line 132 to PlaneEquation.Z := MyForm.GLPlane1.Position.Y;.

Select GLLeadSphere in the Object Inspector and click on the Material ellipsis to see that the colour is brown. Click on the Behaviours ellipsis and select the only item (0 - NGD Dynamic) in the pop-up editor. Now you can see in the Object Inspector that the density is 10. Similarly, you can see that the paper sphere has colour grey and density 0.1.

When you execute the program you can change the density, linear viscosity and angular viscosity of the fluid. Increase the "Water Density" to the maximum (20) to see the lead sphere rise to the surface and to see the brown colour above the fluid surface. You can also observe a cone, capsule, cube and cylinder as you change the properties of the fluid.

NewtonJoints

This is an impressive demonstration of several labelled types of joint that you can enjoy testing with the mouse in the executed program. The types of joint are ball, ball and socket, corkscrew, hinge, slider, and universal. Some have limits applied. Use the Object Inspector to see the value of its JointType property (e.g. nj_CustomSlider).

Deduce from the key combination settings that pressing left and right buttons of the mouse will enable zoom.

NewtonMaterial

You should be able to use this sample as a basis from which to develop your collisions and sliding motions between pairs of objects in your own programs. (This would be very difficult without a working example!) See from the Object Inspector that for each surface pair you can set up properties such as elasticity, softness, and both kinetic and static friction. There is plenty of scope for experimentation!

NewtonSpawn

Repeatedly add spheres, cones, capsules, cubes and cylinders to see them fall and interact with the other objects as shown in this screenshot.

Screenshot of NewtonSpawn in action

Screenshot of NewtonSpawn in action

It is not difficult to spawn an object and assign a colour e.g.
GLCone1 := TGLCone.CreateAsChild(GLDummyCube1);
GLCone1.Material.FrontProperties.Diffuse.RandomColor;    

NewtonWalkCarryShoot

We needed to make the following changes to make this example work for us.

  1. In NewtonWalkCarryShoot.lpi we removed a space before the GLSceneLCL_RunTime package and another space before this GLApplicationFileIO.pas. We updated the name of packageGLS_NGD to GLSceneLCL_NGD.
  2. We copied ngdchair.3ds, mushroom.3ds and ngdmap1.3ds from media to the project directory.
  3. We changed line 209 to Ball.Translate(delta.X, delta.Y, delta.Z);
  4. We changed line 302 to f.Y := 0; // Do not allow the body to go up or down.

Read the initial comment in Unit1 to see the inputs that are expected. Pressing X several times then arranging the view gave us this screenshot.

Screenshot of NewtonWalkCarryShoot in action

Screenshot of NewtonWalkCarryShoot in action

Programming - a skill for life!

How learn from the GLScene Samples