I want to rotate the mesh before attaching it to a Body

hoffhoff

04-10-2006 23:28:48

Hello,
Here is my problem: I´m loading a .mesh file, but it is not correct. I need to rotate it.

So Im trying:


ent = pSceneManager->createEntity(entityname, meshfilename);
pSceneNode = pSceneManager->getRootSceneNode()->createChildSceneNode(nodename);
pSceneNode->setScale(scale);
pSceneNode->pitch(Ogre::Radian(PI/2), Ogre::Node::TransformSpace::TS_LOCAL);

conv = new OgreNewt::CollisionPrimitives::ConvexHull(pWorld, pSceneNode, pSceneNode->getOrientation(); pSceneNode->getPosition());

pBody = new OgreNewt::Body(pWorld, conv);
...


but it does not work. The mesh do rotate, but the newton body still holds the old orientation.

I also tried:


ent = pSceneManager->createEntity(entityname, meshfilename);
pSceneNode = pSceneManager->getRootSceneNode()->createChildSceneNode(nodename);
pSceneNode->setScale(scale);

Ogre::Quaternation orientbox(Vector3(0,-1,0), Vector3(1,0,0), Vector3(0,0,1));
orientbox.FromAngleAxis(Radian(Degree(-90)), Vector3(1,0,0);
pSceneNode->setOrientation(orientbox);

conv = new OgreNewt::CollisionPrimitives::ConvexHull(pWorld, pSceneNode, pSceneNode->getOrientation(); pSceneNode->getPosition());

pBody = new OgreNewt::Body(pWorld, conv);
...



But I still have the same problem.

Could someone tell me what I´m doing wrong?


Thanks!

HexiDave

04-10-2006 23:57:19

How about setting it up without any rotation first (create the entity, attach to SceneNode, create body/collision data, attach body) and THEN rotate to your desired angle? That way there's only one rotation taking place.

hoffhoff

05-10-2006 00:04:08

But why doesn´t it work?

Because it will be easier for me if I rotate the mesh first, because I´ll have first a class for creating the mesh.
If I rotate it after the attachment, the code will get very dirty.

HexiDave

05-10-2006 04:50:54

Best guess is that it's rotating too far because (if memory serves) when you attach the Body to the node, it assumes it's rotation -

conv = new OgreNewt::CollisionPrimitives::ConvexHull(pWorld, pSceneNode, pSceneNode->getOrientation(); pSceneNode->getPosition());

There, skip pSceneNode->getOrientation(); and use Quaternion::IDENTITY (pretty sure that's the correct one.) That way it's not rotating the node AND the body - this way the body assumes the node's rotation only and doesn't spin past the position you want.

Give it a shot at least - I'm 99% sure this should work. I have some extra hard drive space now so I can download all the Newton/OgreNewt files and build it and test tomorrow if you still have issues.

hoffhoff

06-10-2006 23:46:47

Hey,
thank you. That did work, but now all my movement functions are weird.

The code is like this now:


pSceneNode = pSceneManager->getRootSceneNode()->createChildSceneNode(nodename);
pSceneNode->setScale(scale);
pSceneNode->attachObject(ent);

Ogre::Quaternion orientbox(Ogre::Vector3(1,0,0),Ogre::Vector3(0,1,0), Ogre::Vector3(0,0,1));
orientbox.FromAngleAxis(Ogre::Radian(Ogre::Degree(90)), Ogre::Vector3(1,0,0));
orientbox.normalise();
pSceneNode->setOrientation(orientbox);

//Now create the newton body
conv = new OgreNewt::CollisionPrimitives::ConvexHull(pWorld, pSceneNode, Ogre::Quaternion::IDENTITY /*pSceneNode->getOrientation()*/, pSceneNode->getPosition());
pBody= new OgreNewt::Body(pWorld, conv);
pBody->setMaterialGroupID(pMatCollision->getMaterialID(this->Material));

pBody->attachToNode( pSceneNode );
pBody->setPositionOrientation( pos, pSceneNode->getOrientation() );
pBody->setUserData(this);
pBody->setAutoFreeze(0);

this->setStateToFloating();


//Now calculate the Intertial
conv->calculateInertialMatrix(inertia, offset);
//We dont need conv anymore
delete conv;
//Set the mass matix
pBody->setMassMatrix(mass, inertia);
//Set force callback
pBody->setCustomForceAndTorqueCallback(CCharacter::playerForceandTorqueCallback);




And for movement, I have this function (for moving forward):


int CCharacter::MoveForward()
{
Vector3 globalFaceVec = this->getSceneNode()->getOrientation() * Ogre::Vector3::NEGATIVE_UNIT_Z;
globalFaceVec.y = 0.0f;
globalFaceVec.normalise();
movement = globalFaceVec * SpeedMove;
return 1;
}


And for spinning:


int CCharacter::SpinLeft()
{
this->spin.y = SpeedSpin;
return 1;
}



And the callback is basically:

Ogre::Vector3 force(0,GRAVITY,0);
force += player->getMovement();
force *= player->getMass();

me->addTorque( player->getSpin());
me->addForce( force );


The spinning is ok, but when I press it to move forward, it does not move in a straight direction. Depending on the angle, it moves really forward (from the mesh´s view point), sometimes to the left, sometimes to the right and so.

It was working fine before.

Do you know what happened?

Thanks!