Jump to content
  • Advertisement

Archived

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

The big Question?

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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[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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!