🎉 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!

Adding squared values

Started by
4 comments, last by frob 5 years, 9 months ago

Not a lot of activity in this forum, except for all of my dumb questions.

This one is very basic. Something I should probably already know the answer to. I'm wondering if it is possible to add two squared values together in a way that produces its squared result, or if there is any way to modify the inputs of an operation to produce the desired (squared) result.

For example 3 + 3 = 6, but 3*3 + *3*3 != 6*6.

If I have two squared distances, is there a clever way to combine these into the sum of their roots or the sum of their squares without using sqrt?

The reason I'm wanting to do this is to compare two squared distances while adding a radius to one of them. Is there any way to do this without finding the roots first?

if( dist_sq < len_sq + RADIUS_SQ ) // won't work - cannot extend either side by radius without sqrt?

Thanks for any advice

Advertisement

First write your equation in the 'non-squared' form and then square it to get rid of the root. E.g.

distance = sqrt( x^2 + y^2 ) < max_dist + epsilon

<=> x^2 + y^2 < ( max_dist + epsilon )^2

 

So you see that you need to add first and then square the result instead of adding the squared values.

 

Unfortunately, I wouldn't have access to the values before they were squared. They are computed as distances between points in a cluster of points (spheres). Currently, I am using sqrt to compute the distances, but was trying to avoid using sqrt for every point distance test by comparing squared distances. Comparing distances in squared form usually works, but apparently not for spheres?

Thanks for your advice

I think I did come up with an alternative. Instead of adding the radius to one of the distances, I can add the radius of both spheres together, square that, then just make sure the squared distance between the centers is greater or less. I know my original question didn't have enough information for anyone to be able to point this out, so that was my bad.

Working with already-squared values is commonplace in game math libraries.

For example, when computing distance between two objects it is common to have a DistanceSquared() function.  That's from the classic Pythagorean formula a^2+b^2=c^2, which can apply in 3D as well.  Instead of computing the distance c as sqrt(a*a+b*b) they ignore the square root. If you want to see if the points are closer than distance d, you'd compare against the distance squared (d*d) instead. 

This topic is closed to new replies.

Advertisement