Advertisement

Need help with collision detection

Started by June 28, 2003 06:30 AM
4 comments, last by ACAC 21 years, 7 months ago
I drew it out any help would be greatly appreciated! Thanks Ash.
Check out this website:
http://geometryalgorithms.com/Archive/algorithm_0102/algorithm_0102.htm#Distance%20to%20Ray%20or%20Segment
At the bottom the actually have source code, but it is kinda confusing, read the discussion then take a look at the function. If the | distance | is < the radius it is interseting, if it is eqaul then they are touching. Hope this helps.


~Evlich
~Evlich
Advertisement
Hi ...

I had the same problem with my collision detection ...
try this : http://www.peroxide.dk/download/tutorials/tut10/pxdtut10.html

It''s for 3D-Collision (Sphere->Triangle), but I think you can use the same principles for 2D.

Hope it helps
Funny thing is I bumped into the 3D version of this problem tonight (comprehend Sphere/Cylinder collisions) and I figured out my own algorithm to solve it.
Basically you look for the closest distance between the circle and the line and check it with your radius; if it's smaller then obviously there were collision.
Here's how you figure out the distance;
Make a vector starting Px2, ending at Px1. Don't forget to normalize it (dunno what happens if you omit that part though)
Find it's magnitude (length = sqrt(x*x + y*y));
Now compute the cross product between that vector and vector ( px1 , px3 ).
Get the latter's magnitude and divide it by vector ( px1,Px2 )'s length.
Call that d; if d
  public int check_distance(ball_info b, cylinder_info c, double radii){    Vector3f c_2_b = new Vector3f();    Vector3f cross = new Vector3f();    c_2_b.sub(c.start,b.location);    cross.cross(c.direction,c_2_b);    float disttance_c_b = vector_length_2(cross)/c.length;    return (disttance_c_b<= radii) ? 1 : 0;  }  


[edited by - JavaCoolDude on June 29, 2003 2:47:23 AM]
One inefficient but fun way to solve the problem that comes to mind, would be to transform the coordinate system that contains the line to where the line has a slope of 0, then its just a matter of:

sqrt( (circle'sXorigin - circle'sXorigin)^2 + (circle'sYorigin - 0)^2 )

which equals:

circle'sYorigin


If that value is less than or equal to the radius of the circle, then the objects are intersecting. I've never seen this way of detecting an intersection and just thought of it while looking at the picture, going to go play with it a bit and see if it actually works.

EDIT: This method would be extremely effective with a system that used exclusively parametric lines. Hmm... is this why Wolf3d used parametrics?


[edited by - haro on June 29, 2003 3:11:30 AM]
Whoa, it works like a charm.

I translate the given system so that P( x2, y2 ) is at the origin of my system. Now, I make a vector that extends to P( x1, y1 ) and another vector that extends to P(1, 0). Take the dot product between these two vectors. Now rotate the entire system along the z axis, that many degrees. Now do as mentioned earlier. If the absolute value of the circle''s origin''s Y value is less than or equal to the radius of the circle, there is a collision. simple as that.

This topic is closed to new replies.

Advertisement