Tuesday, September 11, 2012

3d graphics programming - adding some physics

Now that we got the basics of 3d game programming using Irrlicht out of the way, how about adding some physics? I'll demonstrate how physics could be added to a 3d application using the open source Tokamak Physics library. In order to better illustrate the source code, I've uploaded it to GitHub. It is accessible via the 3dGraphicsExamples repository.
Update the Qt project file to include Tokamak library:
In order to use Tokamak in C++ code, you'll need to include its header file:
Before Tokamak can take over as the physics engine behind your application, you need to provide it with some details about the simulation as well as information about your 3d models. In Tokamak, 3d objects that are supposed to be mobile are called rigid bodies, while bodies that are supposed to stay in their position are called animated bodies. In order for Tokamak to run, it needs to know how many objects are needed to be tracked in the simulation as well as information about gravity.
In our simulation, we'll start with poker cards initially suspended in air at varying distances from the a point and all cards facing this point. These cards will fall on a inanimate floor. Create the floor in Irrlicht and record it's attributes in Tokamak:
Create cards using the routine from the previous example however this time also set their physical attributes in Tokamak:
In order to run the simulation, Tokamak requires you to provide an advance interval. In order to get a consistent feel, you'll need to use a timer object and keep track of elapsed time interval. Luckily, Irrlicht provides you with a timer object as well:
You'll also need to ensure that your simulation runs consistently regardless of the amount of processing that goes on in your render loop. The below code is adaptation of code used by Adam Dawes on his site:
In your render loop, you'll have to traverse through your catalog of 3d objects in Tokamak and apply the position and rotation to the corresponding Irrlicht scene nodes.
There you go! You should now see your 3d objects, all initially facing in different directions, falling from on the floor, colliding with each other and bouncing. The complete source file can be found on github: example2.h

2 comments:

  1. Hello,

    Someone in my Facebook group shared this site so I came to check it out. I'm definitely loving the information.

    Randy
    architectural drawings

    ReplyDelete