Computer Science Canada Whatdotcolour collision detection problem |
Author: | Badsniper [ Fri May 06, 2011 10:00 pm ] | ||
Post subject: | Whatdotcolour collision detection problem | ||
This is an exert from a program I am writing, I need it to move a small circle around the screen. This is the code when the circle "jumps". For some reason, the collision detection doesn't work when the circle is in the jump. for example, it will come to a stop when it hits a verticle black line, but when it "jumps" it can go through it. I've tried many things, from changing the x and y values the computer checks for black and I've redrawn the background (variable "back") in many different positions, but I can't get it to work. Can someone at least explain why? |
Author: | Zren [ Fri May 06, 2011 11:02 pm ] |
Post subject: | RE:Whatdotcolour collision detection problem |
That's because your CD is probably outside this for loop in the main game loop. You're not checking CD inside this loop. The typical way to do loops is to not freeze everything else (going into a subloop) but to keep with the ... loop input logic render frame (draw) end loop ... program flow. The wrong way would be to make the CD a procedure and call that within the for loop. The right way would be to remember the state of the player (jumping/not jumping) that'd be toggled during input stage. Then move the player during the logic (which is when you check the CD). There's bound to be a tutorial or source code for this like somewhere. If you can't find an example I'll write one up later. |
Author: | mirhagk [ Sat May 07, 2011 1:15 pm ] |
Post subject: | RE:Whatdotcolour collision detection problem |
A trick is to use the movement as a velocity variable. That way the jump increases the velocity, and each turn the velocity gets added to the position, then the velocity gets smaller and smaller till it goes negative and the player falls back down. |