# Dealing with angles in 3D

In 2D everything is easier to understand. I'm not enought experienced to join people who say that 3d is the same: you JUST need to add a dimension - THAT adding a dimension is my problem, bacause I can't easily visualize (or draw on paper) the situation I'm dealing with. So please don't say it is the same.

First let's talk of a concrete situation.

Use case: bounding sphere intersection

I have two entities colliding with theirs bounding sphere intersecting at a certain point. Let's consider two vectors, the velocity of the first entity v1 and distance between boundingsphere centers d. If I want to get v1 component along d I may want to do

V1 * Math.Cos(alpha)


with alpha = Math.Atan2(...)

a. My questions come now. Math.Atan2(...) takes 2 arguments, the first is the difference between Xs and second is the difference between Ys of the two vectors involved. So, since I'm working in 3D and not in 2D, should I assume that common practice is to consider a pair of dimenstion at time when dealing with angles in 3D? Since I'm a bit confused here, can someone tell me what's the best practice to follow in this case? (details would be appreciated here).

b. Then I have some information I'd like someone to confirm. When my double angle value is returned from the Math.Atan2(...), I have my angle in radians. I read somewhere that this float value varies between +- Pi, reasonable. And also that Math.Atan2(...) always consider the lesser between the 2 angles, which is not important if you need to calculate Cos(angle) but you must keep it in mind if you wants to get the Sin(angle). Is this how things work?

• It's a good rule of thumb that, if you are using trigonometric functions (sin, cos, tan, atan2, etc), you are probably doing something wrong. If you are passing the output of atan2 directly into sin or cos (converting to an angle and back), then you are definitely doing something wrong. In both 2D and 3D games you should prefer linear algebra approaches, as per Steve H's answer. – Andrew Russell Feb 12 '14 at 1:20

Instead of an approach that relies heavily on trig (ie. your Atan2) as a means to solve the problem, 3d lends itself to a more linear algebra approach.

float v1ComponentAlongD = Vector3.Dot(v1, d); // look ma, no angles


Check out the last two paragraphs in Shawns blog here: http://blogs.msdn.com/b/shawnhar/archive/2010/02/12/doing-math-in-2d-vs-3d.aspx

So really I would recommend not wanting an answer to your a & b. Instead, get comfortable migrating away from trying to relate every problem to angles and instead relate it all to vector math (mainly the dot product and the cross product).

Generally in 3d, you won't get completely away from angles, but it sure is easier to do 3d when you can lean heavily on linear algebra and less on trig.

• Commentary imported to chat. – Steve H Feb 13 '14 at 13:06
• Please avoid having extended discussions in comments. – user1430 Feb 13 '14 at 16:16