Skip to main content
GameDev.net gamedev.net
🔒 Locked

OO Pong Clone Help

Started by Taphreek Jan 7, 2001 at 7:58 PM 3 replies 650+ views
Original Post
Taphreek
Taphreek
I am having some problems with my Pong clone. The main thing is that it doesn''t draw when I want it to. The collision detection is also screwey I think but don''t tell me any tips on that . Sorry my code is commented yet but it should be fairly easy to understand. If it helps at all I think the problem may be in the constructor. Thanks! The header file /* This is the header file for my Pong clone */ class Ball { public: GLfloat BallXCoord; GLfloat BallYCoord; GLfloat BallZCoord; float BallXVector; float BallYVector; float Speed; Ball() { BallXCoord = 0.0; BallYCoord = 0.0; BallZCoord = 0.0; BallXVector = 1.0; BallYVector = 0.0; Speed = 0.01; glBegin(GL_TRIANGLES); glVertex3f(BallXCoord, BallYCoord, BallZCoord); glVertex3f(BallXCoord + 0.5, BallYCoord + 0.5, BallZCoord); glVertex3f(BallXCoord + 1.0, BallYCoord, BallZCoord); glEnd(); } ~Ball() { } void SpeedAdjust(Ball BallOne) { BallOne.Speed += 0.01; } void VectorAdjust(Ball BallOne) { BallOne.BallXVector *= BallOne.Speed; BallOne.BallYVector *= BallOne.Speed; } }; class PlayerOnePaddle { private: GLfloat XCoord; GLfloat YCoord; GLfloat ZCoord; GLfloat YLoopCoord; float Score; public: PlayerOnePaddle() { XCoord = 5.0; YCoord = 3.0; ZCoord = 0.0; } /*~PlayerOnePaddle() { }*/ void DrawPaddle() { glBegin(GL_QUADS); glVertex3f(XCoord,YCoord,ZCoord); glVertex3f(XCoord,YCoord,ZCoord); glVertex3f(XCoord - 0.5,-YCoord,ZCoord); glVertex3f(XCoord - 0.5,-YCoord,ZCoord); glEnd(); } void CollisionDetection(Ball BallOne) { YLoopCoord = BallOne.BallYCoord; while(BallOne.BallYCoord < YLoopCoord || BallOne.BallYCoord > (YLoopCoord - 3)) { if(XCoord == BallOne.BallXCoord) { if(YLoopCoord == BallOne.BallYCoord) { BallOne.BallXVector = (BallOne.BallYVector) * -1; BallOne.BallYVector = (BallOne.BallXVector) * -1; } } YLoopCoord -= 0.01; } } void MovePaddleUp() { if(YCoord < 5.0) { YCoord += 0.1; } else { return; } } void MovePaddleDown() { if(YCoord > -5.0) { YCoord -= 0.1; } else { return; } } }; class PlayerTwoPaddle { private: GLfloat XCoord; GLfloat YCoord; GLfloat ZCoord; GLfloat YLoopCoord; float Score; public: PlayerTwoPaddle() { XCoord = -5.0; YCoord = 3.0; ZCoord = 0.0; } ~PlayerTwoPaddle() { } void DrawPaddle() { glColor3f(1.0,1.0,1.0); glBegin(GL_QUADS); glVertex3f(XCoord,YCoord,ZCoord); glVertex3f(XCoord,YCoord,ZCoord); glVertex3f(XCoord - 0.5,-YCoord,ZCoord); glVertex3f(XCoord - 0.5,-YCoord,ZCoord); glEnd(); } void CollisionDetection(Ball BallOne) { YLoopCoord = BallOne.BallYCoord; while(BallOne.BallYCoord < YLoopCoord || BallOne.BallYCoord > (YLoopCoord - 3)) { if(XCoord == BallOne.BallXCoord) { if(YLoopCoord == BallOne.BallYCoord) { BallOne.BallXVector = (BallOne.BallYVector) * -1; BallOne.BallYVector = (BallOne.BallXVector) * -1; } } YLoopCoord -= 0.01; } } }; And the main game loop int GameWorld() { //The paddles PlayerOnePaddle P1Paddle; PlayerTwoPaddle P2Paddle; //Clears the buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Reloads the identity glLoadIdentity(); //Moves into the screen glTranslatef(0.0,0.0,-10.0); //Makes the paddle white glColor3f(1.0,1.0,1.0); //Draws the paddle P1Paddle.DrawPaddle(); return TRUE; } -Taphreek
-Taphreek
FrigidHelix
FrigidHelix
Im not quite sure I understand what you mean by 'it doesnt draw when you want it to'

Edited by - FrigidHelix on January 7, 2001 11:43:52 PM
Taphreek
Taphreek
Well when I call the section like this


PlayerPaddleOne P1Paddle;

P1Paddle.Draw();


It doesnt do the drawing.
Should it be like this?


PlayerOnePaddle P1Paddle;
P1Paddle->Draw();
-Taphreek
_Damage_Inc_
_Damage_Inc_
Hmm, does it draw anything at all to the screen or just not the paddle? If not the problem might be that you are missing the glFlush(); or SwapBuffer(); cmd. Didn''t see that in the code but I might have missed it or it is in another file

/Damage Inc.
Psychocatt
Psychocatt
Not too good with OpenGL, but I have some suggestions. First, in your Draw function, you have it draw a quad, but the first two points are the same and the last two points are the same. May want to try this :

   void DrawPaddle()
{
glBegin(GL_QUADS);
glVertex3f(XCoord,YCoord,ZCoord);
glVertex3f(XCoord - 0.5, ,YCoord,ZCoord);
glVertex3f(XCoord - 0.5,-YCoord,ZCoord);
glVertex3f(XCoord,-YCoord,ZCoord);
glEnd();
}


And, as far as the difference between using ''.'' and ''->''
Only use ''->'' when you have a pointer to your class, otherwise, use the ''.'' Hope that helps

"I kinda think, therefore, I kinda... am?"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.