🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to check in which direction angle player is looking?

Started by
2 comments, last by _WeirdCat_ 4 years, 2 months ago

Hello.

I would like to ask you, if we have a player his coordinates: X, Y, Z and float angle how can we check if looking for a direction and for example “spawn” something at coordinates where player is looking?

Advertisement

Hey,
With only one angle, you can convert to a 2D unit vector, the 2D direction:

x = cos(Angle)
y = sin(Angle)

For a 3D Vector, you will need 2 angles, pitch and yaw, then convert using a similar way:

cosPitch = cos(pitchAngle)
sinPitch = sin(pitchAngle)

cosYaw = cos(yawAngle)
sinYaw = sin(yawAngle)

directionX = cosPitch * cosYaw; 
directionY = cosPitch * sinYaw; 
directionZ = sinPitch;

Once you have this unit vector you can spawn something at the distance you want.

having these sin x cos y thingy(call it vecP) and check point(CP) and the point you want to look at(LP): you calculate the dot of two vectors: positon dot(vecP, Normalize( vectorAB( LP, CP) )) and if its negative then its facing.. additionally it cant be smaller than -1

This topic is closed to new replies.

Advertisement