# How do I find the angle between two 3D angles represented in this form?

I have two 3D angles, represented by [a1,a2] [a1,a2] where the a2 is < 180. My coordinates are represented as [X,Y,Z], where X,Y,Z are 64 bit ints (longs in my lang). My direction angles are represented as (Angle1, Angle2) where Angle1 is 360 degrees along the X/Y plane, and Angle2 is 180 degrees along the Z axis.

In this case, I know for a fact that both angles were calculated from a single starting point, so there should be a single angle between them. However, the math eludes me. I sure would appreciate some help.

I've read the stuff on calculating the angle between two vectors, which is great, but for me would involve multiplying two longs together, which is 1. not practical, and 2. shouldn't be necessary.

My goal is to determine whether obj1 is moving generally towards obj2. One of the angles is the direction of movement of obj1, and the other is the direction from obj1 to obj2. Can I get there from here?

• It sounds like you are working with vectors. What is wrong with calculating the angle between those two vectors? Aug 15, 2014 at 16:26
• 1. It's not practical because I'm using every bit of my long ints, which means moving to more expensive (cpu wise) structs, in a method that is called often. 2. It's not necessary because I have the two angles at this point in the code anyway. Aug 15, 2014 at 16:33
• Can you elaborate more on how you store/represent those angles, and what you mean by [a0, a1]? (Are they angles in the XY and YZ planes, for example?) It sounds like you want to avoid the multiplications because you're concerned about overflow (you're "using every bit" of the integers now), is that correct?
– user1430
Aug 15, 2014 at 16:38
• For that matter, why are you using 64 bit integers to represent what would probably (or at least traditionally) be unit vectors? Aug 15, 2014 at 16:42
• Given your current data model, I don't see how you can get whether or not obj1 is generally headed towards obj2 (using a threshold) without using even more costly sinusoidal functions. Since cpu cost is an issue, I'd highly recommend trying to change the data model if possible. Aug 15, 2014 at 18:58

A possible solution is to use the Dot Product. Of course, you need two vectors and not two angles, but I guess you're using them (otherwise I wouldn't explain how you're having 3D angles).

Quoting Van Verth & Bishop from the book Essential Mathematics for Games & Interactive Applications, page 30-31:

A more common use of the dot product is to test the angle between two vectors. If v·w > 0, then we know the angle is less than 90 degrees. If v·w < 0, then we know the angle is greater than 90 degrees, and if v·w = 0, then the angle is exactly 90 degrees. [...]

For example, suppose that we have an AI agent that is looking for enemy agents in the game. The AI has a view vector v and a vector t which points toward an object in our scene. If v·t < 0, then the object is behind us and therefore not visible [...]

That could solve your current problem because outside that range, your objects probably won't collide, but you also need to consider the distance between them for that statement to be true.

• I would love to, but finding the dot product means multiplying coordinates together. In my case, it means pulling together data structures called BigInts, which are expensive cpu-wise. This method is called very often. If that is, literally, the ONLY way to possibly do it, then great. However, surely it can be done with the information given... :( Aug 15, 2014 at 17:00
• @user50612 I've used this with floating point variables in Unity and XNA with C# and the book works with C++. I don't know how BigInt is needed here =( Aug 15, 2014 at 17:08
• It sounds to me like it's needed because that's what his data model currently requires -- if he can change his data model, that would be one thing, but he's given no indication that he can. It is important to respect that.
– user1430
Aug 15, 2014 at 17:25
• The expensive part wouldn't be using BigInts or any coordinates, as those actually dont need to be used. What you would need to do is convert the angle representation into direction vectors (no need for world coords) but that requires trig. Once you have them as direction vectors, take the dot product. Then your angle is the arccos of said product.
– vero
Dec 10, 2016 at 6:56