Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » C# Language » Real Time 3D physics simulation using peace engine

Real Time 3D physics simulation using peace engine

This demo shows how to create physics simulation by using peace engine, a free 3d simulation/rendering engine, Which can use with various computer languages.

Technologies: .NET 1.0/1.1, DirectX,Visual C# .NET
Total downloads : 672
Total page views :  19617
Rating :
 1/5
This article has been rated :  2 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
PErelease01.zip
 
Become a Sponsor


Related EbooksTop Videos

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.

 

 

Reference:

Rigid Body Dynamics
Euler Angles


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Akkaradech Sjn
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
PErelease01.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Powerful ASP.NET Hosting w/ NO Setup Fees. Click Here!
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved