SQRT(); Another Option? give me your ideas....

Started by
54 comments, last by OpenGL_Guru 19 years, 10 months ago
quote:Original post by Tree Penguin
Stop there! Summing the distances of the dimensions does NOT give you the 3d distance, not even when you''d do it in 2d, take a look at this:
...
Anyway even if you do need the distance in 1d that piece of code you gave is really slow, it can be replaced with this:

distance=x1-x2;
distance=max(distance,-distance);

or this:

distance=max(x1-x2,x2-x1);

For my piece of code, it just finds each distance between two points - it isn''t made to find the distance between two points that are on two different planes. The assumption is made that if you are trying to find say x1 and x2, you are assuming that y1,y2 and z1,z2 are the same. There is no 2D/3D vector conversion there.

You are right about it being limited to 1D but I was just presenting the code for finding the distance between two points assuming only 1 value changes within each point''s faction.

For the max function, I did not know of that function - thank you for pointing that out to me. Does that function also take negatives into account, say if x2 is negative and/or x1 is negative?
Advertisement
max just returns the highest value, min the lowest.

EDIT: i think it is in doubles

[edited by - Tree Penguin on May 31, 2004 11:21:16 AM]
lots of good ideas but i think i can do some kinda fast math and not waste too many cycles. as one person just say, with a 3 Ghz machine you might have enough cycles to waste...i am gonna go test and see what i can work with..
heh
I''m lazy and haven''t read all of the posts, but in case this hasn''t been mentioned jet:

You can avoid the square root in colission detection by simply squaring the size of the objects being checked for.

Meaning:

if (Squareroot(distance) < object1radius + object2radius)
colission


is the same as


if (distance < object1radius² + object2radius²)
colission



Squaring is much faster than square-rooting.
quote:Original post by FiveFootFreak
if (Squareroot(distance) < object1radius + object2radius)
colission


is the same as


if (distance < object1radius² + object2radius²)
colission

Just nitpicking. (a+b)^2 is not a^2 + b^2. It is a^2 + 2ab + b^2


You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
Yeah, it seems like FiveFootFreak is not that good at math
Anyway, his idea was not completely bad.
Actually:
distance = sqrt(x^2+y^2)

is the same as:
distance^2=x^2+y^2

This way you would avoid the square root.
Unfortunately, having read the other posts about a week ago, I'm not sure if this is really something you might be interested in.

[edited by - lazork357 on June 9, 2004 5:12:55 PM]

This topic is closed to new replies.

Advertisement