# Getting right sprite direction in 3D world - like FFT or Xenogears

• 05-10-2003
rmullen3
Getting right sprite direction in 3D world - like FFT or Xenogears
This is a problem I have been trying hard to figure out and I can't. You know how in Final Fantasy Tactics and Xenogears the characters are billboarded sprites that change "directions" (flat images of the sprite facing certain directions) depending on the camera?

So, given a position of a sprite and a point that sprite is facing (the point is rotated around the sprite by 0,45,90,135,180,225,270,315 degrees), and given a position of a camera and a point the camera is facing (a point rotated around the camera by 0 to 359 degrees), how can I find the correct "direction" image (0 - 7) for the sprite? What relationship between these points / angles needs to be found?

If anyone can help me or give me a hint, tip, or tutorial, I'd appreciate it. I've tried various things - law of cosines, etc, but I'm still not sure what thing exactly I'm looking for, so my attempts have always been messed up.

Thanks!

(ps I have all the following information about the sprite and the camera:
sprite position
sprite angle
point sprite is facing (position.x + sin (radangle), position.z + cos (radangle))

camera position
camera angle
point camera is facing (ditto)
)
• 05-10-2003
MrWizard
All you need is the dot product. We will use the vector the camera is facing and the vector the sprite is facing ( also normal to sprite billboard plane ).

Code:

```float fDot = DotProduct3D( vCameraFace, vSpriteFace ); float fAngle = acosf( fDot );```
This will give you the angle between the two vectors. Then based on certain ranges of that angle you can choose which image to display.
• 05-10-2003
rmullen3
~
Alright heres my code:

Code:

```                const float radAngle = DEG2RAD (xAng);                 const float radCam = DEG2RAD (CameraAngle);                 vector3d D (pos + vector3d(pos.x * sin(radAngle), pos.y,                         pos.z * cos(radAngle)));                 vector3d R = (CamPos + vector3d(CamPos.x * sin(radCam), CamPos.y,                         CamPos.z * cos(radCam)));                 float fDot = Dot(R,D);                 float fAngle = acosf(fDot);                 fprintf (ff, "%f %f \n", fDot, fAngle);                 dir_ = ((int)RAD2DEG(fAngle) / 45) & 7;```
xAng is the sprite angle in degrees, radAngle is the sprite angle converted to radians, CameraAngle is the camera angle in degrees, radCam is it in radians.

D is the point the sprite is facing, R is the point the Camera is facing. (Am I calculating these wrong?). As shown, I calculated the dot product of the camera face-point and the sprite-face point and the arccosine of that dot product.

However, the fprintf statement shows that, after being calculated, fAngle is always equal (in floating point) to -1.#IND00... a really tiny number.

Is this from calculating the points the camera and sprite face incorrectly? From getting degrees/radians mixed up? Thank you very much for your help!

Ps "Dot" is defined as:
Code:

```__inline float Dot (const vector3d &v1, const vector3d &v2) {         return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z); }```
as far as I know, Dot() and all of the other vector3d routines I wrote function as they should.

Thanks!
• 05-12-2003
rmullen3
~
Sorry to bump... but can anyone help me further? If not, that's okay