
-----------------------------------
Fashoomp
Mon May 21, 2007 3:53 pm

Collision Detection Problem
-----------------------------------
For my racing game, i am trying to figure out collision. I found a sample code just for movement, and im trying to change it for collision. Its a ball that moves on screen. I drew a line on screen, and was wondering if anyone can show me how to make the ball not able to pass through that line. Heres the code setscreen ("graphics:800;600,offscreenonly") 

var x, y : real 
var vx, vy : real := 0 
var key : array char of boolean 
x := 400 
y := 300 

loop 
drawline (maxx div 2, maxy div 2-100, maxx div 2, maxy div 2+100, 7)
    Input.KeyDown (key) 
    if key (KEY_UP_ARROW) then 
        vy += 0.2
    elsif key (KEY_DOWN_ARROW) then 
        vy -= 0.2
    end if 
    if key (KEY_RIGHT_ARROW) then 
        vx += 0.2
    elsif key (KEY_LEFT_ARROW) then 
        vx -= 0.2
    end if 
    if key (' ') then 
        vx := 0 
        vy := 0 
    end if 
    x += vx 
    y += vy 

    if x > maxx-5 or x < 0+5 then 
        vx := -vx 
        x += vx 
    end if 
    if y > maxy-5 or y < 0+5 then 
        vy := -vy 
        y += vy 
    end if
    vx *= 0.99
    vy *= 0.99
    drawfilloval (round (x), round (y), 5, 5, blue) 
    View.Update 
    delay (10) 
    cls 
end loop


-----------------------------------
Carey
Tue May 22, 2007 8:45 am

Re: Collision Detection Problem
-----------------------------------
You could use whatdotcolour to check if you are touching the line, or you could check the distance from the line with Math.Distance. If the distance from the line is equal to the radius of your ball then bounce.
