#1 GDNet+ - Reputation: 542
Posted 09 December 2012 - 10:58 PM
#2 Members - Reputation: 2142
Posted 09 December 2012 - 11:13 PM
How do you store bricks? How do you turn bricks off/on? For both answers, show a bit of code.
#3 GDNet+ - Reputation: 542
Posted 09 December 2012 - 11:17 PM
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();
#4 Members - Reputation: 2142
Posted 10 December 2012 - 01:24 AM
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:
// 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
}
Edited by Endurion, 10 December 2012 - 01:26 AM.
#6 Crossbones+ - Reputation: 5345
Posted 10 December 2012 - 05:37 PM
L. Spiro
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums
#8 Crossbones+ - Reputation: 5345
Posted 10 December 2012 - 05:50 PM
Why don’t you show enough code to include both your update logic and your clear/swap calls?
L. Spiro
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums
#9 GDNet+ - Reputation: 542
Posted 10 December 2012 - 06:25 PM
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);
}
}
here is my drawing code and some collision detection
#10 Crossbones+ - Reputation: 5345
Posted 10 December 2012 - 06:45 PM
for(float i=-5.0f;i<=3.0f;i+=2.0f)
You draw every brick regardless of whether at has been destroyed or not.
If you don’t want a brick to be drawn, don’t draw it. 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:
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();
}
}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
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums
#11 GDNet+ - Reputation: 542
Posted 10 December 2012 - 07:20 PM
#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;
}
#12 Crossbones+ - Reputation: 5345
Posted 10 December 2012 - 07:52 PM
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, 11 January 2013 - 07:32 PM.
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums
#14 GDNet+ - Reputation: 542
Posted 11 December 2012 - 08:01 PM
here is the stubbed out 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;
}
#15 Crossbones+ - Reputation: 5345
Posted 11 December 2012 - 08:16 PM
L. Spiro
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums
#17 Crossbones+ - Reputation: 5345
Posted 11 December 2012 - 09:03 PM
L. Spiro
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums
#19 Crossbones+ - Reputation: 5345
Posted 11 December 2012 - 09:38 PM
CBlock g_bBlock[ENUM_TOTAL_BLOCKS] = {
{ false, 0.0f, },
{ false, 1.0f, },
…
};
L. Spiro
Edited by L. Spiro, 11 December 2012 - 09:38 PM.
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums






