Hi! How do I phigure out range between 2 objects?....

Started by
8 comments, last by XEN 22 years, 3 months ago
Hi hello! Im new here. What I wantd to know was the phoremula phor how to get the distance between 2 objects. What I mean is, I want to create 2 phunctions that will return the distance between 2 objects. The phirst phunction named range2d() would phigure out the range in 2dimension (x,y), and would take the x y vectors of 2 objects. The second phunction would phigure out the range in 3dimension (x y z) would take the x y z vectors of 2 objects. So, anyone care to help the newbie? Edited by - XEN on December 16, 2001 1:05:38 AM
Advertisement
This is elementary math. Given Ax, Ay, Bx, and By with A and B as two points you can do this:
Distance = sqrt((Ax - Bx)2 + (Ay - By)2)

Given the additional data of Az and Bz you do do this:
Distance = sqrt((Ax - Bx)2 + (Ay - By)2 + (Az - Bz)2)

[Resist Windows XP''s Invasive Production Activation Technology!]
well phrist you should phigure out how to spell correctly...
just an ph-y-i...
your "f" key is located between the ''d'' and ''g'' on your keyboard.



-eldee
;another space monkey;
-eldee;another space monkey;[ Forced Evolution Studios ]
Mayb you should''ve explained it in idiot terms? Sorry, I really don''t know that much about math. So I should do something like this:



int range2d(int x1, y1, x2, y2)



{




int distance = ((x1 - x2)^2 + (y1 - y2)^2)^3;



return distnance;


}



Right?
No, that''s not correct (close though ). The carrot (''^'') is for exclusive or in C/C++. Here''s a Range2D function (I''m sure you can figure out 3D from this):
  #include <math.h>int Range2D(int AX, int AY, int BX, int BY) {  int DeltaX = AX - BX;  int DeltaY = AY - BY;  return (int)sqrt((DeltaX * DeltaX) + (DeltaY * DeltaY));}  

However, you may want to use floats instead of integers.

[Resist Windows XP''s Invasive Production Activation Technology!]
Thankx!
you spelt phloat wrong


Beer - the love catalyst
Beer - the love catalystgood ol' homepage
kiddies cant spell these days... it''s the phall of western civilization...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
i noticed that mostly only very simple questions get answered here.
the question was about the distance between two objects
if points are the only things that are considered to be objects
they the subject would be fine but it''s not
Answers to complex questions usually require a multi-post dialog, since people on the forums don''t necessarily have time to hash out a full answer all at once.

I''ll partially address the issue of finding out the distance between two arbitrary objects. It is a minimization problem to find the two points on the objects that represent the closest distance between the objects. Its a constrained problem because the dot product of the normals at those two points has to be negative (so the range is found between parts of the objects that face each other).

A brute force way to do this for polyhedra is to just walk through all possible pairs of vertices or faces between the objects. Too slow too slow. Next step would probably be to do checks between partitions of the objects, such as using OBB trees to narrow the search down to one or two triangles on each object.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

This topic is closed to new replies.

Advertisement