Sign in to follow this  
i__h4x

Rotate player to object

Recommended Posts

How can I go about calculating the amount that i need to rotate the player to face an object? The players view angles are given as (x,y) where x is up/down and y is left/right. I need to calculate from knowing the origin of the player (x,y,z), where its facing, and the origin of the object to face (x,y,z) how much to increase/decrease x and y. Thanks!

Share this post


Link to post
Share on other sites
Ok I'm not sure I follow your description of the players angles of (x,y). In a 2d game you would have a single YAW angle in the XY plane. When you add a third dimension PITCH comes into play.

First construct a vector from the player to the target. i.e. Diff(xyz) = Targ(xyz) - Player(xyz).

Then you can use ATan2 to calculate the YAW angle using atan2( diff.x, diff.z ). Finally calculate the pitch using asin( diff.y / magnitude(diff) )

That will give you two angles in absolute terms. If you set the players pitch and yaw to those values it should face in the direction you want.

Share this post


Link to post
Share on other sites
Minor issue, im using a scripting language, and it has no built in math functions.
Working on implementing atan2 using CORDIC... Should be fun
http://www.worldserver.com/turk/opensource/index.html#CORDIC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this