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

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    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)
    )
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    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!
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Sorry to bump... but can anyone help me further? If not, that's okay
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  2. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM
  3. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 12:40 PM
  4. Camera rotation/movement in 3D world
    By tegwin in forum Game Programming
    Replies: 11
    Last Post: 01-24-2003, 01:43 PM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 11:19 AM