Computer Science Canada

[Tutorial] Troubleshooting collision detection

Author:  jrblast [ Wed Jan 18, 2006 11:14 pm ]
Post subject:  [Tutorial] Troubleshooting collision detection

Okay, well troubleshooting is very important, and though I have noticed alot of people (mostly at school) couldn't detect colision if their lif depended on it.... But it is much easier to use these methods than it is to try to get someone else to fix it for you, because this way is:

A) Faster

And

B) More educational (Since you notice the problem on your own, always better to do that than get someone else to)

Anyways, there are multiple ways to troubleshoot, I'll post em in whichever order I think of em.

The first way is, if you are doing collision detection, and your if statement is faulty, but you dont know in what way, just use the drawbox thing

Copy the parts of the if statement that have the boundry information into a drawbox procedure, I.E.
code:

if x >= wallX and x <= wallX + wallWidth and y >= wallY and y <= wallY + wallWidth then

    blah blah blah

end if


then move the parts on the right side of the comparison operators (the =, <, >, etc... signs) into a drawbox thing, remember, the order of locations in the drawbox thing is x1 y1 x2 y2, but the order in the if statement is x1 x2 y1 y2

so you get

code:

drawbox (wallX, wallY , wallX + wallWidth, wallY + wallWidth, black)


(remember, you need to specify a coulour, in this case i chose black)

At this point, if you draw that, you will get a square, because if you look at the last part of the drawbox command, is wallY + wallWidth, width is horizontal, but you would want vertical, AKA Height... (this one recently happened to me, took a week before i thought to draw the box Embarassed

the second way would be if you are unsure of a variable, or even a predefined variable like maxx and maxy.

If you have a program like this:
code:

var x,y,objectx, objecty : int := 200
var keys : array char of boolean
loop
 randint (objectx,0,maxx)
 randint (objecty,0,maxy)
 loop
   Input.KeyDown (keys)
   //
   //stuff to move x and y according to key being pressed, by 10s
   //
   drawoval (objectx div 5 * 5,objecty div 5 * 5,5,5,red)
   drawoval (x, y, 10,10, green)
   if x = objectx div 5 * 5 and y = objecty div 5 * 5 then
    exit
   end if
 end loop
end loop



As you may have noticed (since this is a simple code) the grid size of the object and user are not the same, one is 10s the other is 5s, so you use a put statement to put the variable values somewhere, then you will hopefuly notice the problem (though, if you use the method of collision detection as used above, this problem will be avoided, however, you will typicaly want to have things aligned when using a grid...I do believe thats the point of a grid.

At this point i only have those two methods, however i will post more later if i think of them. I'll look on teh help forums to see if theres anything there "solvable" by trouble shooting, also, you can post em here and I'll try to help you out.


: