Demo and Benchmark Documentation Engines
Demo and Benchmark Documentation Engines
» Introduction » Install Instructions » Developer Documentation » Simple Example »»A simple PAL example. »»»Introduction »»»Setting up the (MSVC 8.0) workspace »»»A simple example » Graphics Engine Integration Examples » PAL Developer Documentation » PAL Publications


Adding physics to your application with PAL is a simple process. The first thing to do is to set up a simple example using PAL physics as a test platform. From there it is advised to look in to a more complex example demonstrating how to interconnect the physics system with a graphics engine.
In this example we will work through the steps involved in setting up a PAL project for Visual Studio and dropping a box on to the ground.

^ top

Setting up the (MSVC 8.0) workspace

To begin, create a new project (select Visual C++, Win32, "Win32 Console Application", and provide a name). Make sure it is an empty project with no additional dependencies (On the "Application Settings" tab, select "Empty Project"). Next, add a source file to your project (Add, New Item, C++ file (.cpp), and provide a name).

Now we need to set the correct compiler settings to make sure we can compile the PAL code. Set the build to release (Build, Configuration Manager, Active Solution Configuration), and select the project properties and make sure the compiler is generating DLL compatible code (eg:Multi-threaded DLL (/MD)). Under the general tab, we need to set up the include directories. In the "Additional Include Directories" section, enter the location where the PAL library include files are stored (eg: C:\lib\PAL\pal). Now select the Linkers general settings and under "Additional Library Directories", enter the location where the PAL library files are stored (eg: C:\lib\PAL\lib). Finally, select the libraries input tab and add "libpal.lib" as an "Additional Dependencies".

Now the workspace is configured and we are ready to write some physics code!

^ top

A simple example

The code for our program is given in the listing below.

The first step is to include the PAL physics library via the "palFactory.h" file. Then the appropriate engine should be loaded and selected. The PF->LoadPALfromDLL(); call loads all the DLL's in the current directory to search for PAL support. The PF->SelectEngine call selects the desired physics engine for running the simulation. At this point we have completed the initialization of PAL.

The next step is to initialize the physics engine. This is done by creating a palPhysics class, and calling the initialization method with the desired values for gravity.
palPhysics *pp = PF->CreatePhysics();pp->Init(0,-9.8f,0);.

Now we would like to add a terrain for our object to drop on to. We will use a simple plane, referenced by the PAL class palTerrainPlane.
palTerrainPlane *pt= PF->CreateTerrainPlane();pt->Init(0,0,0,50.0f);
The plane is initialized at the origin (0,0,0) and given a minimum size of 50x50 units.

Finally we would like to add a box in to our scene that can drop down on to the plane. palBox *pb = PF->CreateBox(); pb->Init(0,5,0, 1,1,1, 1); This creates a box at the position (0,5,0), five units above the location of the terrain, and sets it to be 1x1x1 units large, with a mass of 1. We now have a complete physics scene consiting of a flat plane and a 1x1x1 cube that will drop on to the plane.

To 'run' the physics, we call the update function (pp->Update(0.02f);). The position of the cube is then printed to the display. Finally the physics system is cleared from memory with the PF->Cleanup(); call.

#include <stdio.h> //for our old friend, the printf function
#include "palFactory.h" //PAL physics

int main(int argc, char *argv[]) {

	PF->SelectEngine("Bullet"); //"Bullet" is the name of the engine you wish to use. eg:"Bullet"
	palPhysics *pp = PF->CreatePhysics(); //create the main physics class
	if (pp == NULL) {
		printf("Failed to create the physics engine. Check to see if you spelt the engine name correctly, and that the engine DLL is in the right location");
		return 0;
	pp->Init(0,-9.8f,0); //initialize it, set the main gravity vector
	palTerrainPlane *pt= PF->CreateTerrainPlane(); //create the ground
	pt->Init(0,0,0,50.0f); //initialize it, set its location to 0,0,0 and minimum size to 50
	palBox *pb = PF->CreateBox(); //create a box
	pb->Init(0,5,0, 1,1,1, 1); //initialize it, set its location to 0,5,0 (five units up in the air), set dimensions to 1x1x1 and its mass to 1
	for (int i=0;i<100;i++) { //run 100 steps of the simulation
		pp->Update(0.02f); //update the physics engine. advance the simulation time by 0.02

		palVector3 pos;
		pb->GetPosition(pos); //get the location of the box

		printf("Current box position is %6.5f at time %4.2f\n",pos.y,pp->GetTime());
	PF->Cleanup(); //we are done with the physics. clean up.

Before running the program, we need to copy the appropriate physics library DLL to our projects directory (ie: "libpal_bullet.dll"). Otherwise the bullet physics library will not be loaded and our program will fail to create the physics system.