Public Group

collision detection

This topic is 4061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am still confused about this snippet of code if x1 + width1 > x2 and y1 + height1 > y2 and x1 < x2 + width2 and y2 < y2 +height2 then collision! Am I correct in assuming that x1,y1 the upper left corner of a box and width1 and height1 is the height and width of the same box and x2,y2 is the upperleft corner of the other box, and height2 and width2 is the height and width of the other box, a little picture of this code would be very helpful

Share on other sites
You're right. It might help to understand if you think of how to check for a collision of a box x1, y1, width1, height1, and a single point x2, y2.

               width1  x1y1 ______      |      |      |      |height1      | x2y2_|____      |____|_|    |           |      |height2           |      |           |______|            width2

Its crude but hopefully it helps.

Share on other sites
cool thanks for the picture

Share on other sites
What if you are using images with transparencies? I can start a new thread if you like.

Lets say in a Pong game. The ball is round, the paddle is curved. How can I determine if the non transparent part of the ball hit the non transparent part of the paddle?

Share on other sites
Quote:
 Original post by BUnzagaWhat if you are using images with transparencies? I can start a new thread if you like.Lets say in a Pong game. The ball is round, the paddle is curved. How can I determine if the non transparent part of the ball hit the non transparent part of the paddle?

You could first do a bounding box collision check and then check per pixel. So if you find a collision with bounding boxes then you perform a more detailed per pixel check.

Share on other sites
Think of it this way: the data you use to check your collision does not neccesarily have to be the same as the image you're displaying, if you wanted to you could perform the calculations without displaying anything at all.

So because it's pretty easy to figure out if a couple of rectangles are overlapping we like to use those. With your pong example (remembering what I said about our collision representation not having to relate to the image) you could instead represent your ball as a circle - it's still pretty easy to figure out collisions with circles.

If you've got a more complex object you could check your collisions on a per-pixel basis, but generally you'd test some bounding shape (often a rectangle) around the object first to see if it's worth the more intensive effort of checking per-pixel.

Look for "per-pixel" or "pixel perfect" collision detection if you want to seek out more reading material on exactly how you'd go about doing that, although keep in mind that even if your objects aren't really a perfect rectangle or circle the game will often play good enough (or even better in some cases) if you just stick to those and don't bother with per-pixel checking, as it can be a simple way to give players a bit of leeway with "close calls" which will make them feel more skilled for having avoided a near miss.

Also while we're on the subject, there are some excellent collision detection articles here for anyone interested in some reading on the subject: Basic Collision Detection and Response and Grid-based Collision Detection and Raycasting.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633695
• Total Posts
3013376
×