# Calculate angle between two 3D lines

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

## Recommended Posts

Hi I'll try to explain this the best I can I have a small sphere inside a larger sphere. Between them I have several sparks moving(a rotation with its origin in 0,0,0). I want that when one of the sparks collides with another, they will merge into one and another will be generated elsewhere at random. My idea for a low resource collision detection is to calculate the angle between the lines defined by the starting and end point of each spark, compared them and, if the angle is inferior to a given value, I have a collision. If I'm thinking this right, how does one calculate the angle between two lines in 3D? Thanks

##### Share on other sites
You use the usual DotProduct formulae.

|A.B| = |A||B|Cos(a)

Where A and B are your vectors and (a) is the angle between them.

If you already have the vectors A and B then

Cos(a) = |A.B| / |A||B|

So if you normalize the A and B vectors, their lengths will be 1 so |A| = 1 and |B| = 1 and |A||B| = 1 so for normalized vectors:

Cos(a) = |A.B|

Expanding:
cos(a) = AxBx + AyBy + AzBz;

And thats all...

Luck!
Guimo

##### Share on other sites
Unless you are already storing your sparks in a form that contains angles it will almost definitely be faster to do a squared distance check then it would be to perform any trig functions. However, if your just comparing the angle to a threshold then you can use the dot product approach above and just never take the cosine in your inner loop (instead just compare the dot product to the precalculated inverse cosine of the desired angle). The normalization step would still involve a square root though, so I still doubt that there would be any benefit of this method over the standard distance formula.

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013162
×