Sign in to follow this  

Calculate angle between two 3D lines

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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