Problem with ball-reflection in breakout-clone.

Started by
5 comments, last by The big Question? 24 years ago
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..
<<>> The Big ? <<>>
Advertisement
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;

Yanroy@usa.com

Visit the ROAD Programming Website

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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.
<<>> The Big ? <<>>
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
aig
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:

//////////////////////////////////
// In header:
#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.nPosY +BLOCK_HEIGHT) <br>&& (nX2 >= m_Blocks.nPosX) && (nX =< m_Blocks.nPosX +BLOCK_WIDTH))<br>{<br> // Now there is some kind of collision.<br>}<br><br>This part should be ok, because the block does disappear, but the ball continues and hit the next block. </i>
-Damn -
I forgot to put my name on the code above, but it is mine.

- The Big Question? -
<<>> The Big ? <<>>
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.nPosX +BLOCK_WIDTH, otherwise the expressions are being evaluted wrong (I think).<br><br>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?<br><br>aig<br><br><br>Edited by - An Irritable Gent on 4/24/00 11:51:13 AM
aig

This topic is closed to new replies.

Advertisement