ziems
29-06-2006 14:54:58
I created a small Physics Class for a huge project.
The physics works fine, but I have problems to create
custom ForceAndTorqueCallback() functions.
this is the addObject Function where the Callback function should
be appied.
[CLASS dgk:Physics]
==============
void dgkPhysics::addObject(dgkObject *obj, Ogre::Real mass)
{
OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::ConvexHull(mWorld, obj->getNode());
OgreNewt::Body* body = new OgreNewt::Body(mWorld, col);
body->attachToNode(obj->getNode());
body->setPositionOrientation( obj->getNode()->getWorldPosition() , Ogre::Quaternion::IDENTITY );
delete col;
Ogre::AxisAlignedBox bbox = obj->getEntity()->getBoundingBox();
Ogre::Vector3 max = bbox.getMaximum();
Ogre::Vector3 min = bbox.getMinimum();
Ogre::Vector3 size = max-min;
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcBoxSolid(mass, size);
body->setMassMatrix( mass, inertia );
body->setCustomForceAndTorqueCallback(&dgkPhysics::sFC);
obj->setPhysicsBody(body)}
}
Yust to test if you can apply callback functions to Ogre::Body here is the example Callback function:
void dgkPhysics::sFC(OgreNewt::Body* me)
{
Ogre::Real mass;
Ogre::Vector3 inertia;
me->getMassMatrix(mass, inertia);
Ogre::Vector3 force(0,-5,0);
force *= mass;
}
It doesn't compile. What am I doing wrong.
How can I easily apply custum callback-functions?
Thanks
abecam
29-06-2006 15:28:00
You can try that, should work if all is in dgkPhysics class:
body->setCustomForceAndTorqueCallback<dgkPhysics>(dgkPhysics::sFC,this);
More generic:
body->setCustomForceAndTorqueCallback<callBackClass>(dgkPhysics::sFC,callBackClassInstance);
I am kind of beginner with this syntax, so I hope it still helps...
TimErwin
29-06-2006 23:59:51
Following the comments in ogrenewt_body.h you have to do it this way (from inside the class):
body->setCustomForceAndTorqueCallback(fastdelegate::MakeDelegate( this, &dgkPhysics::sFC ));
Just tested it, works fine though I don't understand this "fastdelegate" thing
walaber
30-06-2006 06:21:24
you have an outdated version of OgreNewt. we no longer use fastdelegate... please delete your OgreNewt directory, and redownload it!
lonwolf
30-06-2006 20:21:51
yo *Ziems*
this is what i use to set a customForceAndTorqueCallback but with one big difference:
the callback function isnt in a namespace/class but stand-alone(global I mean

)
void forceCallback(OgreNewt::Body* me)
{
Ogre::Real mass;
Ogre::Vector3 inertia;
me->getMassMatrix(mass, inertia);
Ogre::Vector3 force(0, -9.81, 0);
force*=mass;
me->addForce(force);
}
and setting the callback is done in createScene()
OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Cylinder(mWorld, 10, 70, bn->getOrientation(), bn->getPosition());
OgreNewt::Body* bd = new OgreNewt::Body(mWorld, col);
delete col;
bd->attachToNode(bn);
bd->setPositionOrientation(bn->getPosition(), bn->getOrientation());
Ogre::Real mass = 500.0f;
Ogre::Vector3 inert = OgreNewt::MomentOfInertia::CalcCylinderSolid(mass, 10, 70);
bd->setMassMatrix(mass, inert);
bd->setCustomForceAndTorqueCallback(forceCallback);
hope it helps in anyway

PS: im using the last version of OgreNewt and Ogre
SilentRage
04-07-2006 03:33:19
I'm by no means an expert in OOP and I may not see something, but it was my understanding that the custom call back functions were setup in Newton as a means of polymorphism.
If the Body class was setup with a static member function like below and had a virtual function applyForceAndTorque(), wouldn't that make inheriting from OgreNewt::Body a little more intuitive to C++ stylee? I imagine it might add a bit of overhead and increase the size of the objects, but it would mean it would only be necessary to subclass from Body and provide your own applyForceAndTorque member function, masking the whole callback business from users of the class.
I wouldn't exactly call that a suggestion, but I brought it up to stimulate conversation. I've spent a lot of time walking through the fundamentals of Newton and then how in turn OgreNewt integrates Newton and Ogre. I just want to see if I'm starting to get an understanding and the callbacks confused me quite a bit. Would what I'm talking about technically work? Any comments on the pros and cons of setting up OgreNewt::Body this way?
Body::newtonForceTorqueCallback is static.
Body::applyForceAndTorque is virtual
Body::Body( const World* W, const OgreNewt::Collision* col, int bodytype )
{
m_world = W;
m_collision = col;
m_type = bodytype;
m_node = NULL;
m_matid = NULL;
m_userdata = NULL;
m_forcecallback = NULL;
m_transformcallback = NULL;
m_buoyancycallback = NULL;
m_body = NewtonCreateBody( m_world->getNewtonWorld(), col->getNewtonCollision() );
NewtonBodySetForceAndTorqueCallback( m_body, newtonForceTorqueCallback );
NewtonBodySetUserData( m_body, this );
NewtonBodySetDestructorCallback( m_body, newtonDestructor );
}
void _CDECL Body::newtonForceTorqueCallback( const NewtonBody* body )
{
OgreNewt::Body* me = (OgreNewt::Body*)NewtonBodyGetUserData( body );
me->applyForceAndTorque();
}
void Body::applyForceAndTorque() { }
walaber
04-07-2006 08:22:55
yes, that is probably what I would do if I were to re-write OgreNewt. however, using a callback system makes it much easier to "swap out" callbacks... so for example you can make a simple gravity callback, and use it for all bodies... and then make a callback that drags objects around with the mouse, and through the callback, you can apply that function to ANY body. through polymorphism, this would be a little difficult.
but you are probably right, it's more OOP style to use an inherited base class, and virtual functions.
OgreNewt was the first OOP library I created, and I have definately been learning as I go.
abecam
04-07-2006 10:52:25
I really like the way it works now... The possibility to share a callback between different objects is very nice, and the current solution is elegant too. Thinking that we not always need to create inherited bodies too (I use a lot of primitives with custom callbacks), the callback might stay "outside", and the "interface-style" used now is ok for that, and as flexible as everyone want.
I am not such a specialist in C++ (then I have teached it

), but in general programming, and C++ often lacks clarity because of "overdone OO mechanisms", so I do not want to see polymorphism at all cost. ONewt is very clean in this aspect, and that's all I ask! (maybe to see my object reappearing too

)
Walaber: for a first OOP library, it's a master work!
OvermindDL1
05-07-2006 17:42:46
Making Body a virtual class will incur an overhead, avoid it and use callbacks instead. Not to mention that it is really easy to swap in/out callbacks (which I am using rather heavily) this style.