Archived

This topic is now archived and is closed to further replies.

PmanC

pong math... lol

Recommended Posts

i am having trouble with the simplest game around... lol. well anyways, back to the problem. i am having problems with the ball movement. i doesn''t seem to bounce off the walls right. i have tried may things i have seen on this fourm but none of them seem to work. here is my code: glLoadIdentity(); //ballX += (float)sin(ballDir*piover180)*ballInc; //ballY += (float)cos(ballDir*piover180)*ballInc; ballX+=ballInc; ballY+=ballInc; glTranslatef(ballX,ballY,NULL); glBindTexture(GL_TEXTURE_2D, NULL); gluDisk(quadratic,0,8,32,32); if (ballY<=58){ ballInc*=-1; //ballDir = 90-ballDir*piover180*ballSpeed; //ballDir = 180-2*(piover180*(tan(lastPoint.y*ballX))); //lastPoint.x = ballX; //lastPoint.y = ballY; //ballDir = -ballDir-180; } if (ballX<=58){ ballInc*=-1; } if (ballX>=744){ ballInc*=-1; } if (ballY>=544){ ballInc*=-1; } as you can see.. i have tryed a lot of ways but none of them have worked. how would i get the ball to bounce off of the walls? -PmanC

Share this post


Link to post
Share on other sites
Remember, the ball is moving horizontally until it hits the wall, it is also moving vertically until it hits a wall. These are happening independently. The way I do it is have the horizontal and vertical position updated by a speed * direction. So it looks something like this:
These are global:
directionX = 1;
directionY = 1;

Here is the part that does the business of moving the ball:
x+= velocityX * directionX;
y+= velocityY * directionY;

Now the ball is moving to the right and down the screen (since both directions are positive 1), so we need a test to see if they hit the wall:

if(x >= SCREEN_WIDTH || x <= 0)
directionX*=-1;
if(y >= SCREEN_HEIGHT || y <= 0)
directionY*=-1;

Now if a wall is hit, (say the right side of the screen) the x direction gets flipped to negative. Now the ball travels in the other direction. Same if the ball hits a wall in the vertical direction. You need in your code a ballIncX and ballIncY. I hope that was clear ;P

[edited by - Mr Bakbugawk on June 10, 2002 3:14:44 PM]

Share this post


Link to post
Share on other sites