Advertisement

Let me see that Pong! Baby!

Started by May 09, 2002 03:35 PM
4 comments, last by catfoodgood 22 years, 9 months ago
Pong, p, pong, pong, pong. Ok, that was my Pong song. Anyway, here's my little problem. It's less OpenGL, and more general programming, but you guys are getting the question anyway. My user paddle works, and my ball moves like so:
        

glTranslatef(xpos, ypos, 0.0f);

xpos+=0.02f;
ypos-=0.02f;

  

//That's not everything, but when I get down to -6.0f on the y 

//axis (bottom of the playfield) what is the formula to make it 

//go back up at the opposite angle? I tried:

      
if (ypos<=-6.0f)
    ypos+=0.04f;
    
But the ball just went straight along the bottom of the screen. I know the solution is totally simple, but my math sucks. ______________________________ [Free Books!] [Reference and Tutorials] [For Beginners] [IGDA] [OpenGL Tutorials] [Sourceforge] [DirectX Tutorials] [Gamasutra] [edited by - catfoodgood on May 9, 2002 4:35:55 PM] [edited by - catfoodgood on May 9, 2002 4:37:06 PM] [edited by - catfoodgood on May 9, 2002 4:48:44 PM]
Do this instead:

//////////////////
xoffset = 0.02f;
yoffset = 0.02f;

xpos += xoffset;
ypos += yoffset;

if ( ypos <= 6.0f)
yoffset = -yoffset;
//////////////////

You might have to fiddle around with it a bit, but the idea is to reverse the offset, not only adjust its position. The reason your code sends it along the bottom of the screen is because you are still adding 0.2 every tick when you should now be subtracting it.

[edited by - Waverider on May 9, 2002 4:40:53 PM]
It's not what you're taught, it's what you learn.
Advertisement
Waverider: Does there have to be a seperate variable for the offset, because initially I tried what you said, but I just did:


    if(ypos<=-6.0f)  ypos=-ypos;    


But it didn't do anything.?.

Actually I want to add it, because the ball starts on a decline, and goes to -6.0f, and I want it to go back up. Sorry I forgot to put the negative in my first post.

______________________________
[Free Books!] [Reference and Tutorials]
[For Beginners] [IGDA]
[OpenGL Tutorials] [Sourceforge]
[DirectX Tutorials] [Gamasutra]

[edited by - catfoodgood on May 9, 2002 4:49:27 PM]
As it looks, you're always moving the ball down 0.2 "units", regardless of the direction it should go. You'll have to change that to 0.2 units UPwards.

You could use a variable that contains the in/decrement for that and change it when the direction reverses, like:

ydirection = -0.2f;glTranslatef(xpos, ypos, 0.0f);xpos+=0.02f;ypos-=0.02f;if( ypos <= LOWER_BOUND || ypos >= UPPER_BOUND ) {  ydirection *= -1; // reverse direction}  


Edit: OK. Nameserver crashes and you're 8 minutes behind with your answer ;-)

[edited by - Shadowdancer on May 9, 2002 4:50:52 PM]
Note the distinction between yoffset and ypos.

ypos is the ball's position.
yoffset is the amount you move it each tick.

They are separate variables.

You want to reverse the ball's direction, not its position. So reverse yoffset.

[edited by - Waverider on May 9, 2002 4:50:53 PM]
It's not what you're taught, it's what you learn.
Ok, thanks everybody I like all those suggestions. I''ll give them a try. I think I had the right idea to begin with just the wrong implementation.

This topic is closed to new replies.

Advertisement