PDA

View Full Version : how to make the bricks disappear when the ball hit them?



08-19-2002, 10:20 PM
i'm doing a simple game for my assignment..the game mostly like DX-ball which is we have a ball ,a paddle and some bricks. we must control the paddle to make the bouncing ball won't fall to the ground.ok the question is how to make the ball hit the bricks and the bricks will disappear and the users get the mark. i just can make the paddle move by keyboard and the ball bouncing but i still cannot make the bricks disaappear when the ball them..and also cannot do the marks function so can anybody help me or some source code maybe?..

Pa3PyX
08-19-2002, 11:02 PM
What are you using to draw the bricks? glRasterPos/glDrawPixels I'm guessing? If so, you can undraw your bricks (when the ball hits them) by using XOR blitting:

glDisable(GL_DITHER);
...

glEnable(GL_LOGIC_OP);
glLogicOp(GL_XOR);
glRasterPos(<same position where the brick was initially drawn> );
glDrawPixels(<same call as was used to draw the brick> );
glDisable(GL_LOGIC_OP);

That should erase the brick. Or if you are just using glRect to draw bricks as rectangles on the black background, you can draw a black rectangle at the same position, same size.

Now as per Physics (determining when your ball hits the brick so that you need to erase it), OpenGL won't do it for you... you have to maintain the positions of all bricks and the ball, and in your event loop check if they match. For example, you have the following thing to store your bricks, let's say a linked list:


struct brick_t {
int lowerLeftX;
int lowerLeftY;
int upperRightX;
int upperRightY;
char imageData[32 * 16 * 3]; // RGB8
struct brick_t *next;
// something else
...
};
and you initialize it randomly when you start the game. Let's assume that the ball is a point.


struct ball_t {
int ballX;
int ballY;
// How about ballZ http://www.opengl.org/discussion_boards/ubb/smile.gif
float velocityX;
float velocityY;
// other stuff
...
}

Now on each iteration of your event loop, after you advance the ball, you need to see if the ball accidentally occupies the same space as any one of the bricks (let's assume struct brick_t *first points to the first brick in the linked list, and brick's "next" field points to the next brick (if 0, that means it's the last brick)):


struct brick_t *next;

brick_p = first;
while (brick_p) {
if (ball.ballX >= brick_p->lowerLeftX &amp;&amp; ball.ballY >= brick_p->lowerLeftY &amp;&amp; ball.ballX <= brick_p->upperRightX &amp;&amp; ball.ballY <= brick_p->upperRightY) {
// the ball has hit the brick
undrawBrick(brick_p);
bounceBall(ball);
// done
break;
}
// not hit this brick - go check next one
brick_p = brick_p->next;
}

Now this is actually subject to bugs - what if the ball moves so fast that it can "skip" a brick in just one iteration of the event loop? It will just "fly over". More, to bounce a ball, you need to know whether it hit the brick vertically or horizontally (and then invert the y or x component of the ball's velocity).

nexusone
08-20-2002, 11:16 AM
I would make each brick an object.

// define object
typedef struct
{
glFloat x,y,z; // location of brick
int status; // status: 0 = hit, 1= not hit
}Object;

static Object bricks[20];


// code in draw routine

for(x=0; x < 20; x++) //draw bricks
{
if (bricks[X].status = 1) Draw_brick();// draw bricks not hit
};

// code in control loop.
// will compare locations of bricks and there status to location of ball.
If brick has been hit ball passes throuh it.




[This message has been edited by nexusone (edited 08-20-2002).]