breakout game

Started by
19 comments, last by phil67rpg 11 years, 4 months ago
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);
}

Advertisement
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.
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.

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?
ok yes I would like the ball to have a shallower angle so that it hits off the ceiling more than off the walls.
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.
wow that is a lot to learn, I need to read up on vectors
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.
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.)
cool thanks jtippetts, after much tweaking I finally got my ball to behave properly, now all I have to do is the collision detection.

This topic is closed to new replies.

Advertisement