Distance between 2 points in 3d space
Lets say i have a point Ax Ay Az
and another point Bx By Bz
How do i find the distance between these 2 points?
DX= Bx -Ax
and so on...
There aren''''t problems that can''''t be solved with a gun...
and so on...
There aren''''t problems that can''''t be solved with a gun...
There aren''t problems that can''t be solved with a gun...
dx = ax-bx
dy = ay-by
dz = az-bz
distance = sqrtf(dx*dx+dy*dy+dz*dz)
[edited by - swx on June 29, 2003 6:57:57 AM]
dy = ay-by
dz = az-bz
distance = sqrtf(dx*dx+dy*dy+dz*dz)
[edited by - swx on June 29, 2003 6:57:57 AM]
Is there a faster way?
I need to use this several times per frame. I heard using sqrt is pretty expensive.
I am already using quite a number of sqrts for ray-triangle intersection every frame.
I need to use this several times per frame. I heard using sqrt is pretty expensive.
I am already using quite a number of sqrts for ray-triangle intersection every frame.
June 29, 2003 06:38 AM
There isn''t another way to calculate the distancee between two points.
What you could try to do is to approximate the square function.
I''ve read an article previously but I can`t say anything about speed.
If you want to try it by yourself, here''s the link:
http://www.flipcode.com/articles/article_fastdistance.shtml
Greetings,
Adam
What you could try to do is to approximate the square function.
I''ve read an article previously but I can`t say anything about speed.
If you want to try it by yourself, here''s the link:
http://www.flipcode.com/articles/article_fastdistance.shtml
Greetings,
Adam
sqrt is not expensive for some calls per frame. its expensive for some million calls per frame (coding an own raytracer for example). just use it.
you''ll find out later if it really is a bottleneck, and then optimize.
just one question? do you need the actual distance, or do you need to compare two distances? because you don''t need to sqrt them both then, just use the squared distances directly.
"take a look around" - limp bizkit
www.google.com
you''ll find out later if it really is a bottleneck, and then optimize.
just one question? do you need the actual distance, or do you need to compare two distances? because you don''t need to sqrt them both then, just use the squared distances directly.
"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
I need them for bounding spheres or bounding circles since i cant jump in my game.
Since i am going to compare the distance against the sum of the two radii, i think it might be necessary to use it.
if(Distancereturn true;
Hmm, since i only need to know if it is less, is it possible to do it this way assuming i have not used squareroot on the Distance.
if(Distance<((Aradius+Bradius)*(Aradius+Bradius)))
both radius are going to be positive values which can be precalculated.
Since i am going to compare the distance against the sum of the two radii, i think it might be necessary to use it.
if(Distance
Hmm, since i only need to know if it is less, is it possible to do it this way assuming i have not used squareroot on the Distance.
if(Distance<((Aradius+Bradius)*(Aradius+Bradius)))
both radius are going to be positive values which can be precalculated.
Dont worry about the distance formula eating to much process power. In a game of mine it gets used sevral thousand times a frame, It''s not much to worry about, if your concerned you could write your own sqrt function that stops a a low precission.
The problem is that my target machine is a 450mhz CPU. When i have a scene with 14k polygons, it slows down to about 30fps.
Previously without any collission code, it could handle 18k-19k polygons in a scene at 60fps.
I only use the sqrt for polygons which are very close to the intersection point. This means about less than 5 per frame.
But to determine if the polygon is close to the intersection point, i use about 8 if statements comparing more than and less than values to determine if a polygon should be tested.
Which is faster? 8 if statements or using the addition of angles method from intersection point against all vertices in the triangle.
Now i wish to use a bounding sphere around all my objects, so if the whole object is far away from the user, all polygons in that object will not be tested. Since my levels are not huge, i figure this suits my game and is easier than using octrees.
Previously without any collission code, it could handle 18k-19k polygons in a scene at 60fps.
I only use the sqrt for polygons which are very close to the intersection point. This means about less than 5 per frame.
But to determine if the polygon is close to the intersection point, i use about 8 if statements comparing more than and less than values to determine if a polygon should be tested.
Which is faster? 8 if statements or using the addition of angles method from intersection point against all vertices in the triangle.
Now i wish to use a bounding sphere around all my objects, so if the whole object is far away from the user, all polygons in that object will not be tested. Since my levels are not huge, i figure this suits my game and is easier than using octrees.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement