Page 2 of 2
Posted: Wed Aug 15, 2007 10:58 am
by nulldragon
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);
}
almost exactly what i was looking for, now all i need is it to follow the camera when you move up down?
Posted: Tue Dec 04, 2007 6:15 pm
by Mag-got
How do I use "faceTarget", node->faceTarget(thetarget);?
Posted: Mon Jan 07, 2008 7:13 pm
by Aleyer
I modified it a little
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));
}
}
using it is simple
Code: Select all
node->setRotation(faceTarget(target->getPosition();node->getPosition());
Posted: Tue Mar 11, 2008 2:25 am
by varmint
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;
}
}
I have another process that smoothly transitions from yaw to the node's current rotation. Alls I did was take the atan2 of (diff.X, diffZ)...
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);
}
Posted: Mon Jun 23, 2008 2:32 pm
by squisher
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.
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();
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 :-)
Posted: Thu Feb 12, 2009 8:04 am
by reezkasphere
wow man.. that code save me a lot.. thanks..
Posted: Mon Mar 09, 2009 1:18 am
by Vandrick
Never Mind this miss post...
Posted: Mon Sep 20, 2010 10:36 pm
by raincool
facetarget function is very useful to help me . thanks