History: PhysX Candy Wrapper
Source of version: 6
Copy to clipboard
The [http://eyecm-physx.sourceforge.net/|PhysX Candy Wrapper for .NET] is a wrapper library for the [http://en.wikipedia.org/wiki/PhysX|PhysX] physics engine currently owned by Nvidia, although it was originally developed by Ageia. The wrapper is open source and is designed to be integrated with the graphic library of your choice. In this case we are using the existing ((MOGRE|Mogre)) binding so that the methods are directly compatible with Mogre.Vector3, Mogre.Quaternion, and so on. This approach makes things a lot easier and removes the need for yet another wrapper around ((NxOgre)) for example.
The first thing you'll want to do if you haven't already is download the SDK's.
!PhysX SDK
The PhysX SDK can be downloaded from the from NVidia website. The Windows version is completely free for both commercial and non-commercial use. You'll only need to pay a fee if you intend on releasing your game for the PS3, XBox or Wii. It's not open source, but it has been used in a many commercial games so it's heavily battle tested in the real world.
http://developer.nvidia.com/object/physx_downloads.html
!PhysX Candy Wrapper SDK
The wrapper is open source and can be downloaded from SourceForge. Although there is limited documentation, most of the code can be translated from the PhysX documentation fairly easily. There is also some bits and pieces floating around on the forums. It's my hope by making this wiki page that this situation improves. Even if the wrapper is no longer maintained, at least it's open source and can be taken over by other developers.
http://sourceforge.net/projects/eyecm-physx/files/eyecm-physx-mogre/
!Implementation
The first thing you will want to do is add a reference to the PhysX candy wrapper DLL to your Mogre project. Once this is done you'll have access to the wrapped classes under the namespace Mogre.PhysX;
What I did in my game engine was create a thin physics wrapper for the purpose of holding common functionality and potentially making it easier to use a different physics engine if the day comes. Firstly add this line to the top of your physics class.
{CODE(wrap="1", colors="c#")}
using Mogre.PhysX;
{CODE}
Next, you can add some private variables to the class that control the physics controller root and the scene (kind of like the SceneManager in Ogre).
{CODE(wrap="1", colors="c#")}
private Physics physics;
private Scene scene;
{CODE}
Okay, that's the easy stuff out of the road. Now for the initialisation method that should be called somewhere as your game loads. I call it just after initailising Ogre.
{CODE(wrap="1", colors="c#")}
public bool Initiliase()
{
// create the root object
this.physics = Physics.Create();
this.physics.Parameters.SkinWidth = 0.0025f;
// setup default scene params
SceneDesc sceneDesc = new SceneDesc();
sceneDesc.SetToDefault();
sceneDesc.Gravity = new Vector3(0, -9.8f, 0);
sceneDesc.UpAxis = NX_Y;
// your class should implement IUserContactReport to use this
//sceneDesc.UserContactReport = this;
this.scene = physics.CreateScene(sceneDesc);
// default material
scene.Materials[0].Restitution = 0.5f;
scene.Materials[0].StaticFriction = 0.5f;
scene.Materials[0].DynamicFriction = 0.5f;
// begin simulation
this.scene.Simulate(0);
return true;
}
{CODE}
After that you'll want to call an Update method in your main game / rendering loop (frameStarted for example).
{CODE(wrap="1", colors="c#")}
public void Update(float deltaTime)
{
this.scene.FlushStream();
this.scene.FetchResults(SimulationStatuses.AllFinished, true);
this.scene.Simulate(deltaTime);
}
{CODE}
And don't forget to be a good little programmer and clean up your resources. This should get called when your game exits. I'm not sure if it's absolutely required in managed code but it's better to be safe than sorry when we are dealing with unmanaged wrappers.
{CODE(wrap="1", colors="c#")}
public void Dispose()
{
this.physics.Dispose();
}
{CODE}
That's about it for the basics. You're game now has a working physics engine, although, it won't do much because you haven't setup any actors or collision shapes.
!Solid things that move
To make your game more interesting, you'll want to give physical properties to the models in your game. PhysX likes to call these Actors. You set up an actor by providing it the details of it's shape, mass and if it's static or dynamic.
More to come soon...
If you need help, open a thread in [http://www.ogre3d.org/addonforums/viewforum.php?f=8|Mogre add-ons forum] and ask user [http://www.ogre3d.org/addonforums/memberlist.php?mode=viewprofile&u=9599|zarifus] to answer in this thread.
!See also
* [http://en.wikipedia.org/wiki/PhysX|PhysX at Wikipedia]