#### Archived

This topic is now archived and is closed to further replies.

# Getting the right angle for 2D sprites in a 3D world - Can YOU help?

This topic is 5377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay, I guess in my days as a programmer I've finally come upon my first true ROADBLOCK, that I can't figure out and after asking on several forums can't find the answer either. Essentially, the effect I want is used in games like Xenogears and Final Fantasy Tactics - where a billboarded quad's texture represents a sprite, and the angle shown changes for the characters as the camera is rotated. Given four points: Character Position, Point Character's looking at, Camera Position, and Point the camera is facing... how can you determine what the right direction (8 directions) is for the character's sprite? I think it has something to do with finding the angle between two of those vectors... (dot product)... but which ones!? How would I go about it? Is this method right at ALL? Can anyone help me with this? It would be awesome, as I really want to learn, and after ten or so different functions and ways of calculating it I'm next to pulling out my hair... =( Thank you for any help. cheers, rm3 (p.s. although this may be a stupid note, don't make the same mistake I initially did... after playing so much Xenogears I only tried to work on finding the right angle for the 1 character the camera rotates around, instead of all the characters in the scene) [edited by - rm3 on September 3, 2003 10:46:32 PM]

##### Share on other sites
theoretically couldn''t you draw a different picture depending on what rotation the view is at?

for example: 0-20 degrees: draw front facing.....
20-40 draw right facing, or whatever, and etc.....

i don''t know if that''d work, but if you haven''t tried yet, you should give it a try.

Link //For the latest on all of the IVGDA projects!

##### Share on other sites
Yes, that is how you do it - the player can face 0, 45, 90, 135, 180, 225, 270, 315, or 360 degrees around his position, and his degree angle is divided by 45 to get a number which determines what direction of the sprite is shown.

However, depending on the direction he is facing (he''s not always going to be facing right) and the camera position relative to him, the exact angle is different.

Look at the game Xenogears (I know that not a huge slice of PC-mainly users have ever played this fantastic game, but..). In the game, for example, Fei is facing forward and the camera is behind him. You see his back. If you turn right, you see his right side... but if after turning right, you rotate the camera around him what you see differs. What direction he moves in and what sprite angle you see is related all to the camera. Other NPCs in the scene also follow this relationship... the angle that is shown depends first on their angle relative to their position, than the relationship between that angle and the camera position/where the camera is facing.

It''s not a terribly difficult problem, just confusing for me.
Thanks for your input though = )

##### Share on other sites
Why don''t you just do angle_the_camera_is_facing - angle_person_is_facing and then do what jverkoey said?

##### Share on other sites
You need more than a dot product.

Subtract player pos from camera pos.
Project to the plane described by the "up" vector and 0 distance.
Use atan2() on x/z to find the [-180,180) angle from player to camera.

##### Share on other sites
"Subtract player pos from camera pos.
Project to the plane described by the "up" vector and 0 distance.
Use atan2() on x/z to find the [-180,180) angle from player to camera."

You lost me... Sorry.

So with my vector dif (player pos from camera pos), what do I do? How does the character angle play into this equation? Do you mean x over z, or atan2() on x and z? How do I define that plane... and "project to it"?

Hasn''t somebody written something about this? I''ve never seen this effect reproduced in any homebrewn games... unfortunately...

I apologize for not know enough about 3D math to translate that into code. I wish the answer was something as simple as cam angle - player angle...

Thank you everyone!

##### Share on other sites
This sounds like billboarding to me: like what they used in the original DooM games? This liknk might be useful (DX8, VB6):

Tutorial (cover maths behind it too)

##### Share on other sites
I already understand and have implemented billboarding, but thank you for the article, I'll read thru it.

The problem is one of a relation I can't seem to understand, and every time I've tried to get help, unfortunately, I receive a wide range of answers... none of which work, or atleast, none of which I have been able to get to work.

Here is the basic code for my engine (requires OpenGL). This little (poorly programmed- there's tons of sillily put together classes and functions) engine displays a very basic 3D level, and scattered about is a bunch of billboarded sprites.

In x_entity.cpp is the most important function, entity::calc_face(). It doesn't do anything at all... but I need to make it get the right angle for the character to show.

Basically, each frame the entities calculate the point they're looking at, the camera calculates the point it's looking (at the player moves the camera around, rotates it, etc), and then the entities are drawn in the world. When they're drawn, they're supposed to calculate the right angle that the camera would see.

Anybody who would like to see please download the following winzip file. If you have any questions about anything in the code, please ask me and I will try to answer. I really want to learn how to do this, but I don't think I'll get it without someones help, unfortunately. Anyway, here's the link to the code:

XEngine

[edited by - rm3 on September 6, 2003 3:50:41 PM]

• 10
• 17
• 9
• 14
• 41