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

Username:   Password: 
 RegisterRegister   
 collision detection
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jonos




PostPosted: Sun Feb 01, 2004 4:47 pm   Post subject: collision detection

im trying to make a game, but i didn't understand the collision detection tony, dan, and some other people are talking about in tutorials, so i decided to try to make my own. im not sure if this is the right way to go about it, or if this way will cause problems with trying to do some things in the game. it works, but im not sure if it the right way to do it...

code:

var chars : array char of boolean
var circlex, circley : int := 100
var radius : int := 20

colorback (black)
cls

loop
    setscreen ("offscreenonly")
    View.Update
    drawfillbox (20, 20, maxx - 20, maxy - 20, brightred)
    drawfilloval (circlex, circley, radius, radius, black)
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        if circley + radius > maxy - 20 then
            circley := circley - 1
        else
            circley := circley + 1
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if circley + radius < 60 then
            circley := circley + 1
        else
            circley := circley - 1
        end if
    elsif chars (KEY_LEFT_ARROW) then
        if circlex + radius < 60 then
            circlex := circlex + 1
        else
            circlex := circlex - 1
        end if
    elsif chars (KEY_RIGHT_ARROW) then
        if circlex + radius > maxx - 20 then
            circlex := circlex - 1
        else
            circlex := circlex + 1
        end if
    end if
end loop


if the brevity of the code is not very good, oh well.
Sponsor
Sponsor
Sponsor
sponsor
Paul




PostPosted: Sun Feb 01, 2004 4:49 pm   Post subject: (No subject)

oh, so you used the size of the walls? I was just thinking about how to do it with whatdotcolor, you know, the only thing I've thought up of is if a color drawn over that doesnt belong there, then change the coords.
recneps




PostPosted: Sun Feb 01, 2004 4:50 pm   Post subject: (No subject)

that works. thats basically the "rectangle" because you've made an invisible rectangle around the whole screen area, eg i show as a drawbox ;p

drawbox(0,0,maxx,maxy,7)

thats basically your box. and when it hits the edge of that box, its gonna go back one rather than forward, according to your code.
Paul




PostPosted: Sun Feb 01, 2004 4:55 pm   Post subject: (No subject)

Jonos, you won't mind if I leech off your post right? It has to do with the same thing. Can anyone tell me how to do collision detection with whatdotcolor?
jonos




PostPosted: Sun Feb 01, 2004 4:57 pm   Post subject: (No subject)

ill try it with whatdotcolor right now, hold on ten mins. i don't care if you leach off mine, i leached off your title colors Very Happy
recneps




PostPosted: Sun Feb 01, 2004 5:00 pm   Post subject: (No subject)

have your walls say, black,....
drawbox(x,y,x2,y2,black)

and your car, say, red
drawoval(x,y,rad,rad,red)

and
if whatdotcolour(x[car]+rad,y[car]+rad) = black then
collision=true
end if
jonos




PostPosted: Sun Feb 01, 2004 5:06 pm   Post subject: (No subject)

code:

var chars : array char of boolean
var circlex, circley : int := 100
var radius : int := 20

colorback (black)
cls

loop
    setscreen ("offscreenonly")
    View.Update
    drawfillbox (20, 20, maxx - 20, maxy - 20, 12)
    drawfilloval (circlex, circley, radius, radius, white)
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        if whatdotcolor (circlex, circley + radius + 2) = 12 then
            circley := circley + 1
        else
            circley := circley - 1
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if whatdotcolor (circlex, circley + radius + 2) = 12 then
            circley := circley - 1
        else
            circley := circley + 1
        end if
    elsif chars (KEY_LEFT_ARROW) then
        if whatdotcolor (circley, circlex + radius + 2) = 12 then
            circlex := circlex - 1
        else
            circlex := circlex + 1
        end if
    elsif chars (KEY_RIGHT_ARROW) then
        if whatdotcolor (circley, circlex + radius + 2) = 12 then
            circlex := circlex + 1
        else
            circlex := circlex - 1
        end if
    end if
end loop

this is what i got, but it still doesn't work perfectly, i hope no one beat me to it. play around with it paulbian and maybe you can get it to work.,
Andy




PostPosted: Sun Feb 01, 2004 5:08 pm   Post subject: (No subject)

your approach only works for mono colored things... what u should do is check to the direction of movement...
Sponsor
Sponsor
Sponsor
sponsor
Paul




PostPosted: Sun Feb 01, 2004 5:10 pm   Post subject: (No subject)

Im thinking of doing one of those obstacle courses where you have to manuver the ball, which the size you can change, but the the smaller it is the faster it gets, through a tube that has different sized sections. Just to get familiar with collision detection. To me that's where whatdotcolor could be useful.
jonos




PostPosted: Sun Feb 01, 2004 5:10 pm   Post subject: (No subject)

can you help us please, cause what i gave doesn't even work, it stops in a place where it shouldn't.
jonos




PostPosted: Sun Feb 01, 2004 5:17 pm   Post subject: (No subject)

code:

var chars : array char of boolean
var circlex, circley : int := 100
var radius : int := 20

colorback (black)
cls

loop
    setscreen ("offscreenonly")
    View.Update
    drawfillbox (20, 20, maxx - 20, maxy - 20, 12)
    drawfilloval (circlex, circley, radius, radius, white)
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        if whatdotcolor (circlex + radius + 1, circley + radius + 1) not= 7 then
            circley := circley + 1
        else
            circley := circley - 1
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if whatdotcolor (circlex + radius + 1, circley - radius - 1) not= 7 then
            circley := circley - 1
        else
            circley := circley + 1
        end if
    elsif chars (KEY_LEFT_ARROW) then
        if whatdotcolor (circley + radius + 1, circlex - radius - 1) not= 7 then
            circlex := circlex - 1
        else
            circlex := circlex + 1
        end if
    elsif chars (KEY_RIGHT_ARROW) then
        if whatdotcolor (circley + radius + 1, circlex + radius + 1) not= 7 then
            circlex := circlex + 1
        else
            circlex := circlex - 1
        end if
    end if
end loop

just one small thing to fix cause the circle stops in this one place on the y axis all the time
Paul




PostPosted: Sun Feb 01, 2004 5:21 pm   Post subject: (No subject)

I just made a minor change to the if structure, cause you used elsif, it doesnt move diagonally.
code:

var chars : array char of boolean
var circlex, circley : int := 100
var radius : int := 20

colorback (black)
cls

loop
    setscreen ("offscreenonly")
    View.Update
    drawfillbox (20, 20, maxx - 20, maxy - 20, 12)
    drawfilloval (circlex, circley, radius, radius, white)
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        if whatdotcolor (circlex + radius + 1, circley + radius + 1) not= 7 then
            circley := circley + 1
        else
            circley := circley - 1
        end if
    end if
    if chars (KEY_DOWN_ARROW) then
        if whatdotcolor (circlex + radius + 1, circley - radius - 1) not= 7 then
            circley := circley - 1
        else
            circley := circley + 1
        end if
    end if
    if chars (KEY_LEFT_ARROW) then
        if whatdotcolor (circley + radius + 1, circlex - radius - 1) not= 7 then
            circlex := circlex - 1
        else
            circlex := circlex + 1
        end if
    end if
    if chars (KEY_RIGHT_ARROW) then
        if whatdotcolor (circley + radius + 1, circlex + radius + 1) not= 7 then
            circlex := circlex + 1
        else
            circlex := circlex - 1
        end if
    end if
end loop

jonos




PostPosted: Sun Feb 01, 2004 5:24 pm   Post subject: (No subject)

yeah, i was trying some stuff to make it go diagonal, thanks. i got it working, i just had to make the window smaller, which probably isn't the best way to do it, but whatever
code:

var chars : array char of boolean
var circlex, circley : int := 100
var radius : int := 20

setscreen ("offscreenonly")
setscreen ("graphics:400;400")
colorback (black)
cls

loop
    View.Update
    drawfillbox (20, 20, maxx - 20, maxy - 20, 12)
    drawfilloval (circlex, circley, radius, radius, white)
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        if whatdotcolor (circlex + radius + 1, circley + radius + 1) not= 7 then
            circley := circley + 1
        else
            circley := circley - 1
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if whatdotcolor (circlex + radius + 1, circley - radius - 1) not= 7 then
            circley := circley - 1
        else
            circley := circley + 1
        end if
    elsif chars (KEY_LEFT_ARROW) then
        if whatdotcolor (circley - radius - 1, circlex - radius - 1) not= 7 then
            circlex := circlex - 1
        else
            circlex := circlex + 1
        end if
    elsif chars (KEY_RIGHT_ARROW) then
        if whatdotcolor (circley + radius + 1, circlex + radius + 1) not= 7 then
            circlex := circlex + 1
        else
            circlex := circlex - 1
        end if
    end if
end loop

maybe someone will know how to do it better.
TheXploder




PostPosted: Sun Feb 01, 2004 5:32 pm   Post subject: (No subject)

it's still wierd somethimes the up button make it move through the wall or the up button makes it move down.. really weird, and I've never see anyone do collisions with whatdotcolour...
jonos




PostPosted: Sun Feb 01, 2004 5:35 pm   Post subject: (No subject)

yeah, i see what you mean, i think it's just turing though, because i don't see how it could be wrong. ive thought it over pretty hard Sad
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 4  [ 49 Posts ]
Goto page 1, 2, 3, 4  Next
Jump to:   


Style:  
Search: