Wrapping OgreNewt physics

Vectrex

22-07-2006 09:38:57

I know I know, a wrapper of a wrapper ;) But it's a very nice and very easy to use physics engine. I'm about to start on this so I thought I'd ask if anyones working on it before I start. I've really no idea about swig but it doesn't seem too hard, so I'll give it a go. Is swig the main option? There aren't many classes/functions in ogrenewt (compared to ogre) so would doing it by hand be easier?

LuRenJia

23-07-2006 03:22:25

Great work, but why don't you warp ODE or Novodex(PhysX)?
keep go on, looking forward to that.

Vectrex

23-07-2006 06:00:41

well it's basically because I know how to use newton :) and it supports super easy convex hulls and tri meshes. Plus ogrenewt has a great ragdoll class.
I've never really used ode or physx, I'm sure NXOgre would be great to wrap.

Bekas

23-07-2006 22:17:17

Here's an idea: how about instead of wrapping the OgreNewt wrapper with SWIG, you convert it to C++/CLI.

Most of the native code of OgreNewt will stay the same, you will just convert the class declarations and the method signatures to managed ones.

Wherever OgreNewt requires an Ogre object (like Mesh) you will provide the native pointer of the OgreDotNet class. It will be much simpler IMO.

pjcast

23-07-2006 22:23:11

I second this that a managed c++/cli conversion would be cool :)

Personnaly, I am getting kind of tired dealing with SWIG limitations when returning/casting from swig returned base classes. And, I am also looking for a managed physics solution for my editor :)

Vectrex

24-07-2006 00:34:11

ok cool... now if someone could point me somewhere as to HOW to do that it'd be much appreciated ;)

Bekas

24-07-2006 19:46:19

http://msdn2.microsoft.com/en-us/library/ms177551.aspx

Huge collection of C++/CLI examples to get you started :wink:

Vectrex

25-07-2006 20:26:52

cool, thanks. I was looking over MOgre and it seems you've got a magic wrapper tool :) Would it be possible for you to run it over ogreNewt to give me a head start? Doesn't matter if it doesn't work, but looks like it'd get me going. Will callbacks be a problem?

Game_Ender

25-07-2006 21:40:41

Bekas, you are working on Mogre right? Is there any chance you can release the tools you use for generating the a wrapping, I am looking in on how to make wrapping for D and I am looking for something better than swig.

Bekas

25-07-2006 22:22:11

@Vectrex, that's not necessary, OgreNewt is not that big of a project; and instead of 'wrapping', I suggest 'porting' the code over to C++/CLI, which means you'll actually create a Newton .NET wrapper based off the code of OgreNewt. In fact my intention is, once I finish Mogre, to do just that. If you're interested in giving it a go, I can offer you pointers with specific classes to see how they should be converted, post or PM me.

@Game_Ender: There are two parts, the first is a tool to parse Ogre's include files and spit out a language independent xml representation of Ogre's classes. Full credits for this belong to doxygen and Ogre4J's wrapping effort. I downloaded the tool from http://ogre4j.org/drupal/node/12 and modified it a bit to get stuff that I wanted (like getting the protected members too). I can upload it if you like.

The second part is a C# tool to parse the xml file and spit out the C++/CLI wrapping code. The code for this is a mess and I'll release it only after some heavy dose of cleaning up.
<edit> and it's too C++/CLI specific to be of any use for D anyway..</edit>

Game_Ender

25-07-2006 22:58:28

It would be greate to see an updated version of your tool. Once I handle a proper way to wrap C++ in C, I can start on the wrapping D. I just have to figure out how to pass exceptions through the C layer. It will probably require two additional paramters to every function. One thats a pointer to the function object, another thats a pointer to some kind of type identifier.

Vectrex

06-08-2006 16:03:55

@Vectrex, that's not necessary, OgreNewt is not that big of a project; and instead of 'wrapping', I suggest 'porting' the code over to C++/CLI....

Well I've got nearly no idea, I've looked at the MOgre code and can seeish what's happening in the cpp files, but the .h files are a no go.
I'm mainly wondering if I can use a CLI wrap/port with the swigged ogreDotNet? Are they 'compatible' especially with native method parameters like Ogre::Vector3 and Ogre::Entity etc
I also notice with Morge that it even uses it's own Mogre::radians and stuff. Why can't it use native ogre stuff there, do you need to wrap everything that ever gets passed through a method?
I really don't understand what you can and can't do, I thought it'd be easy and it looks like it might be, but there's loads of stuff going on which I've no idea about. Can you wrap just one method in ogrenewt for me? Something with a vector3 or some other ogre specific parameters. eg ogreNewt::Body::setPosition.
Is Swigging ogreNewt a bad idea compared to CLI?

Bekas

08-08-2006 20:02:16

I'm mainly wondering if I can use a CLI wrap/port with the swigged ogreDotNet? Are they 'compatible' especially with native method parameters like Ogre::Vector3 and Ogre::Entity etc
I haven't used ODN much, but I think that all SWIG classes get a method that exposes the native pointer.

do you need to wrap everything that ever gets passed through a method?
Yes, in order to handle native objects and pass them around in .NET you need to wrap them somehow.

To take OgreNewt's Body for example, here's a bit of "pseudo-code":

Native OgreNewt:
class _OgreNewtExport Body
{
public:
//! custom force callbacFk.
/*!
this function is called from within the OgreNewt::World::update() command when applying forces to Rigid Bodies, such as
gravity, etc.

You can set this as the custom force callback for a body by using the setCustomForceCallback() function.
Using boost::function means OgreNewt can now accept pointers to member functions of specific classes.
*/
typedef boost::function<void(OgreNewt::Body*)> ForceCallback;


[.........]


Body( const World* W, const OgreNewt::Collision* col, int bodytype = 0 );

//! destructor
~Body();


[.........]


void setPositionOrientation( const Ogre::Vector3& pos, const Ogre::Quaternion& orient );


void Body::setPositionOrientation( const Ogre::Vector3& pos, const Ogre::Quaternion& orient )
{
if (m_body)
{
float matrix[16];

OgreNewt::Converters::QuatPosToMatrix( orient, pos, &matrix[0] );
NewtonBodySetMatrix( m_body, &matrix[0] );

if (m_node)
{
m_node->setOrientation( orient );
m_node->setPosition( pos );
}
}
}




Ported to C++/CLI:
public ref class Body
{
public:
//Use .NET delegates to replace OgreNewt's callbacks
delegate void ForceCallBack(....)

[.........]


//World and Collision have been also ported to C++/CLI
Body( World^ W, OgreNewt::Collision^ col, int bodytype = 0 );

//! Dispose method
~Body();


[.........]


//
void SetPositionOrientation( Math3D::Vector3 pos, OgreDotNet::OgreQuaternion^ orient );


void Body::SetPositionOrientation( Math3D::Vector3 pos, OgreDotNet::OgreQuaternion^ orient )
{
//if you need to convert to Ogre's Vector3 and Quaternion, do the conversions like this:
OgreDotNet::OgreVector3^ odn_vec = OgreDotNet::OgreVector3::FromVector3(pos);
Ogre::Vector3& ogre_vec = *(Ogre::Vector3*)(void*)OgreDotNet::OgreVector3::getCPtr(odn_vec).Handle

Ogre::Quaternion& ogre_quat = *(Ogre::Quaternion*)(void*)OgreDotNet::OgreQuaternion::getCPtr(orient).Handle


if (m_body)
{
float matrix[16];

OgreNewt::Converters::QuatPosToMatrix( orient, pos, &matrix[0] );
NewtonBodySetMatrix( m_body, &matrix[0] );

if (m_node)
{
m_node->setOrientation( orient );
m_node->setPosition( pos );
}
}
}



You have to experiment a bit with the delegates to get them to work with Newton, I'm not sure how the code should look like exactly.