X

EZPhysics

API Documentation

Introduction

This basic introduction will help you use the EZPhysics API in your application. You will be able to read an EZPhysics XML file that was saved by the EZPhysics Editor, display scenes, run simulation steps, and manipulate the ODE entities.

Using the EZPhysics API means you can create your own scenes using Ogre and physics-enabled objects to your scenes.

It is assumed that you have a basic familiarity with Ogre and ODE and that you have already played with EZPhysicsEditor and seen the demo EZPhysicsDemo1.

Unzipping the package and compiling

Download the latest EZPhysics API package here. Unzip the two directories called EZPhysicsLib and EZPhysicsDemo1, EZPhysicsDemo2.

It is advisable to have ODE and Ogre in the same directory as the two above in addition to the EZPhysics binaries if you have downloaded them. If you haven't already, please download and have fun compiling the latest versions of ODE. Please download the latest version of the pre-cooked Ogre SDK.

There is one DLL called OgrePlatform.dll found in Ogre SDK package that does not conform with the way EZPhysics works. This is because it makes the mouse invisible and does not allow it’s events to be captured by other windows when the mouse leaves the window. To solve this, copy OgrePlatform.dll from the Ezphysics Editor package to the directory where you plan to have your project executables and dlls. See the note at the end of this page for more information about OgrePlatform.dll.

 It should be replaced

You can create documentation from the source code and comments using doxygen. The comments in the code will improve in the next releases.. :)

Compile the EZPhysicsLib using Visual Studio .Net or 2003. The file EZPhysicsLib.vcproj contains directory names that assume the directory hierarchy mentioned.

Using EZPhysics API

We will go through the source code of EZPhysicsDemo1.cpp

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The demo is based on Ogre’s ExampleApplication base which should be familiar to anyone using Ogre.

#include "EZPhysics.h"
#include "Ogre.h"
#include "ExampleApplication.h"

using namespace Ogre;
using namespace EZPhysics;

The ordered list below is used by the API to map the names of the proxy mesh objects. These names are defined using the editor under the Graphics node.

static const EZstring s_Helpers[EZHELPERS] = {                          "Axis1",
                  "Axis2",
                  "Axis3",
                  "JointSlider",
                  "Sphere",
                  "Box",
                  "Bone",
                  "Focal",
                  "BoundingBox",
                  "Cylinder" };

EZOgre* s_EZOgre;
EZBaseNode* s_EZNode;
EZTree* s_EZTree;

// Event handler to animate
class EZPhysicsDemo1FrameListener : public ExampleFrameListener
{
public:
    EZPhysicsDemo1FrameListener(RenderWindow* win, Camera* cam) : ExampleFrameListener(win, cam)
    {
    }

frameStarted() is called for every frame by Ogre and should contain code that redisplays the window. The first line calls an EZPhysics singleton class called EZInput that looks for Controler nodes within the scene hierarchy and applies the appropriate torques and forces to joints if there’s any appropriate keyboard input. Next we call ODE to perform a step with fixed time-delta. SceneRefresh() redisplay the hierarchy. s_EZNode is passed as parameter in all these cases. It has been set beforehand and points to a valid node within the scene.

    bool frameStarted(const FrameEvent& evt)
    {
         EZInput::getSingleton().Apply(s_EZNode);
         s_EZNode->GetScene()->ODE_Step(0.07);           s_EZOgre->SceneRefresh(s_EZNode, true);
                  // Call default
        return ExampleFrameListener::frameStarted(evt);
    }
};

class EZPhysicsDemo1 : public ExampleApplication
{
public:
    EZPhysicsDemo1() {}
protected:
    // override the mandatory create scene method
    void createScene(void)
    {

We create a scene that is similar to that seen in the Editor’s Ogre window. Please refer to Ogre’s documentation.

         mSceneMgr->setSkyDome(true,"Sim/CloudySky");         Ogre::Plane plane;
         plane.d = 5000; // 5000 world units from the camera
         plane.normal = Ogre::Vector3::UNIT_Y;// Below the camera, facing up
         mSceneMgr->setSkyPlane(true, plane, "Sim/CloudySky",100,3);
         mSceneMgr->setAmbientLight(Ogre::ColourValue(0.8, 0.8, 0.8));
         Ogre::Light* l;
         l = mSceneMgr->createLight("BlueLight");
         l->setPosition(1000,1000,1000);
         l->setDiffuseColour(0.7, 0.7, 1.0);
         l = mSceneMgr->createLight("GreenLight");
         l->setPosition(-1000,1000,1000);
         l->setDiffuseColour(0.7, 1.0, 0.7);
         mCamera->setPosition(0,0,1000);

 

Create the EZPhysics classes. First the EZTree() class imports the XML file create by the Editor. Then the Ogre display class is created. We pass the Ogre Scene Manager and helper proxies string list. Finally we look for the Scene node called “taxi” and reset the internal structures to prepare them for simulation (SceneRefresh(.. False)  means pre-simulation, later we call refresh with true once simulation is active. 

         // create the EZPhysics Ogre class
         // s_Helpers is an enumerated list of name of helper meshes that were
         // defined in the EZPhysicsEditor and are to be found in the .sim file
         EZstring s="../Media/EZPhysicsDemo1.sim";
         s_EZTree = new EZTree();
         s_EZTree->ImportFile(EZstring("../Media/EZPhysicsDemo1.sim"));
         s_EZOgre = new EZOgre(mSceneMgr, s_Helpers);
         new EZInput(mWindow);

         //s_EZOgre->ParseResourceFile(s_EZTree->m_firstnode->GetGraphicsInfo()->resourcefile);
         s_EZOgre->SceneNew(s_EZNode=EZTree::getSingleton().Find("taxi"));
         s_EZOgre->SceneRefresh(s_EZNode, false);
         s_EZNode->GetScene()->ODE_Init();
}

That’s basically it. All the rest of the code has to do with Ogre window initiations.

 

    // override the method and create frame listener

    void createFrameListener(void)
    {
        mFrameListener= new EZPhysicsDemo1FrameListener(mWindow, mCamera);
        mRoot->addFrameListener(mFrameListener);
    }
};

#define WIN32_LEAN_AND_MEAN
#include "windows.h"

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
    // Create application object
    EZPhysicsDemo1 app;
    try {
        app.go();
    } catch( Ogre::Exception& e ) {
        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
    }
    return 0;
}

 

OgrePlatform.dll problem

Ogre’s philosophy about mouse/keyboard input is that it’s not Ogre’s business and with version 1.x it formally let the whole business be outsourced to CEGUI.  However, there still seems to be a dependency on Ogre to do on the low level layer of capturing Windows input.

EZPhysics Editor is a windows (wxWidgets) application and needs the Ogre window to behave like a good citizen in a windows environment without making the mouse invisible and trapping it within the window.

In order for the mouse to work you can just replace the default pre-compiled OgrePlatform.dll that comes with Ogre SDK (or that you compiled from ogrenew), with the DLL that comes with EZPhysics Editor package.

If you want to understand the difference between that DLL and the default, here goes:

1) Modify ogrenew\PlatformManagers\Win32\src\OgreWin32Input.cpp and OgreWin32Input8.cpp so that ShowCursor(true) instead of false

2) Modify those same files looking for lines like this:

SetCooperativeLevel(mHWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE) and change DISCL_ECLUSIVE to DISCL_NONECLUSIVE

3) Compile for debug and release and then copy the DLL PlatformManager_Win32.dll found in bin/release and bin/debug to the directory where you plan to place and run your compiled executables. Change the DLL name to OgrePlatform.dll

 

Technology

EZPhysics may eventually support multiple physics engines and multiple graphics libraries. It is designed to run on Windows and Linux (though currently only tested under Windows), relying on popular public-domain multi-platform libraries :

Ogre—An OpenSource object oriented 3D graphics package of libraries.

ODE—An OpenSource physics simulation library.

OPCODE—An OpenSource mesh collision detection library (bundled with ODE).

TinyXML—An OpenSource library for easily reading and writing XML files.

wxWidgets—An OpenSource MS Windows GUI library look-alike, compatible with all operating systems.

Doxygen—An application that transforms documented C++ code into readable documentation.