Rotate a mesh

majc

10-03-2008 21:52:50

How i rotate a mesh with ogrenewt for example 90º ?
Thanks in advance!

I found the solution for this:

Ogre::Quaternion orientbox = Ogre::Quaternion::IDENTITY;
orientbox.FromAngleAxis(Ogre::Radian(Ogre::Degree(Angle)), Ogre::Vector3(0,1,0));
orientbox.normalise();
enemy_body->setPositionOrientation(Vector3(this->CharXPos,0,this->CharZPos),orientbox);


But now i have another problem, how i move the mesh in the direction it is facing?

Alpha_Blend

11-03-2008 10:13:37

Maybe you can find solutions here:
http://www.ogre3d.org/wiki/index.php/Quaternion_and_Rotation_Primer

majc

11-03-2008 11:20:54

But i already know how to rotate the body my problem is to move the body in the direction it is facing :s
But thanks for the link anyway :)

FrSl

11-03-2008 12:26:06

Hi,

Use addLocalForce() to move the object in the right direction without having to calculate too much.
You could also use addForce(), setForce() or setVelocity() for each object. You can calculate the x and z forces with 'cos(angle)*force' and 'sin(angle)*force' for a simple walking character.

Good luck!

majc

11-03-2008 19:29:19

Thanks mate i will try it :)

majc

11-03-2008 20:32:03

I did this but still dont work:

void cEnemy::enemyWandering(cPlayer* player)
{
Vector3 pos, trans;
Vector3 vec;
Ogre::Quaternion quat = mEnemyBodyNode->getOrientation();

float Direction = 6.28f / 360.0f * (float)(rand() % 360);
this->XMove = cos(Direction) * Distance;
this->ZMove = sin(Direction) * Distance;
this->CharXPos = mEnemyBodyNode->getPosition().x + this->XMove;
this->CharZPos = mEnemyBodyNode->getPosition().z + this->ZMove;


while(this->Wandering == true)
{
pos.x = mEnemyBodyNode->getPosition().x;
pos.y = mEnemyBodyNode->getPosition().y;
pos.z = mEnemyBodyNode->getPosition().z;


if (this->CharXPos >= this->WanderMinX && this->CharXPos <= this->WanderMaxX && this->CharZPos >= this->WanderMinZ && this->CharZPos <= this->WanderMaxZ)
{
if ((mEnemyBodyNode->getPosition().x != CharXPos) && (mEnemyBodyNode->getPosition().z != CharZPos))
{
enemy_body->setVelocity( Vector3(50,0,50));
}
else
{
//enemy_body->setVelocity( Vector3(1,1,1));
float Angle = (float)(rand() % 360);
Direction = 6.28f / 360.0f * Angle;
this->XMove = cos(Direction) * Distance;
this->ZMove = sin(Direction) * Distance;
this->CharXPos = mEnemyBodyNode->getPosition().x + this->XMove;
this->CharZPos = mEnemyBodyNode->getPosition().z + this->ZMove;

Ogre::Quaternion orientbox = Ogre::Quaternion::IDENTITY;
orientbox.FromAngleAxis(Ogre::Radian(Ogre::Degree(Angle)), Ogre::Vector3(0,1,0));
orientbox.normalise();
enemy_body->setPositionOrientation(pos,orientbox);
[b]enemy_body->addForce(Vector3(cos(Angle)*1000000, 0.0f, sin(Angle)*1000000));[/b]
//this->Wandering = false;
}
}

break;
}
}


I tryed also this but still dont work:

enemy_body->addLocalForce(Vector3(cos(Angle)*1000000, 0.0f, sin(Angle)*1000000),pos);

FrSl

12-03-2008 09:55:14

Hi,

You can't set the force directly from within your code, use a callback for it.
It's also not a good idea to use functions like: enemy_body->setPositionOrientation(pos,orientbox);
Because this conflicts with the physics, try to use the force and torque functions instead for a realistic result.

Use a function like this for the forcecallback:
void cPlayer::forceCallback(OgreNewt::Body* body)
{
body->addForce(Vector3(1000,0,1000));
}


And assign this forcecallback to your model instead of the default force/torque callback:
enemy_body->setCustomForceAndTorqueCallback(boost::bind(&cPlayer::forceCallback, this, _1));

This example should atleast move your character in a direction, it should be easy asjust your already existing code within this system.

Good luck,

FrSl.

majc

12-03-2008 12:45:05

Ok thx mate for the advice i already use the force callback for gravity i will try it to apply it in the enemies :)

majc

12-03-2008 13:16:53

I follow your advice and i did this:

void cEnemy::CustomEnemyCallback(OgreNewt::Body * body)
{
body->addTorque(Vector3(cos(Angle)*1000, 0.0f, sin(Angle)*1000));
body->addForce(Vector3(cos(Angle)*1000, 0.0f, sin(Angle)*1000));
}


and this:

void cEnemy::enemyWandering(cPlayer* player)
{
Vector3 pos, trans;
Vector3 vec;
Ogre::Quaternion quat = mEnemyBodyNode->getOrientation();
Angle = (float)(rand() % 360);
float Direction = 6.28f / 360.0f * (float)(rand() % 360);
this->XMove = cos(Angle) * Distance;
this->ZMove = sin(Angle) * Distance;
this->CharXPos = mEnemyBodyNode->getPosition().x + this->XMove;
this->CharZPos = mEnemyBodyNode->getPosition().z + this->ZMove;


while(this->Wandering == true)
{
pos.x = mEnemyBodyNode->getPosition().x;
pos.y = mEnemyBodyNode->getPosition().y;
pos.z = mEnemyBodyNode->getPosition().z;

if (this->CharXPos >= this->WanderMinX && this->CharXPos <= this->WanderMaxX && this->CharZPos >= this->WanderMinZ && this->CharZPos <= this->WanderMaxZ)
{
if ((mEnemyBodyNode->getPosition().x != CharXPos) && (mEnemyBodyNode->getPosition().z != CharZPos))
{

enemy_body->setCustomForceAndTorqueCallback(boost::bind(&cEnemy::CustomEnemyCallback, this, _1));
//enemy_body->setVelocity( Vector3(50,0,50));
}
else
{
//enemy_body->setVelocity( Vector3(1,1,1));
Angle = (float)(rand() % 360);
Direction = 6.28f / 360.0f * Angle;
this->XMove = cos(Angle) * Distance;
this->ZMove = sin(Angle) * Distance;
this->CharXPos = mEnemyBodyNode->getPosition().x + this->XMove;
this->CharZPos = mEnemyBodyNode->getPosition().z + this->ZMove;
Ogre::Quaternion orientbox = Ogre::Quaternion::IDENTITY;
orientbox.FromAngleAxis(Ogre::Radian(Ogre::Degree(Angle)), Ogre::Vector3(0,1,0));
orientbox.normalise();
enemy_body->setCustomForceAndTorqueCallback(boost::bind(&cEnemy::CustomEnemyCallback, this, _1));
}
}

break;
}
}


Now its moving but how i rotate the body in the sabe direction of the movement using addTorque() ?

majc

13-03-2008 20:42:54

Anyone?