Computer Science Canada little fuzzy on Math.DistancePointLine |
Author: | Clayton [ Fri Jun 30, 2006 4:20 pm ] |
Post subject: | little fuzzy on Math.DistancePointLine |
ok, i understand that Math.DistancePointLine finds the closest distance between a point and a line segment, however, i dont quite understand the parameters, is xp and yp the point on the perpindicular to your line segment? if it is, how would you use that in collision detection? |
Author: | Cervantes [ Fri Jun 30, 2006 5:25 pm ] |
Post subject: | |
Yes, (xp, yp) would be the external point. Say you've got a line segment and a circle. Using Math.DistancePointLine, and giving it the coordinates of the ends of the line segment and also the centre of the circle, you can determine if there is a collision between a line and a circle (if the value returned by Math.DistancePointLine is less than the radius of the circle). |
Author: | Clayton [ Fri Jun 30, 2006 6:26 pm ] | ||
Post subject: | |||
how would you do that for say two circles? would it look something like this?
or if not, how would you go about doing this so that it can find the distance whenever it may happen, for example in that program, the balls will overlap a bit before they bounce, how can you fix that? |
Author: | NikG [ Sat Jul 01, 2006 2:07 am ] | ||
Post subject: | |||
For circle/ball detection, you don't need Math.DistancePointLine, Math.Distance will suffice. All you have to do is check the distance (using Math.Distance) from one point to another, and check if that distance is less than or equal to the ball's radius. So for two balls, you just double the radius:
And just a note to help you out, try to use better variable names. x1,y1,x2,y2 can become confusing the longer your program gets, so why not try and use naming styles like in my code above or better yet, create your own variable types. |