• Create Account

## breakout game

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

20 replies to this topic

### #1phil67rpg  Members

Posted 21 November 2012 - 10:34 PM

I am working on a breakout game using texture mapping and OpenGL. I have the paddle working but when I move it the bricks disappear. The problem is probably related to the buffer swapping command.
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glEnable(GL_TEXTURE_2D);
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
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();
}
for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
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();
}
for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
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();
}
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(-1.0f, -4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(1.0f, -4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, -5.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -5.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);

glutSwapBuffers();
}
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexCoord2f(-1.0f, 0.0f);
glVertex3f(-1.0f+i, -4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(1.0f+i, -4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f+i, -5.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0+i, -5.0f, 0.0f);
glEnd();
i-=0.05f;
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}

hopefully this is not too much.

### #2Trienco  Members

Posted 21 November 2012 - 11:44 PM

Why would you expect anything else if you clear the buffer, draw only the paddle and then display only the paddle?

How does this even compile if 'i' isn't declared in paddle_left (and if it's global... can you tell why naming a global variable "i" is just incredibly bad and confusing?)

And how is this ever going to work if the next time you draw your scene you will always draw the paddle in exactly the same position, no matter what?

How will you ever "remove" a brick if drawing them is hard coded like that?

Why is that loop copy pasted for every row of bricks and how will you ever be able to have any other number of rows in a level?

You need to take a very big step back and realize that the stuff drawn on the screen isn't the "game", but takes the current state of the game and visualizes it. AI players (or psychics) should be able to play without needing ANY kind of graphics of drawing. In one nice Google friendly acronym: "MVC".

A buffer in OpenGL isn't a stage where you put stuff and then move it around, it's a ton of pixels that need to be rendered again for every frame (unless you really know what you're doing and invest a lot of head ache to optimize something that absolutely doesn't need to be optimized).
f@dzhttp://festini.device-zero.de

### #3phil67rpg  Members

Posted 22 November 2012 - 07:53 PM

I have worked on this a lot, can I get another hint on what to do.

### #4SiCrane  Moderators

Posted 22 November 2012 - 08:48 PM

If you don't understand what he is saying, then you should ask a specific question about his post.

### #5phil67rpg  Members

Posted 22 November 2012 - 09:11 PM

Why would you expect anything else if you clear the buffer, draw only the paddle and then display only the paddle?

can you please clarify what you mean by only drawing the paddle and the bricks.

### #6JTippetts  Moderators

Posted 22 November 2012 - 11:51 PM

POPULAR

I assume you call paddle_left whenever the paddle is supposed to move left? That's why your bricks disappear. As soon as paddle_left is called, the buffer is cleared and only the paddle is drawn. Anything else that was drawn (perhaps during a previous call to draw_scene) is wiped when glClear is called inside paddle_left.

What you are doing here is just... no. There should be one place, and one place only where things are drawn. The proper place is not inside whatever logic you use to make paddles move. You might want to take a break and do some research on basic game loops: how they're structured, what pieces go where, etc... Mixing logic and rendering like you are doing here is just not going to cut it.

Typically, a loop proceeds through a set of tasks in a pre-defined order:

1) Call Time() to get the start of frame time

2) Process any waiting input events, and hand the results off to interested parties. Here you might call functionality to move your paddle, for instance by adding or subtracting a value from its position. Only that, though; no drawing, no buffer clearing. Just input handling.

3) Update logic. How this is done varies; some setups will update logic on a fixed time step (every, say, 1/60th of a second) while others will compare the current Time() against the Time() of the last logic update, and use the difference as a step length. In this stage, only logic is performed: things are moved, decisions are made. No drawing. None whatsoever. Doesn't belong here, either.

4) Update collisions. This process might be interleaved with your physics step, depending on how you are doing your physics. At any rate, here you account for the things that happen when a collision occurs. Break blocks, bounce balls, check for ball out of bounds and decrement ball counter, and so on. Again, no rendering. These stages of the loop don't care what goes on the screen. As Trienco indicated, they should do what they do even if nothing is ever drawn on the screen. Any drawing code in these sections should actually generate compile errors, because there is absolutely no reason these modules should even know what OpenGL is. That's how you avoid problems.

5) Render. Aha! Here we go, now that everything has moved to its new location, we can draw the screen. This part of the loop doesn't care about logic or physics or things moving or input being handled. If you are doing any of that here, you are doing the wrong thing. The only thing this part of the loop cares about is drawing the stuff it's told to draw on the screen. There should be exactly one call to glClear() and one call (after drawing is done) to swap buffers. Any more and you are just going to eradicate work already done. Clear the buffer, draw your game, draw your UI, swap buffers.

6) Repeat.

There are many variations to the loop, but that is the basic gist of it. You might want to read the usual suspects when it comes to game loops (Gaffer's fixed time step article, the Bullet physics canonical game loop, etc...) in order to really understand the processes and how the different sections need to be separated. Make any number of throwaway applications as you need to get this figured out. Without this basic understanding, you are just doomed to repeat your past failures and cause yourself more frustration.

### #7phil67rpg  Members

Posted 23 November 2012 - 01:19 PM

wow that is a lot to digest, thanks for all the help, I will do some research on game loops.

### #8phil67rpg  Members

Posted 23 November 2012 - 04:31 PM

assume you call paddle_left whenever the paddle is supposed to move left? That's why your bricks disappear. As soon as paddle_left is called, the buffer is cleared and only the paddle is drawn. Anything else that was drawn (perhaps during a previous call to draw_scene) is wiped when glClear is called inside paddle_left.

I am still working on this problem.

### #9phil67rpg  Members

Posted 23 November 2012 - 10:24 PM

well according to tippets I should put all my rendering inside the drawscene function. I should do no drawing inside the paddle left function. My question is what should I put in my paddle left function.

### #10phil67rpg  Members

Posted 23 November 2012 - 11:13 PM

well tippets is right on, thanks a lot I finally figured it out.

### #11phil67rpg  Members

Posted 24 November 2012 - 07:44 PM

google is my friend, I have done a lot of research on this topic. I am trying to move a ball in a breakout game. I am unsure of how to proceed.here Is the code I am working on.
void Timer(int value)
{
if(x > windowWidth - rsize || x < -windowWidth || y > windowHeight)
{
xstep = -xstep;
ystep = -ystep;
}

x += xstep;
y += ystep;

glutPostRedisplay();

glutTimerFunc(20,Timer,1);
}


### #12SiCrane  Moderators

Posted 24 November 2012 - 09:17 PM

I don't know why this is so hard for you to understand: if you make a post and you have a question, then you need to actually ask the question. If you have an error you can't figure out, post the error. If you have a problem, describe the problem. This, at a minimum, includes both what you want to happen and what is happening instead. Just posting code and saying that you are having troubles still doesn't cut it.

Note that this isn't just frustration at your extremely poor communication skills. The minimum information for formulating a proper post is also the minimum information for properly examining a problem on your own. You need to develop the skill of breaking down a problem into smaller chunks, and the very first step of that is understanding the problem well enough to describe it.

### #13phil67rpg  Members

Posted 24 November 2012 - 09:39 PM

ok this code works, kind of. It causes a ball to move from the lower left side of the screen and to move to the upper right side and then back again. what I want it to do is move more in the middle of the screen, I am using a cartesion coordinate system. The origin is at the center of the screen. I am using gluOrtho2D(-5.0,5.0,-5.0,5.0) to set the edges of the screen. With a small amount of code change I can get the ball to move from the upper right to the lower left of the screen.

### #14SiCrane  Moderators

Posted 25 November 2012 - 11:24 AM

what I want it to do is move more in the middle of the screen

Be more precise. How do you want the ball to do this? Would you like a shallower angle so that it bounces between the walls more often before it hits the ceiling? Do you want the ball to bend towards the center as it travels like there's a black hole there?

### #15phil67rpg  Members

Posted 25 November 2012 - 03:01 PM

ok yes I would like the ball to have a shallower angle so that it hits off the ceiling more than off the walls.

### #16JTippetts  Moderators

Posted 25 November 2012 - 03:46 PM

Your xstep and ystep values together constitute what is called a vector. The word vector has quite a few different but related meanings, but in this context you can think of it as Direction + Magnitude. If you imagine the vector as an arrow, the direction of course is the direction the arrow is pointing, and the magnitude is the arrow's length. If xstep=1 and ystep=1, the resulting vector is (1,1). It can be conceptualized as an arrow that is pointing up and to the right (assuming the origin, (0,0), to be in the lower left corner of the screen) at a 45 degree angle. The magnitude of the vector is approximately 1.414. (The magnitude, or length, of course is calculated by the Pythagorean theorem.) So if you add xstep and ystep to the ball's current position, the result is to move it up and right a distance of 1.414 units.

To get the ball to move in other directions, you merely change the values of xstep and ystep to point the arrow in different directions. xstep=-1, ystep=1 will move it up and to the left. xstep=0, ystep=1 will move it straight up. xstep=1, ystep=0 will move it straight right. And so forth.

Of course, you also need the ball to move at a consistent speed (denoted by the magnitude of the vector) regardless of what direction it is pointing. A vector of (1,1) is not the same length as the vector (0,1), so a ball moving along the vector (1,1) will move faster than along the vector (0,1). To fix this, you need to normalize the vector; ie, convert the vector to what is called a unit vector, or a vector whose magnitude is 1. This is simple enough to do, you simply divide xstep and ystep by the magnitude of the vector.

Once your vector is normalized to unit length, then you can scale it by the ball's speed (by multiplying xstep and ystep by speed) before using it to move the ball. One way of doing this is to encapsulate xstep, ystep and speed into some sort of ball structure, which is far preferable than having xstep and ystep live globally in your program. Then you can just call a method on the ball structure/class to move the ball. Something like this:

class Ball
{
public:
Ball(float x, float y, float vx, float vy, float speed) : x_(x), y_(y), speed_(speed)
{
setDirection(vx,vy);
}
~Ball(){}

void setPosition(float x, float y)
{
x_=x;
y_=y;
}

void setDirection(float vx, float vy)
{
vx_=vx;
vy_=vy;
float len=sqrt(vx*vx+vy*vy);
vx/=len;
vy/=len;
}

void move()
{
x_+=vx_*speed_;
y_+=vy_*speed_;

// Check to see if it hit the sides, and reflect the vector if so
if(x_<0 || x_>ScreenWidth) vx_*=-1.0f;
if(y_<0 || y_>ScreenHeight) vy_*=-1.0f
}

private:
float x_, y_, speed_,  vx_, vy_;
};


This is just a quickie, of course, but it shows how the Ball class encapsulates everything it needs to move. Then in your timer function, instead of explicitly performing the movement and checks there, you can simply call Ball.move() to have the ball update itself.


void timer(int value)
{
ball.move();
glutPostRedisplay();
glutTimerFunc(20, timer, 1);
}



There is no reason any of the ball's internal logic should be in timer() itself; that should be safely encapsulated inside Ball, so that the timer function doesn't have to worry about it. The timer function should just be calling logic update methods, and letting the object logic handle itself. It's much cleaner and far more flexible this way.

### #17phil67rpg  Members

Posted 25 November 2012 - 05:34 PM

wow that is a lot to learn, I need to read up on vectors

### #18phil67rpg  Members

Posted 27 November 2012 - 08:39 PM

I have stubbed out the above code. It works good but I have a question. The x and y variables are the ball's position and I think vx and vy are the balls velocity. What I don't know is what the x_ and _y and speed_ variables are. I have studied this code in depth and I know that setDirection is a function that normalizes the vector associated with the ball.

### #19JTippetts  Moderators

Posted 27 November 2012 - 09:19 PM

x_, y_, vx_ and vy_ are class members. They're the actual ball position and velocity. That should be apparent by the fact that setPosition() sets x_ and y_, and setDirection() sets vx_ and vy_. (Appending a _ for class members is a convention of mine I picked up who knows where to allow me to quickly identify members while reading code.)

### #20phil67rpg  Members

Posted 28 November 2012 - 09:54 PM

cool thanks jtippetts, after much tweaking I finally got my ball to behave properly, now all I have to do is the collision detection.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.