Public Group

# collision detection help needed

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

## Recommended Posts

Hi, I am in the middle of my first game (a pong clone) and the collision detection is confusing. I can detect a collision between the paddle and ball, thats no problem its basic bounding box stuff. However, I'm trying to figure out which side of the paddle was intersected. I need this information to figure out how to set the ball's position outside of the paddle's bounding box in the correct position before I invert the balls's velocity.

##### Share on other sites
just check which side of the bat the balls center is.

eg:

|
|0
|

ball center x cord is greater than bat center x cord

##### Share on other sites
If two objects collide like so:
|-----|
|-----|
|--|--|--|
|--|--|--|
|-----|
|-----|

do you set the ball
|-----|
|-----|
|-----|
|--|--|--|
|-----|
|-----|
|-----|

or

|-----|
|-----|
|-----|-----|
|-----|-----|
|-----|
|-----|

##### Share on other sites
That's one of the problems with static collision detection - there really isn't a clear answer to that question.

One approximation is to choose the option that results in the smallest overall translation (i.e. distance moved). In your example, where the distances of the two translations are the same, you can just choose one.

You might also look into the swept test for bounding boxes, which takes the motion of the boxes into account. It's a little more complicated, but would solve the problems you're likely to encounter with static intersection. I can't remember the title, but there's a gamasutra article (linked from the articles section here on gamedev) which describes the swept test for bounding boxes, along with code - I believe the author is Gomez.

##### Share on other sites
I would simply do something like this:

xVelocity = -xVelocity;
yVelocity = -yVelocity

That should produce the bounce-back effect. Make sure the ball doesn't get stuck inside the paddle, though.

##### Share on other sites
Note that negating both components of the velocity vector might not give you the behavior you want. Generally you'll only want to negate one of them, depending on where the ball collides with the paddle.

##### Share on other sites
I had a similar problem before with my sad excuse of a Pong game. When the ball came down from above, it would bounce back and forth INSIDE the paddle like 20 times and then fly off the screen (the ball was programmed to increase in speed everytime the paddles hit it, so you can imagine the effect). I never did finish that, but now that you brough it up, here's an idea:

Have each paddle track a few flags denoting where the ball is in relation to it.
For example: bool isToTheSide = true, isAbove = false, isBelow = false;
This assumes your paddles are centered vertically and your ball is in the center of the screen to begin with. Then AFTER you check for collision (assuming the very first frame won't result in one), you update the flags based on the ball position relative to the paddles.

Now, when your ball collides, do you bounce vertically (when hitting the top) or horizontally (when hitting the side)? That's the issue, correct? Well, check the paddle's flags, which now tell you where the ball was JUST BEFORE the collision. If it was to the side, but not above or below, bounce horizontaly. If it was either above or below, but not to the side, bounce vertically, otherwise it was to the side and either abover or below, in which case you bounce in both directions (i.e., the ball hit the exact corner of the paddle, so it should bounce back straight)

That's just off the top of my head, so it may not be totally on point, but I think it should do the trick. Let me know how it turns out if you try it.

##### Share on other sites
Thanks for the tip I have no time to try it out now but it does sound promising. ldramire, what are you working on now? and howcome you never finished your pong clone? lol

##### Share on other sites
Quote:
 Original post by ranglerThanks for the tip I have no time to try it out now but it does sound promising. ldramire, what are you working on now? and howcome you never finished your pong clone? lol

Currently working on a group project building a 3D game in DX w/ C++ for a course. We're making something like the original StarFox. Should be good times.

That pong game I started was for the final project of a game programming course I took. I had 5 other classes to deal with, so you can imagine I didn't exactly have all the time in the world to devote to it, so I didn't manage to complete it. I got the basics down and some powerups, but no weapons as I intended (catch the ball AND avoid your opponents missiles! :-D) It was in C# and quite a pain. Now that I've done more DX in C++, I plan to eventually port it and pick up where I left off ... but definitely not for a while...

Let me know if my idea worked so I can use it later :-P

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634133
• Total Posts
3015747
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!