Computer Science Canada Square and Circle collision detection not working proporly |
Author: | Offroadrider [ Thu Mar 31, 2011 11:05 am ] | ||
Post subject: | Square and Circle collision detection not working proporly | ||
Hey guys, I am trying to make a tag program but the collision detection between the circle and the square is not working proporly. Here is the code that is not working
This is in turing 4.1.1 1 more thing, this is not the entire code it would be way too big for this space. this is just the pieces of code that have to do with the collision detection Thanks for your help |
Author: | Insectoid [ Thu Mar 31, 2011 11:16 am ] |
Post subject: | RE:Square and Circle collision detection not working proporly |
Quote: bally -20 > boxy
Quote: bally > boxy +20
What's different about these two statements? |
Author: | Offroadrider [ Thu Mar 31, 2011 11:29 am ] |
Post subject: | Re: Square and Circle collision detection not working proporly |
Nothing, it's just that when i wrote the ballx-20, it finnaly worked on the some what on y-axis if I put boxx +20 > ballx like on the other line, it didn't work |
Author: | Offroadrider [ Thu Mar 31, 2011 1:02 pm ] |
Post subject: | Re: Square and Circle collision detection not working proporly |
feel free to copy the code into a turing file to help find out what's not working |
Author: | Zren [ Thu Mar 31, 2011 2:07 pm ] |
Post subject: | RE:Square and Circle collision detection not working proporly |
Box: The bottom left is (boxx, boxy). The top right is (boxx+20, boxy+20) Circle: The middle is (ballx, bally) Circle and rectangular collision is done with if a point is within the range of the circle or rectangle. You seem to be trying to compare two ranges. And also treating the ball's center point like one of the rectangles control points (it's in the middle not a corner). Make sure your code follows this pattern, where p is the middle of your ball. Rectangle: box.x <= p.x <= box.x + box.width box.y <= p.y <= box.y + box.height The way collision detection works, is that you can check circle against circle, or rectangle against rectangle, or if a point is within either. You can't easily check collision between a circle and a rectangle. You can however treat a circle as a rectangle since the circle can fit inside a rectangle (easier and more accurate than putting the rectangle inside the circle). This post on collision detection should help: http://compsci.ca/v3/viewtopic.php?t=13661 |