almost exactly what i was looking for, now all i need is it to follow the camera when you move up down?BableOff wrote:Updated solution.Code: Select all
void faceTarget(irr::core::vector3df targetPos) { core::vector3df nodePos = targetPos - myNode->getPosition(); myRotation.Y = atan(nodePos.Z/nodePos.X) * (180.0f / irr::core::PI); if((targetPos.X - myNode->getPosition().X) > 0) { myRotation.Y = 90 - myRotation.Y; } else if((targetPos.X - myNode->getPosition().X) < 0) { myRotation.Y = -90 - myRotation.Y; } myRotation.Y -= 90; myNode->setRotation(myRotation); }
Rotate a Node, face another node
-
- Posts: 3
- Joined: Wed Jul 25, 2007 4:09 pm
I modified it a little
using it is simple
Code: Select all
irr::core::vector3df faceTarget(irr::core::vector3df targetpos, irr::core::vector3df nodepos) {
core::vector3df nodePos = targetpos - nodepos;
float degree = atan(nodePos.Z/nodePos.X) * (180.0f / irr::core::PI);
if((nodepos.X - targetpos.X) > 0) {
degree = 90 - degree;
} else {
degree = -90 - degree;
}
if (nodePos.X==0.f)
{
return (vector3df(0,0,0));
}
else
{
return (vector3df(0,(float) degree, 0));
}
}
Code: Select all
node->setRotation(faceTarget(target->getPosition();node->getPosition());
Code: Select all
void Actor::faceTarget(core::vector3df tpos)
{
core::vector3df diff = tpos - actorNode->getAbsolutePosition();
diff.normalize();
if (tpos.X != 0.0f || tpos.Y != 0.0f) {
f32 newYaw = atan2(diff.X, diff.Z);
yaw = newYaw * core::RADTODEG;
}
}
ActorNode and yaw belong to the Actor class. To do something simular to the above code it would look like this:
Code: Select all
irr::core::vector3df faceTarget(irr::core::vector3df targetpos, irr::core::vector3df nodepos) {
core::vector3df posDiff = targetpos - nodepos;
f32 degree = nodepos.Y; //keep current rotation if nothing to do
posDiff.normalize();
if (posDiff.X != 0.0f || posDiff.Z != 0.0f)
degree = atan2(posDiff.X,posDiff.Z) * core::RADTODEG;
return core::vector3df(0,degree,0);
}
I just spent... too many hours trying to get one node to face another node in 3d space. Specifically, I have one spaceship controlled by the AI, which has to turn and fire on the player's ship. Being a space-flight game, there is no ground, and no real sense of "up" to speak of. The spaceship has to rotate through all three axes, not just the Y axis.
By sheer accident (I'm horrible at math) I finally discovered how to do this using Irrlicht without really having to do all that math. Hopefully this will save someone else the trouble I went through.
Edit: I just noticed in the Irrlicht Documentation under this function it says "Thanks to Arras on the Irrlicht forums to add this method." Thanks man :-)
By sheer accident (I'm horrible at math) I finally discovered how to do this using Irrlicht without really having to do all that math. Hopefully this will save someone else the trouble I went through.
Code: Select all
// rotate to face target Node
vector3df nodePos = node->getPosition();
vector3df targetPos = targetNode->getPosition();
vector3df diff = targetPos - nodePos;
node->setRotation(diff.getHorizontalAngle());
node->updateAbsolutePosition();
-
- Posts: 1
- Joined: Thu Feb 12, 2009 5:36 am