Pages

Subscribe:

Ads 468x60px

Monday, 12 September 2011

Real Time 3D Game


Introduction


The idea for using 3D or Physics engine is to reduce time and line of code. So you can write your application without implementing the low level stuff yourselves. You can write an application faster, because you can focus on the things specific to your application, rather that every tiny detail about how it's rendered, the physics.

PeaceEngine is written in c++ to optimize speed and performance. Has build-in physics(at development stage) integrated 3D graphics with hardware accelerator using opengl. PeaceEngine has standard API for using with various computer programming languages, currently support PYTHON, VB.NET and C#

This example show how to create 3D physics simulation. and how easy of using PeaceEngine.
We will create a little dominoes tower. and hit it with bullet.

To see how it looks like go to http://youtube.com/watch?v=S3s6CEKxDrM


 


Code

static int counter; //We will use it to count rendered frame for adding some liner interactive<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
static int objcount; // this will counter how much object we created so we can tell engine which the last object we just create
static void drop_object() //this function for droping object to user scene current we use it to shoot the bullet
{    
    Random Random = new Random(); //create new instance of ramdom class use for generate randomize number
    int x; //this is optional this variable for receive value returning from engine it useful for debuging
    x = peSetObjPos(10.1f, -6.0f, 10.5f); //tell engine which position we want to create object in word space at X,Y,Z axis
    x = peSetObjRot(Random.Next(), Random.Next(), Random.Next(), Random.Next() * 360); //tell engine how much angle of object we want to rotate before create it at X,Y,Z and how much angle value as radiant
    x = peSetObjSize(0.5f, 0.5f, 0.5f); // how big of object relate to X,Y,Z axis
    x = peSetDensity(2.0f); //is it heavy? o.5 is default value 2.0(bullet) mean 4 time heavier than 0.5(domino) if the volume is equal
    x = peCreateObjEx(1); // now we create object here we tell engine what object type(shape) we want to create 0=box, 1 = sphere, 2 = capsule
    x = peAddForce(objcount, -100.0f, 0.1f, -100.0f); // tell engine that we want to add force to which object in this case we add to bullet(lastly added) and vector X,Y,Z of force we want object to move(in which direction) more force cause more directional velocity to the subject. 
    objcount +=  1; //we just created it so we will add 1 to our object counter
}

static void CreateTower() //Create domino tower. this function will call when initialize our scene
{
    int x;  //this is optional this variable for receive value returning from engine it useful for debuging. same as above
    int a ; // collumn of tower counter
    int k,j,i; // row of tower counters
    a = 0; //initialize it
    for (k=1;k<=7+1;k++)//        we want to create 7 row of domino tower. this for "|" shape of dominos
    {
        for (i=1;i<=15+1;i++)//       we want to create 15 column of domino tower
        {
            if (i == 15 + 1 - k * 1) // the higher of row have less column
            {
                break;        //Exit For
             }//  End If
             if (i <= a)  // the higher of row have less column
             {}
             else
             {
                 x = peSetObjPos(((i * 0.5f) - 4.9f), -6f, (k * (0.75f)) - 0.45f); //the same as above. we want to create domino and set position related to i,k values. Y axis will be static so just leave it -6.0
                 x = peSetObjRot(0, 0, 0, 0);
                 x = peSetObjSize(0.15f, 0.5f, 0.5f);
                 x = peCreateObjEx(0);
                 objcount += 1;
             }    //End If
         } //Next i
         for (j=1;j<=15+1;j++)//       we want to create 15 column of domino tower. this for "--" shape of domino
         {
             if (j == 14+1-k*1) // the higher of row have less column
             {
                 break;
             }
             if (j<=a)
             {}
             else
             {
                 x = peSetObjPos(((j * 0.5f) - 4.65f), -6f, (k * (0.75f)) + 0.004f); //same as above
                 x = peSetObjRot(0, 0, 0, 0);
                 x = peSetObjSize(0.5f, 0.5f, 0.12f);
                 x = peCreateObjEx(0);
                 objcount += 1;
             }
         }//Next j
         a ++; // add value to our counter
     }//Next k}

static
 void stepSim(int n)//use for step our simulation ahead
{
    for(int i =0;i<= n-1; i++)
    {
        peSimulationStep(0); // if you want to pause simulation send 1 here.
    }
}
           
static void MainLoop() // this function use for check frame progress and what we want to do
{
    counter ++;// adjust frame counter
    if (counter == 155)  // at 155th frame we will drop the bullet!!
    {
        drop_object();
    }
    if (objcount == 20000)  // at 20000th frame we will end our physics lession!!
    {
        peShutdown(); // the engine to terminate and good bye.
    }
}

[STAThread]
static void Main(string[] args)
{
    InitPeaceEngine(); //initialize engine
    peInitPhysics(); //initialize physics engine
    peCreateGround(); //need to create ground to prevent our object keep falling
    //peSetCameraPos(0.0f, 10.0f, 2.0f); //set cameraposition in word space at X,Y,Z axis
    peSetCameraLookAt(0, 0, 1.0f); // tell camera which point  in word space as X,Y,Z axis  to look at
    CreateTower(); //create domino tower
    while(! peIsShutdown()) //loop until we get shut down message from engine
    {
        peBeginScene(); // tell engine that we want to begin scene. it need to call before draw something
        peDrawGround(); //draw ground

        MainLoop(); //call main loop
        peDrawObjs(); // tell engine we want to draw all objects that we just created

        peRender(); // tell engine to render (end scene)
        stepSim(2);  //call stepSim function more step more slow but more accuracy of simulation
    }
    peShutdown(); // the engine to terminate and good bye.
}
What happening is after we initialize engine by calling InitPeaceEngine(), the dominoes tower is create immediately as we call CreateTower(). Then the program go to the main loop which we can add interactive here. We don't need to put CreateTower() in main loop because we need only 1 time for creating it.

The frame pass and the counter tick. We waiting for 155 frame (~2 second on 75fps ). Then drop_object() is called. and this trigger shoot the bullet at our dominoes tower.


0 comments:

Post a Comment