steppty
29-03-2013 07:18:38
Hi, I would like to ask:
Is it OK for me to register multiple objects to the same setCustomForceAndTorqueCallback()?
I mean ...
Please help, urgent case!!! Thanks!!!
Is it OK for me to register multiple objects to the same setCustomForceAndTorqueCallback()?
I mean ...
for(int i=0; i<3; i++)
{
... // I have a three fishes
... // Can I register all 3 with the same mySwimCallback() like the following?
... // Will there be any problems?
myFish_body->setCustomForceAndTorqueCallback<OgreApp1>(&OgreApp1::mySwimCallback, this);
}
void OgreApp1::mySwimCallback(OgreNewt::Body *body, float timeStep, int threadIndex)
{
// I tried to move the fish only if myFishMoveFlag is true
// but if one fish moves, the other also move which i don't want
for(int i=0;i<3;i++)
{
myAnimationState->addTime(timeStep);
if(myFishMoveFlag==true)
{
Ogre::Real mass;
Ogre::Vector3 inertia;
myFish_body->getMassMatrix(mass, inertia);
myAnimTime += timeStep;
int fish_path_length = 40;
while (myAnimTime > fish_path_length)
myAnimTime -= fish_path_length;
Ogre::Vector3 newPos = myFishSplines.interpolate(myAnimTime / fish_path_length);
// Work out the direction
Ogre::Vector3 direction = myFishLastPosition - newPos;
direction.normalise();
Ogre::Vector3 pos = myFish_body->getPosition();
myFish_body->setPositionOrientation(pos, Ogre::Vector3::UNIT_X.getRotationTo(direction));
Ogre::Vector3 velocity = (myFishLastPosition - pos)/ timeStep;
Ogre::Vector3 accel = (velocity - myFish_body->getVelocity()) / timeStep;
Ogre::Vector3 force = accel * mass;
body->addForce(force);
myFishLastPosition = newPos;
if(myFishLastPosition.distance(myFishSplines.getPoint(1))<5)
{
myFishSplines.clear();
myFishMoveFlag=false;
myAnimTime=0;
}
}
else
{
myFish_body->setPositionOrientation(myFishNode->getPosition(), myFish_body->getOrientation());
}
}
}
Please help, urgent case!!! Thanks!!!