Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 little fuzzy on Math.DistancePointLine
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Fri Jun 30, 2006 5:25 pm   Post subject: (No 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).
Clayton




PostPosted: Fri Jun 30, 2006 6:26 pm   Post subject: (No subject)

how would you do that for say two circles? would it look something like this?

Turing:

View.Set ("graphics,offscreenonly")

var x1, x2, y1, y2 : int
var xv, yv : array 1 .. 2 of int := init (1, 1)
const RADIUS : int := 20

x1 := maxx div 2
y1 := maxy div 2
x2 := 50 + RADIUS
y2 := 25 + RADIUS
yv (2) := -1

procedure wall_collision (xp, yp, ball : int)
    if Math.DistancePointLine (xp, yp, 0, 0, maxx, 0) <= RADIUS then
        yv (ball) *= -1
    elsif Math.DistancePointLine (xp, yp, 0, maxy, maxx, maxy) <= RADIUS then
        yv (ball) *= -1
    elsif Math.DistancePointLine (xp, yp, 0, 0, 0, maxy) <= RADIUS then
        xv (ball) *= -1
    elsif Math.DistancePointLine (xp, yp, maxx, 0, maxx, maxy) <= RADIUS then
        xv (ball) *= -1
    end if
end wall_collision

loop
    Draw.Cls
    Draw.FillOval (x1, y1, RADIUS, RADIUS, brightred)
    Draw.FillOval (x2, y2, RADIUS, RADIUS, brightgreen)
    View.Update
    %collision between balls)
    if Math.DistancePointLine (x1, y1, x2, y2 - RADIUS, x2, y2 + RADIUS) <= RADIUS then
        yv (1) *= -1
        yv (2) *= -1
    elsif Math.DistancePointLine (x1, y1, x2 - RADIUS, y2, x2 + RADIUS, y2) <= RADIUS then
        xv (1) *= -1
        xv (2) *= -1
    end if
    x1 += xv (1)
    y1 += yv (1)
    x2 += xv (2)
    y2 += yv (2)
    wall_collision (x1, y1, 1)
    wall_collision (x2, y2, 2)
    Time.Delay (5)
end loop


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?
NikG




PostPosted: Sat Jul 01, 2006 2:07 am   Post subject: (No 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:
code:
if Math.Distance(Ball1x,Ball1y,Ball2x,Ball2y) <= BallRadius*2 then
   ...
end if


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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: