Rotate a Node, face another node

A forum to store posts deemed exceptionally wise and useful
nulldragon
Posts: 3
Joined: Wed Jul 25, 2007 4:09 pm

Post 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?
Mag-got
Posts: 42
Joined: Tue Dec 04, 2007 5:53 pm

Post by Mag-got »

How do I use "faceTarget", node->faceTarget(thetarget);?
Aleyer
Posts: 17
Joined: Thu Dec 14, 2006 9:03 pm

Post 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());
varmint
Posts: 46
Joined: Fri Oct 06, 2006 4:33 pm

Post 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);
}
squisher
Competition winner
Posts: 91
Joined: Sat May 17, 2008 2:23 am
Contact:

Post 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 :-)
reezkasphere
Posts: 1
Joined: Thu Feb 12, 2009 5:36 am

Post by reezkasphere »

wow man.. that code save me a lot.. thanks.. :D
Vandrick
Posts: 13
Joined: Sat Feb 21, 2009 5:37 am

Post by Vandrick »

Never Mind this miss post...
raincool
Posts: 6
Joined: Sat Feb 27, 2010 11:30 am

Post by raincool »

facetarget function is very useful to help me . thanks :D
Post Reply