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

Username:   Password: 
 RegisterRegister   
 Collision Detection Problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Fashoomp




PostPosted: Mon May 21, 2007 3:53 pm   Post subject: 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
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
Sponsor
Sponsor
Sponsor
sponsor
Carey




PostPosted: Tue May 22, 2007 8:45 am   Post subject: 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.
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  [ 2 Posts ]
Jump to:   


Style:  
Search: