Sign in to follow this  
Followers 0
phil67rpg

little help

32 posts in this topic

I am still working on my breakout game. I can get the ball to hit the brick but I cant get the brick to turn off permanently. The brick turn off and back on again, in other words it flickers on and off. I have tried a lot of different permutations of my code.
0

Share this post


Link to post
Share on other sites
We don't know your code and need a little more help.

How do you store bricks? How do you turn bricks off/on? For both answers, show a bit of code.
0

Share this post


Link to post
Share on other sites
[code]
if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f)
{
bricks[3][5]=0;
glClear(GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glRectf(3.0f,4.0f,5.0f,3.5f);
brick_collision();
glutPostRedisplay();
glutSwapBuffers();
}

//draw ball
glColor3f(1.0f,1.0f,1.0f);

glRectf(x,y,x+rsize,y-rsize);

glutSwapBuffers();
[/code]
0

Share this post


Link to post
Share on other sites
I think the main problem comes from calling glutSwapBuffers two times.

Do a full redraw every time, with one glClear at the top and one glutSwapBuffers at the end. Only draw a brick, if it is not set to 0. Now when the ball is inside the brick rect (as your check seems to do), the brick is cleared and on the next redraw it is simply not drawn.

In other words, make it so:

[code]
// brick/ball collision
if ( ( x >= 3.0f )
&& ( x <= 5.0f )
&& ( y >= 3.5f )
&& ( y <= 4.0f ) )
{
bricks[3][5] = 0;
}

// brick/ball draw code
if ( bricks[3][5] ) != 0 )
{
// draw the brick
}
[/code] Edited by Endurion
2

Share this post


Link to post
Share on other sites
if((x>=3.0f) && (x<=5.0f) && (y>=3.5f) && (y<=4.0f))
{
bricks[3][5]=0;
}

brick_collision();

void brick_collision()
{
if(bricks[3][5]!=0)
{
glColor3f(0.0f,0.0f,0.0f);
glRectf(3.0f,4.0f,5.0f,3.5f);
}
}
here is some of my code
0

Share this post


Link to post
Share on other sites
The way in which you clear and swap was previously called into question.
Why don’t you show enough code to include both your update logic and your clear/swap calls?


L. Spiro
0

Share this post


Link to post
Share on other sites
[code]
void drawScene() {

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,_textureId_four);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 5.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 5.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 4.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 4.5f, 0.0f);
glEnd();
}

glDisable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,_textureId_three);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 4.0f, 0.0f);
glEnd();
}

glDisable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,_textureId);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 4.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 3.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 3.5f, 0.0f);
glEnd();
}

glDisable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,_textureId_two);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//draw paddle
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(-1.0f+j, -4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(1.0f+j, -4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f+j, -5.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f+j, -5.0f, 0.0f);
glEnd();

glDisable(GL_TEXTURE_2D);

if((x>=3.0f) && (x<=5.0f) && (y>=3.5f) && (y<=4.0f))
{
bricks[3][5]=0;
}

brick_collision();

//draw ball
glColor3f(1.0f,1.0f,1.0f);

glRectf(x,y,x+rsize,y-rsize);

glutSwapBuffers();
}

void brick_collision()
{
if(bricks[3][5]!=0)
{
glColor3f(0.0f,0.0f,0.0f);
glRectf(3.0f,4.0f,5.0f,3.5f);
}
}
[/code]
here is my drawing code and some collision detection
0

Share this post


Link to post
Share on other sites
The problem is right there.
[CODE]for(float i=-5.0f;i<=3.0f;i+=2.0f)[/CODE]

You draw every brick regardless of whether at has been destroyed or not.

If you don’t want a brick to be drawn, [i]don’t draw it[/i]. Don’t draw it and then try to cover it up later, just don’t draw it in the first place.

You should have encapsulated the blocks with classes. Each class would have a position/rectangle coordinates for rendering as well as a boolean flag to tell if the block is drawn for not.

Then the loop would like this:
[CODE]for ( int I = 0; I < ENUM_TOTAL_BLOCKS; ++I ) {
if ( g_bBlock[I].Visible() ) {
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(g_bBlock[I].x, 4.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(g_bBlock[I].x+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(g_bBlock[I].x+2.0f, 3.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(g_bBlock[I].x, 3.5f, 0.0f);
glEnd();
}
}[/CODE]

And brick_collision() would simply not exist.

DO: No drawing of objects that are not visible.
DON’T: Draw invisible objects and then erase them.


L. Spiro
1

Share this post


Link to post
Share on other sites
well I stubbed out the above code, I am still confused on how to declare the g_bBlock[I] statement.
#include <iostream>
#include <glut.h>

using namespace std;

const int ENUM_TOTAL_BLOCKS = 15;

bool g_bBlock[15];

int main()
{

for ( int I = 0; I < ENUM_TOTAL_BLOCKS; ++I ) {
if ( g_bBlock[I].Visible() ) {
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(g_bBlock[I].x, 4.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(g_bBlock[I].x+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(g_bBlock[I].x+2.0f, 3.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(g_bBlock[I].x, 3.5f, 0.0f);
glEnd();
}
}
return 0;
}
0

Share this post


Link to post
Share on other sites
Firstly, if you define ENUM_TOTAL_BLOCKS and use it to run over the array, use it to declare the array too. Don’t use ENUM_TOTAL_BLOCKS in some places and 15 in other places. Use ENUM_TOTAL_BLOCKS everywhere.

 
class CBlock {
public :
     bool     m_bActive;
     float     m_fX;
};
CBlock g_bBlock[ENUM_TOTAL_BLOCKS];
 
 
for ( int I = 0; I < ENUM_TOTAL_BLOCKS; ++I ) {
	 if ( g_bBlock[I].m_bActive ) {
		  glBegin(GL_QUADS);
		  glTexCoord2f(-1.0f, 0.0f);
		  glVertex3f(g_bBlock[I].m_fX, 4.0f, 0.0f);
		  glTexCoord2f(-1.0f, 1.0f);
		  glVertex3f(g_bBlock[I].m_fX+2.0f, 4.0f, 0.0f);
		  glTexCoord2f(0.0f, 1.0f);
		  glVertex3f(g_bBlock[I].m_fX+2.0f, 3.5f, 0.0f);
		  glTexCoord2f(0.0f, 0.0f);
		  glVertex3f(g_bBlock[I].m_fX, 3.5f, 0.0f);
		  glEnd();
	 }
}

You also have to initialize the blocks to make them all active and to set their starting positions.
Obviously, telling you how to do this means holding your hand and is not helping you grow on your own.


L. Spiro Edited by L. Spiro
1

Share this post


Link to post
Share on other sites
I want to thank spiro for all her help. Well I picked apart the following code she gave me. I have made some adjustments to it. What I am confused about is how the g_bBlock array works. All I get 3 5 5 3 for the output.
here is the stubbed out code.
[code]
#include <iostream>
#include <glut.h>

using namespace std;

const int ENUM_TOTAL_BLOCKS = 15;

class CBlock {
public :
bool m_bActive;
float m_fX;
};
CBlock g_bBlock[ENUM_TOTAL_BLOCKS];

int main()
{


for ( int I = 0; I < ENUM_TOTAL_BLOCKS; ++I ) {
if ( g_bBlock[I].m_bActive ) {

glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(g_bBlock[I].m_fX+3.0f, 4.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(g_bBlock[I].m_fX+5.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(g_bBlock[I].m_fX+5.0f, 3.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(g_bBlock[I].m_fX+3.0f, 3.5f, 0.0f);
glEnd();
}
}
cout << g_bBlock[0].m_bActive << endl;
cout << endl;
cout << g_bBlock[0].m_fX+3.0f << endl;
cout << g_bBlock[0].m_fX+5.0f << endl;
cout << g_bBlock[0].m_fX+5.0f << endl;
cout << g_bBlock[0].m_fX+3.0f << endl;

cout << g_bBlock[1].m_bActive << endl;
cout << endl;
cout << g_bBlock[1].m_fX+3.0f << endl;
cout << g_bBlock[1].m_fX+5.0f << endl;
cout << g_bBlock[1].m_fX+5.0f << endl;
cout << g_bBlock[1].m_fX+3.0f << endl;
return 0;
}
[/code]
0

Share this post


Link to post
Share on other sites
You never initialized the data held within g_bBlock. This is a one-time pass over the array to set the default values you want the blocks to have.


L. Spiro
1

Share this post


Link to post
Share on other sites
[CODE]
CBlock g_bBlock[ENUM_TOTAL_BLOCKS] = {
{ false, 0.0f, },
{ false, 1.0f, },

};
[/CODE]


L. Spiro Edited by L. Spiro
0

Share this post


Link to post
Share on other sites
This is the last time I am going to remind you to [color=#ff0000][u][i][b][size=8]PROVIDE SOME INFORMATION[/size][/b][/i][/u][/color].
Look at not just this thread but any other you have started. You state you have a problem, then 2 people ask you to post extra information, and you do but it is not enough so you get more requests for information before you finally post something useful.
Why does it take 6 posts for you but only 1 for everyone else? Why can’t you ask proper questions? Why!?

Here is a very basic and easy-to-follow formula for posting a question/asking for help:[list=1]
[*]State the goal.
[*]State the problem and/or errors.
[*]Provide relevant code if there is any (using the proper CODE tags).
[/list]
From now on, if you can’t run these 3 simple steps through your head when posting, I will ignore your posts.
I am tired of this:
[i]You: I have a problem.[/i]
[b]Us: Provide more information.[/b]
[i]You: Here is 30% of the information you requested.[/i]
[b]Us: Provide more information.[/b]
[i]You: Here is another 30% of the information you requested.[/i]
[b]Us: Provide more information.[/b]
[i]You: Here is the full information.[/i]
[u][Repeat][/u]


L. Spiro
0

Share this post


Link to post
Share on other sites
sorry spiro, here is the code I am still working on.
[code]
void drawScene() {

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

for ( int I = 0; I < ENUM_TOTAL_BLOCKS; ++I ) {
if ( g_bBlock[I].m_bActive ) {
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(g_bBlock[I].m_fX, 4.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(g_bBlock[I].m_fX+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(g_bBlock[I].m_fX+2.0f, 3.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(g_bBlock[I].m_fX, 3.5f, 0.0f);
glEnd();
}
}

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,_textureId_four);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 5.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 5.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 4.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 4.5f, 0.0f);
glEnd();
}

glDisable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,_textureId_three);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 4.0f, 0.0f);
glEnd();
}

glDisable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,_textureId);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 4.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 3.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 3.5f, 0.0f);
glEnd();
}

glDisable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D,_textureId_two);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//draw paddle
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(-1.0f+j, -4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(1.0f+j, -4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f+j, -5.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f+j, -5.0f, 0.0f);
glEnd();

glDisable(GL_TEXTURE_2D);

//draw ball
glColor3f(1.0f,1.0f,1.0f);

glRectf(x,y,x+rsize,y-rsize);

glutSwapBuffers();
}
[/code]
1) I want to the ball to collide with the brick and turn off the brick.
2) The ball only bounces off the screen walls.
3) The code is above.
0

Share this post


Link to post
Share on other sites
Why are you doing “for(float i=-5.0f;i<=3.0f;i+=2.0f)”?
The only code that should be drawing the bricks is the first “for ( int I = 0; I < ENUM_TOTAL_BLOCKS; ++I ) {”.
That was the whole point of that loop. Draw bricks if they are active. Nothing else should be drawing bricks.


L. Spiro
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0