• Create Account

## 2d collision

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1phil67rpg  Members

217
Like
0Likes
Like

Posted 20 March 2012 - 09:43 PM

well I am building a breakout game using c#.I am able to get the ball to bounce off of the paddle and walls, but I am confused about how to get it to bounce off the bricks.I have done alot of research on this topic and I am kind of lost.there is sure alot of info. on the net. here is the code I am using.

if (e.KeyChar == (char)32)
{

Image curImage17 = Image.FromFile(@"c:\Users\phil\Desktop\ball.bmp");
g.DrawImage(curImage17, x + vx, y + vy);

x += vx;
y += vy;

if (y >= bottom_wall || y<=top_wall)
{
vy = -vy;
}

if (x >= right_wall || x <= left_wall)
{
vx = -vx;
}

{
vx = -vx;
vy = -vy;
}
}

this is probably the hardest part of this game,I am doing the best I can to solve on my own, but another pair of eyes is always appreciated.

### #2Trienco  Members

2555
Like
0Likes
Like

Posted 20 March 2012 - 11:04 PM

I see a few problems where you ignore how fast the ball might be moving.

"If the ball is beyond the left wall, reverse horizontal direction". What if the ball is STILL beyond the left wall the next frame? If all your values are integers that should be impossible right now, but if at any point you decide to add a random variance to the balls velocity on collision it becomes a possibility.

Your paddle collision only works if vy will never grow larger than your paddle height or the ball can simply move right through it. Taking that into account means you have to stop just looking at snapshots and check for intersection in single frames. In other words, moving from intersection detection to actual collision detection. For now I'd simply stick with it and make sure the ball won't get too fast.

I'm also not sure why you think that the blocks are so much different from the paddle. You already treat your ball as a point and not as a sphere, which is making things a lot easier, since all you have to do is "is this point inside this rectangle". Unless you start being fancy with wildly rotated or weirdly shaped blocks, that is exactly like your paddle (except that you can't just skip the lower edge).
f@dzhttp://festini.device-zero.de

### #3rip-off  Moderators

10730
Like
0Likes
Like

Posted 21 March 2012 - 04:19 AM

You should not mix rendering and game logic like that. It is a great way to get bugs. Instead, having one function which moves the objects and manages collision, and another function which draws the current state of the game.

Images are Disposable, which means that loading and discarding them like that is a resource leak. You should load the image exactly once at the start of your game, and it should be deallocated properly as the game shuts down.

The variable name "currImage17" is poor, better would be "ballImage". The variable name "i" is terrible. It appears to be the paddle's current x position. Thus, paddleX would be a better name.

You are "confused about how to get it to bounce off the bricks", but you haven't shown how you are modelling the bricks.

For a simple breakout clone, you have a set of fixed bricks and a free ball. The former can be modelled as an two dimensional array. To start off, I'd recommend implementing a uniform set of bricks, in which case your array is simply an array of boolean values. Each entry represents the presence or absence of a brick. As the ball hits the bricks, the corresponding boolean in the array is set to false. To start with, populate the array as all true (you can implement random levels or loading later).

You can infer the collision detection data from the index into the array. For example, to do collision detection in screen space units, with a 100 pixel border between the bricks and the side of the screen, and with each brick being 10 pixels high and 25 pixels wide, then a brick at index (y, x) has a collision rectangle { x, y, width, height } of { 100 + (25 * x), 100 + (10 * y), 25, 10 }. Obviously you would use named constants instead of hard coding these values, this is for illustration.

As Trienco says, the collision detection is similar to the the paddle (though you need to test more conditions). It will suffice to brute force through the array of bricks to see if any of the "active" ones are colliding.

In future, please use [ code ][ /code ] tags and paste the source as plain text. This way your code will stay indented, it won't have such a strange font and will be easier for us to copy, paste and test (if necessary). The post editor has a "code" button which you can use for this.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.