Maths problem

Discussion about everything. New games, 3d math, development tips...
Post Reply
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Maths problem

Post by ent1ty »

Hi! I'm trying to calculate the rotation.Y of a character that should be facing camera. But I can't seem to get it working, so, I'd be glad if someone would help me.

vector3df pos - position of character
vector3df target - position of camera

Code: Select all

float ratio= abs((target.X- pos.X)/ (target.Z- pos.Z));
float x= 1/ tan(ratio);
node->setRotation(vector3df(0, x, 0));
Thanks!
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Tihtinen
Posts: 76
Joined: Fri Jan 08, 2010 3:12 pm
Location: Finland

Post by Tihtinen »

I would recommend using c++ atan2 function and use the character as the center. Atan2 takes two arguments, x and y coords of the target (in this case, the camera) but as Irrlicht uses y for up, you should pass the camera's RELATIVE x and z (camerapositionvector2d - characterpositionvector2d) to the function.

The atan2 then returns the angle in radians the camera is located relative to the coordinate origin (the character).

Code: Select all

#include <cmath>

vector2df character( characterposition.X, characterposition.Z );
vector2df camera( camerapos.X, camerapos.Z );
vector2df diff = camera - character;

//Get the angle in radians and convert to degrees
f32 angle
= atan2( diff.X, diff.Y ) * 180/3.14;
I'm not sure if setting the y rotation to that value will get you exatcly what you want, you might need to tweak the final angle by for example, fixed 180 degrees. If I'll have the time I will check this out.
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

That works, thanks!
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Post Reply