Angle between SceneNodes

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Andreas
Posts: 166
Joined: Sun Oct 31, 2004 7:15 am
Location: Münster / Germany
Contact:

Angle between SceneNodes

Post by Andreas »

Hi all,

i have some basic question i'm stuck with for hours by now. I searched the forum, but had no luck... :(

I just want to get the Angle between two sceneNodes so i can rotate one to face the other in a smooth way.

Image

I found the following to face a target scene node (unitNode==SceneNode of player, target==SceneNode of target):

Code: Select all

void CBaseUnit::faceTarget() {
    vector3df diff = target->getPosition() - unitNode->getPosition();
    unitNode->setRotation(diff.getHorizontalAngle());
    unitNode->updateAbsolutePosition();
};
But this is setting the rotation directly. I would like to rotate the scene node with these methods, to make it look nicer:

Code: Select all

void CBaseUnit::turnLeft(unsigned int deltaTime) {
    matrix4 m = unitNode->getRelativeTransformation();
    matrix4 n;
      n.setRotationDegrees(vector3df(0, (-speed * rotationSpeed * deltaTime), 0));
      m *= n;
    unitNode->setRotation(m.getRotationDegrees());
}
and

Code: Select all

void CBaseUnit::turnRight(unsigned int deltaTime) {
    matrix4 m = unitNode->getRelativeTransformation();
    matrix4 n;
      n.setRotationDegrees(vector3df(0, (speed * rotationSpeed * deltaTime), 0));
      m *= n;
    unitNode->setRotation(m.getRotationDegrees());
}
But how do i determine the angle between the scene nodes (or even better, their positions as vector3df)? How do i know when to rotate left or right and that i still have to rotate. :-/

Any help would be great!

Thanks,
Andreas
jontan6
Posts: 278
Joined: Fri Jun 13, 2008 5:29 pm

Post by jontan6 »

what if u just do linear interpolate on each axis?
Masterhawk
Posts: 299
Joined: Mon Nov 27, 2006 6:52 pm
Location: GERMANY
Contact:

Post by Masterhawk »

Do you just want to rotate in 2D(just turn left and right or even up and down?) or can the models have an arbitrary position and angle in 3D space?
Image
Andreas
Posts: 166
Joined: Sun Oct 31, 2004 7:15 am
Location: Münster / Germany
Contact:

Post by Andreas »

Hi,

thanks for your answers so far! The models can only rotate around the y-axis (it's a top down view).

I tried using:

Code: Select all

  vector3df s(0, startRotation.Y, 0);
  vector3df d(0, destinationRotation.Y, 0);
  unitNode->setRotation(s.getInterpolated(d, tick));
with

Code: Select all

startRotation = unitNode->getRotation();
destinationRotation = (destination - unitNode->getPosition()).getHorizontalAngle();
and with "tick" beeing incremented during frames (and between 0.0 and 1.0), but it doesn't work. Obviously i am not understanding the interpolation process... :(

Any suggestions?
Andreas
ehenkes
Posts: 47
Joined: Sun Aug 03, 2008 2:52 pm
Location: Germany
Contact:

Post by ehenkes »

I also had to turn around the Y-axis for facing each other:

Code: Select all

void CCharacter::lookAt(vector3df vecEnd)
{
  static f32 yDegree;
  vector3df vecStart = getPos(); //position of looking character
  f32 yRadian = atan2( vecEnd.X - vecStart.X, vecEnd.Z - vecStart.Z );
  yDegree = f32(yRadian * RADTODEG); 
  yDegree = f32(s32(yDegree)%360); 
  if( yDegree != 0 )
  getNode()->setRotation(vector3df( -90, yDegree+90, 0 )); 
}
Hope it helps you a little bit. It is not perfect, but it works. Thus, if you find a better general solution, please post it here.
Post Reply