#### Archived

This topic is now archived and is closed to further replies.

# Problem with ball-reflection in breakout-clone.

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

## Recommended Posts

Well I´m a beginner in the art of game programming, and I have a little problem in my game, which is a breakout-clone. I can´t make the ball reflect right on the blocks. My program works when the boxes are being hit from the sides and the top, but ball hits one of the conners of a block the shit doesn´t work, and the ball just continues and hit the next block too! I have tried two different methods: Line-interception and vector-interception, but the shit keeps bugging. Do anyone know what could be wrong or have some sample code they wanna share? I hope so..

##### Share on other sites
This could be far off track, but try it. I am not going to actually write out much code, just tell you how. Simplest collision detection in the universe

if (bally < blockbottom && bally > blocktop)
{
if (ballx > blockleft && ballx < ballright)
bally_vel = -bally_vel;
}

what that does, is if the ball hits the block, it makes it bounce as if it hit a flat wall parallel to the top and bottom of the screen. If you add something to stop it from bouncing inside the block, it could help you. You can probably modify it for use with the sides too (it may work as is)

--------------------

You are not a real programmer until you end all your sentences with semicolons;

##### Share on other sites
Well thanks, but the collision is no problem my code works perfect there.
It also works when the ball hits from the sides
(dxBall = -dxBall)
and from the top or bottom (dyBall = -dyBall).

But when a block is hit from a conner my code fucks up every time, because the ball should:
dyBall = -dyBall;
dxBall = -dxBall;

Somehow the ball just continues and hit the next block ):

Hope some can help, because I´m getting tirred of fighting with the code.

##### Share on other sites
Sounds like something is wrong with your collision detection in this case. Post the code so we can see how you''re testing for a corner.

aig

##### Share on other sites
Well the code is "rather" big, so here is the first part, which should work.
I haven´t optimized the code yet, so it´s a little slow, but it should work:

//////////////////////////////////
#define BALL_WIDTH 12
#define BALL_HEIGHT 12

#define BALL_COL_WIDTH 9
#define BALL_COL_HEIGHT 9
#define BALL_X_OFFSET 1
#define BALL_Y_OFFSET 1

////////////////////////////////////
// In code:
INT nX, nY, nX2, nY2;

// Calc the balls bounding-box, based on it´s a circle:
nX = m_nBallX +BALL_X_OFFSET;
nX2 = nX +BALL_COL_WIDTH;
nY = m_nBallY +BALL_Y_OFFSET;
nY2 = nY +BALL_COL_HEIGHT;

// Test for collision:
if ((nY2 >= m_Blocks.nPosY) && (nY <= m_Blocks[i].nPosY +BLOCK_HEIGHT)
&& (nX2 >= m_Blocks[i].nPosX) && (nX =< m_Blocks[i].nPosX +BLOCK_WIDTH))
{
// Now there is some kind of collision.
}

This part should be ok, because the block does disappear, but the ball continues and hit the next block.

##### Share on other sites
-Damn -
I forgot to put my name on the code above, but it is mine.

- The Big Question? -

##### Share on other sites
You have =< instead of <= in the last expression. Is that a typo in the code or just in the post?

Also, <= and >= have a higher precedence than +. So in your 2nd and 4th expressions, I think you want to put brackets around m_Blocks[ i ].nPosX +BLOCK_WIDTH, otherwise the expressions are being evaluted wrong (I think).

You say the block disappears, but the ball doesn't change direction - is the code for clearing the block and for changing ball direction both together inside that if statement? If one is getting executed, the other should as well - is there anything inside the if statement that might prevent this?

aig

Edited by - An Irritable Gent on 4/24/00 11:51:13 AM

• 15
• 13
• 35
• 39