Author:  jonos [ 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.

 Author: Paul [ Sun Feb 01, 2004 4:49 pm ] Post 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.

 Author: recneps [ Sun Feb 01, 2004 4:50 pm ] Post 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.

 Author: Paul [ Sun Feb 01, 2004 4:55 pm ] Post 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?

 Author: jonos [ Sun Feb 01, 2004 4:57 pm ] Post 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 Author:  jonos [ Sun Feb 01, 2004 5:06 pm ]
Post 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.,

 Author: Andy [ Sun Feb 01, 2004 5:08 pm ] Post subject: your approach only works for mono colored things... what u should do is check to the direction of movement...

 Author: Paul [ Sun Feb 01, 2004 5:10 pm ] Post 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.

 Author: jonos [ Sun Feb 01, 2004 5:10 pm ] Post subject: can you help us please, cause what i gave doesn't even work, it stops in a place where it shouldn't.

Author:  jonos [ Sun Feb 01, 2004 5:17 pm ]
Post 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

Author:  Paul [ Sun Feb 01, 2004 5:21 pm ]
Post 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

Author:  jonos [ Sun Feb 01, 2004 5:24 pm ]
Post 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.

 Author: TheXploder [ Sun Feb 01, 2004 5:32 pm ] Post 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...

 Author: jonos [ Sun Feb 01, 2004 5:35 pm ] Post 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 Author:  McKenzie [ Sun Feb 01, 2004 5:35 pm ]
Post subject:

your checks are wrong. e.g. you have:
 code: elsif chars (KEY_LEFT_ARROW) then         if whatdotcolor (circley, circlex + radius + 2) = 12 then

if you are looking left it should be the point (circlex - radius -1, circley)

Author:  Paul [ Sun Feb 01, 2004 5:36 pm ]
Post subject:

I got my whatdotcolor collision working, it works fine.
 code: setscreen ("offscreenonly") var x, y : int x := 100 y := 100 var addx, addy : int := 0 var chars : array char of boolean loop     drawfillbox (0, 0, maxx, maxy, black)     drawfillbox (10, 10, maxx - 10, maxy - 10, 0)     Input.KeyDown (chars)     if chars (KEY_UP_ARROW) then         if whatdotcolor (x, y + 11 + addy) = 0                 then             y := y + 5         end if     end if     if chars (KEY_RIGHT_ARROW) then         if whatdotcolor (x + 11 + addx, y) = 0 then             x := x + 5         end if     end if     if chars (KEY_LEFT_ARROW) then         if whatdotcolor (x - 11 - addx, y) = 0 then             x := x - 5         end if     end if     if chars (KEY_DOWN_ARROW) then         if whatdotcolor (x, y - 11 - addy) = 0 then             y := y - 5         end if     end if     drawfilloval (x, y, addx + 10, addy + 10, red)     View.Update     delay (10)     cls     if chars (KEY_ENTER) then         addx := addx + 5         addy := addy + 5     end if     if chars (KEY_BACKSPACE) then         addx := addx - 5         addy := addy - 5     end if end loop

Mine grows and shrinks too Edit
I deleted the below post because I feared the wrath of AsianSensation.

Author:  Cervantes [ Sun Feb 01, 2004 5:36 pm ]
Post subject:

collisions with whatdotcolour are the first way I learned to do them..
however, whatdotcolour isn't as good as going it with coords in some instances, in other instances whatdotcolour is the only way to go to make it go diagonal..

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

the difference being that I got rid of the elsif's and replaces them with if's. before, if the uparrow was being pressed, it would never ask whether any of the other keys were being pressed. this way it will Author: McKenzie [ Sun Feb 01, 2004 5:37 pm ] Post subject: I type too slow.

 Author: Paul [ Sun Feb 01, 2004 5:47 pm ] Post subject: I tried to get the growing shrinking thing to be restricted by the wall too, using the same principle, but it didn't work.

 Author: jonos [ Sun Feb 01, 2004 5:51 pm ] Post subject: thanks mckenzie, that really helped, don't know why i didn't see it for the growing circle couldn't you just go: [code] if circlex+radius+1 or circlex -radius-1 or circley+radius +1 or circley -radius -1 = [the wall color] then radius-1 or radius+1 else do nothing end if that may not be right haven't tested it.l

Author:  Cervantes [ Sun Feb 01, 2004 5:53 pm ]
Post subject:

where you have the

you should alter add an if statement in there
 code: if chars (KEY_ENTER) then         addx := addx + 5         addy := addy + 5         if x > maxx - 20 then               x -= 5         end if         if x < 20 then               x += 5         end if         if y > maxy - 20 then               y -= 5         end if         if y < 20 then               y += 5         end if     end if

do the same thing with y

if you want to do collisions with whatdotcolour then you can do that as well, you just have to implement it into the spots in that code up there where I put things like "if y > maxx - 20 then"

Cheers

Author:  Cervantes [ Sun Feb 01, 2004 5:56 pm ]
Post subject:

jonos wrote:

else
do nothing
end if

jonos an if statement does not need to have an else. ex of that is your code modified by me so that it can go on a diagonal. sometimes else is useful though, but if its
 code: else    do nothing end if

then just ditch the else.

Cheers

 Author: jonos [ Sun Feb 01, 2004 6:06 pm ] Post subject: yeah, i know that, don't know why i put that. i probably though of putting something there but then decided against it.

Author:  Paul [ Sun Feb 01, 2004 6:07 pm ]
Post subject:

I tried it, using and instead of or and it works!

He didn't delete, I just deleted it, fearing his wrath.

 Author: jonos [ Sun Feb 01, 2004 6:13 pm ] Post subject: there are still things wrong with it, and i can't find it and don't want to spend time doing it. if you blow it up and move it left, then it can still get through, as well as if you keep playing around with it it will do the same for the other walls. ahhhh, this is confusing Author: Cervantes [ Sun Feb 01, 2004 6:14 pm ] Post subject: oh lol, I just deleted my post sense i saw your post reappear. (you can delete this and 2 bits if you want Asian )

 Author: Paul [ Sun Feb 01, 2004 6:15 pm ] Post subject: yea, I noticed that left wall thing to, I fixed that line.

Author:  Cervantes [ Sun Feb 01, 2004 6:20 pm ]
Post subject:

for things like this I prefer to use coordinates rather than whatdotcolour in terms of collision
 code: setscreen ("offscreenonly") var x, y : int x := 100 y := 100 var addx, addy : int := 0 var chars : array char of boolean loop     drawfillbox (0, 0, maxx, maxy, black)     drawfillbox (10, 10, maxx - 10, maxy - 10, 0)     Input.KeyDown (chars)     if chars (KEY_UP_ARROW) then         if whatdotcolor (x, y + 11 + addy) = 0                 then             y := y + 5         end if     end if     if chars (KEY_RIGHT_ARROW) then         if whatdotcolor (x + 11 + addx, y) = 0 then             x := x + 5         end if     end if     if chars (KEY_LEFT_ARROW) then         if whatdotcolor (x - 11 + addx, y) = 0 then             x := x - 5         end if     end if     if chars (KEY_DOWN_ARROW) then         if whatdotcolor (x, y - 11 - addy) = 0 then             y := y - 5         end if     end if     if chars (KEY_ENTER) then         addx := addx + 5         addy := addy + 5         if x > maxx - x - addx - 15 then             x -= 5         end if         if x < 0 + x + addx + 15 then             x += 5         end if         if y > maxy - y - addy - 15 then             y -= 5         end if         if y < 0 + y + addy + 15 then             y += 5         end if     end if     if chars (KEY_BACKSPACE) then         addx := addx - 5         addy := addy - 5     end if     drawfilloval (x, y, addx + 10, addy + 10, red)     View.Update     delay (10)     cls end loop

that's still slightly buggy..

the + 15 or the - 15 in this part
 code: if x > maxx - x - addx - 15 then             x -= 5         end if

10 because you did drawfilloval (x,y,addx + 10, addy + 10, red) and 5 because that's how much you add every time enter or backspace is hit. the 5 is prevention.

Keep playing around with it and we can iron out all the bugs.

EDIT: the bug in this code is that it works fine for the first wall that you got to, but then any of the other walls you go to its buggy...

 Author: Andy [ Sun Feb 01, 2004 6:25 pm ] Post subject: Cervantes wrote:however, whatdotcolour isn't as good as going it with coords in some instances, in other instances whatdotcolour is the only way to go cervantes, how much bits do u want to lose? whatdotcolor is ALWAYS i repeat ALWAYSE the way to go!!!! and not in some instances, nonono, in ALL i repeat ALL instances, whatdotcolor is the only way to not go, LIVE. -5 bits btw

 Author: Paul [ Sun Feb 01, 2004 6:25 pm ] Post subject: crap, I edited the wrong line, I edited it back now. Theres a bug that when you expand while moving, it goes into the wall.

 Author: Cervantes [ Sun Feb 01, 2004 6:32 pm ] Post subject: okay okay dodge I repent I repent! hail whatdotcolour!! whatdotcolour is the way to go! It's the way to live! All hail whatdotcolour!! The Turing command that elevates Turing beyond C++ for gaming and all other purposes! Author: Andy [ Sun Feb 01, 2004 6:34 pm ] Post subject: gj, plus another 10 bits lol

 Author: Paul [ Sun Feb 01, 2004 6:35 pm ] Post subject: Yes, whatdotcolor IS the greatest thing ever, it changed my life, otherwise I might not understand collision at all. I'll go and reccommend it to all my friends now. *nudge*

 Author: Cervantes [ Sun Feb 01, 2004 6:41 pm ] Post subject: woot! I'm getting the hang of this religion now Paulbian I'll have my own style of your program up in a bit.

 Author: shorthair [ Sun Feb 01, 2004 6:46 pm ] Post subject: dodge is being sweet talked every way till sunday , i think a labrinth marble game , would be hte best way to put collision detection to the test ,also becuase you would hav etrue phyics involved , wow i jsut bouht 3dsMax and im lost , the book is 790 pages , its insane in the membrane wanna know why......." its the beginers guide " , someone better make a what dot color tutorial , everyone asks aboutit at least once in their compsci life ( well most ) ,dodge this would be cool of you to do since your like the god of it and what not

 Author: Andy [ Sun Feb 01, 2004 6:50 pm ] Post subject: ok well since ccc is coming up, for every problem on there that some one does with whatdotcolor in a cool way, i will award 20 bits*question number s4 counts as 6 s5 as 7

 Author: recneps [ Sun Feb 01, 2004 6:53 pm ] Post subject: whats CCC?

 Author: Andy [ Sun Feb 01, 2004 6:57 pm ] Post subject: waterloo's canadian computing contest

 Author: shorthair [ Sun Feb 01, 2004 7:03 pm ] Post subject: go look in contests and im sur there is a sticky with everything about it by tony or someone , its really interesting , you ca end up programming overseas for waterloo , well 4 peopel in canada will be

 Author: recneps [ Sun Feb 01, 2004 7:04 pm ] Post subject: Verrrry Interesting Author: Andy [ Sun Feb 01, 2004 7:06 pm ] Post subject: 4? more like 20

 Author: shorthair [ Sun Feb 01, 2004 7:08 pm ] Post subject: for the final amount , i htought it was like 4 , wow ther eteam is that big , didnt bugz make round 2 last year the one at waterloo

 Author: Andy [ Sun Feb 01, 2004 7:10 pm ] Post subject: yea so did another massey kid, but he didnt want to go... he's at MIT now

Author:  Cervantes [ Sun Feb 01, 2004 7:20 pm ]
Post subject:

what compsci knowledge should you have for this CCC? full highschool knowledge?

Paulbian here's my version of your program, flawless if I might add. and in the spirit of my conversion to whatdotcolour I have utilized whatdotcolour in said program 8)

 code: setscreen ("offscreenonly") var x : int := maxx div 2 var y : int := maxy div 2 var xr, yr : int := 10 %x radius, y radius var move : boolean := true % this fixes a small bug, if you want to see what that bug is, delete all the move stuff ;) var chars : array char of boolean loop     drawfillbox (0, 0, maxx, maxy, black)     drawfillbox (10, 10, maxx - 10, maxy - 10, 0)     Input.KeyDown (chars)     %controls     if move = true then         if chars (KEY_UP_ARROW) then             y += 2         end if         if chars (KEY_DOWN_ARROW) then             y -= 2         end if         if chars (KEY_LEFT_ARROW) then             x -= 2         end if         if chars (KEY_RIGHT_ARROW) then             x += 2         end if     end if     %collision whatdotcolour style!!   woot!! ;)     if whatdotcolour (x - xr, y) = black then         x += 2     end if     if whatdotcolour (x + xr, y) = black then         x -= 2     end if     if whatdotcolour (x, y - yr) = black then         y += 2     end if     if whatdotcolour (x, y + yr) = black then         y -= 2     end if     %circle growing and shrinking     move := true     if chars (KEY_ENTER) then         xr += 2         yr += 2         move := false     end if     if chars (KEY_BACKSPACE) then         xr -= 2         yr -= 2     end if     %keep the growth under control!!!! aaaaugh!!!     if xr < 4 or yr < 4 then         xr := 4         yr := 4     end if     if xr > maxy div 2 or yr > maxy div 2 then         xr := maxy div 2         yr := maxy div 2     end if     drawfilloval (x, y, xr, yr, red)     View.Update     delay (2) end loop

btw, just to clarify, do you know what this means?

 code: x += 2

Author:  Paul [ Sun Feb 01, 2004 7:21 pm ]
Post subject:

 code: x:=x+2

either that or
1+1=3

 Author: Paul [ Sun Feb 01, 2004 7:24 pm ] Post subject: Do this with your program, run it, at the start hold down 3 keys: up arrow, right arrow, and enter. release the keys when its grown all it can grow then hold up key. flawless?

 Author: shorthair [ Sun Feb 01, 2004 7:25 pm ] Post subject: 2 + 2 = 5 , in really big instances of 2 , and about ccc , if your gonna d ojunior ( which stops at het first round , you can do it in turing i did really well ) if your doin senior you need to know another language it costs me 5 \$ t oenter the senior just for hte heck of it , i just like the time off school and te chalenge , no where near good enough tomake it out of the school to round 2 ,

Author:  Cervantes [ Sun Feb 01, 2004 7:37 pm ]
Post subject:

*tapes Paulbian's mouth shut like so --> *

flawless indead! also
 code: x += 2

is the same as
 code: x := x + 2

correct

the reason that is doing that is because I used whatdotcolour and I am not experienced enough yet... the other way I did it, like so

 code: if x < 0 + xr + 10 then      x += 2 end if if x > maxx - xr - 10 then      x -= 2 end if if y < 0 + yr + 10 then      y += 2 end if if y > maxy - yr - 10 then      y -= 2 end if

worked fine for that.. it would work with whatdotcolour too but the boarder doesn't go outside the screen, the boarder is pretty thin. I'm going to need instruction in the ways of whatdotcolour here dodge. :