Data Structures Help for Simple Pong Game (c++, opengl)

Hi guys. :) I'm a game design student and we're working on this simple pong game in c++, opengl... I wouldn't be asking here but our professor can really be... "unclear" at times. Apparently he wants us to make the points of our paddles (wall1P1x, etc) into data structures. Can anyone help me here? :) I'd really appreciate it. Don't worry, I NEVER copy and paste. I study the content first and do it by myself. I wanna learn.All I know is that there's supposed to the the word 'struct', nothing else.


#include <vector>
#include <time.h>
#include "Glut_Setup.h"

using namespace std;

float const origin = 0.0, pointy = 10.0;

float triP1x = 2.0, triP1y = 1.0, triP1z = 0.0,
triP2x = 6.0, triP2y = 1.0, triP2z = 0.0,
triP3x = 4.0, triP3y = 3.0, triP3z = 0.0;

float ballRadius = 0.5, ballX = 0.0, ballY = 0.0, ballZ = 0.0;

//float p1x = 2.0, p1y = 1.0, p1z = 0.0,
// p2x = 6.0, p2y = 1.0, p2z = 0.0,
// p3x = 4.0, p3y = 3.0, p3z = 0.0;

float wall1P1x = 13.5, wall1P1y = 2.5, wall1P1z = 0.0,
wall1P2x = 15, wall1P2y = 2.5, wall1P2z = 0.0,
wall1P3x = 15, wall1P3y = -2.5, wall1P3z = 0.0,
wall1P4x = 13.5, wall1P4y = -2.5, wall1P4z = 0.0;

float wall2P1x = -13.5, wall2P1y = 2.5, wall2P1z = 0.0,
wall2P2x = -15, wall2P2y = 2.5, wall2P2z = 0.0,
wall2P3x = -15, wall2P3y = -2.5, wall2P3z = 0.0,
wall2P4x = -13.5, wall2P4y = -2.5, wall2P4z = 0.0;

float wall3P1x=-15, wall3P1y = -15, wall3P1z = 0.0,
wall3P2x = -15, wall3P2y = -12, wall3P2z = 0.0,
wall3P3x = 15, wall3P3y = -12, wall3P3z = 0.0,
wall3P4x = 15, wall3P4y = -15, wall3P4z = 0.0;

float wall4P1x= 15, wall4P1y = 15, wall4P1z = 0.0,
wall4P2x = 15, wall4P2y = 12, wall4P2z = 0.0,
wall4P3x = -15, wall4P3y = 12, wall4P3z = 0.0,
wall4P4x = -15, wall4P4y = 15, wall4P4z = 0.0;




float ballULx,
ballULy,
ballLRx,
ballLRy;

float moveX = 0.1, moveY= 0.1;

void GameScene()
{
//_____________________________
// para gumalaw ang ball :D
ballX =ballX+moveX;
ballY =ballY+moveY;
////Diagonally
//_____________________________
//raidus ng bounding box (0.5/-0.5) para sakto sa ball
ballULx=ballX-ballRadius;
ballULy=ballY+ballRadius;
ballLRx=ballX+ballRadius;
ballLRy=ballY-ballRadius;
//____________________________
//ballULx,
//ballULy,
//ballLRx,
//ballLRy;

//_____________________________________________________________________
// these are for the times when the ball bounces at the sides of the paddles
if ((ballULx<=wall2P1x) && (ballULy<=wall2P2y) && (ballLRy>=wall2P3y))
moveX*=-1;

if ((ballLRx>=wall1P1x) && (ballULy<=wall1P1y) && (ballLRy>=wall1P3y))
moveX*=-1;

if (ballLRy<=wall3P2y)
moveY*=-1;

if (ballULy>=wall4P2y)
moveY*=-1;

if (wall1P4x<= wall3P2x)
moveX*=-1;

//____________________________________________________________________

//whaaat O_o di toh yung intention ko pero sige so these are for the times when the ball bounces at the top/bottom of the paddles
//wall1

if ((ballLRx-0.5>=wall1P1x) && (ballLRy+0.5>=wall1P3y) && (ballULy-0.5<=wall1P1y))
moveY*=-1;



//wall2

if ((ballLRx+0.5<=wall2P1x) && (ballLRy-0.5>=wall2P3y) && (ballULy-0.5<=wall2P1y))
moveY*=-1;


//____________________________________________________________________






glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLineWidth(1.0);
glBegin(GL_LINES);

// X-axis
glColor3f(0.0, 0.0, 0.0);
glVertex3f(-10, 0.0, 0.0);
glVertex3f(10, 0.0, 0.0);

// Y-axis
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.0, -10.0, 0.0);
glVertex3f(0.0, 10.0, 0.0);
glEnd();

//glBegin(GL_LINE_LOOP);
//glColor3f(0.0, 0.0, 1.0);
//glVertex3f(triP1x, triP1y, triP1z);
//glVertex3f(triP2x, triP2y, triP2z);
//glVertex3f(triP3x, triP3y, triP3z);
//glEnd();

glBegin(GL_LINE_LOOP);
glColor3f(0.1,0.1,0.1);

glVertex3f(ballULx, ballULy, ballZ);
glVertex3f(ballLRx, ballULy, ballZ);
glVertex3f(ballLRx, ballLRy, ballZ);
glVertex3f(ballULx, ballLRy, ballZ);
glEnd();


glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(wall3P1x, wall3P1y, wall3P1z);
glVertex3f(wall3P2x, wall3P2y, wall3P2z);
glVertex3f(wall3P3x, wall3P3y, wall3P3z);
glVertex3f(wall3P4x, wall3P4y, wall3P4z);

glColor3f(1.0, 0.0, 0.0);
glVertex3f(wall4P1x, wall4P1y, wall4P1z);
glVertex3f(wall4P2x, wall4P2y, wall4P2z);
glVertex3f(wall4P3x, wall4P3y, wall4P3z);
glVertex3f(wall4P4x, wall4P4y, wall4P4z);

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(wall2P1x, wall2P1y, wall2P1z);
glVertex3f(wall2P2x, wall2P2y, wall2P2z);
glVertex3f(wall2P3x, wall2P3y, wall2P3z);
glVertex3f(wall2P4x, wall2P4y, wall2P4z);




glColor3f(1.0, 1.0, 1.0);
glVertex3f(wall1P1x, wall1P1y, wall1P1z);
glVertex3f(wall1P2x, wall1P2y, wall1P2z);
glVertex3f(wall1P3x, wall1P3y, wall1P3z);
glVertex3f(wall1P4x, wall1P4y, wall1P4z);




glEnd();



glPushMatrix();
glTranslatef(ballX, ballY, ballZ);
glColor3f(0.0, 0.0, 1.0);
glutSolidSphere(ballRadius,10, 10);
glPopMatrix();


glutSwapBuffers();
}

void Keys(unsigned char key, int x, int y)
{
switch(key)
{
case 27 : exit(0); break;
//______________________________________
// these are for the controls (up and down)
case 'q': if (wall2P1y>=wall4P2y)
{
wall2P1y=wall2P1y+0;
}

else
{

wall2P1y= wall2P1y+1;
wall2P2y= wall2P2y+1;
wall2P3y= wall2P3y+1;
wall2P4y= wall2P4y+1;

}
break;

case 'a': if (wall2P3y<=wall3P3y)
{
wall2P3y= wall2P3y+0;
}

else
{
wall2P1y= wall2P1y-1;
wall2P2y= wall2P2y-1;
wall2P3y= wall2P3y-1;
wall2P4y= wall2P4y-1;
}
break;

case 'i': if (wall1P2y>=wall4P3y)
{
wall1P2y=wall1P2y+0;
}

else
{
wall1P1y= wall1P1y+1;
wall1P2y= wall1P2y+1;
wall1P3y= wall1P3y+1;
wall1P4y= wall1P4y+1;
}



break;

case 'k': if (wall1P3y<=wall3P2y)
{
wall1P3y=wall1P3y+0;
}
else
{
wall1P1y= wall1P1y-1;
wall1P2y= wall1P2y-1;
wall1P3y= wall1P3y-1;
wall1P4y= wall1P4y-1;
}


break;



}
}





void SpecialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_LEFT: triP1x = triP1x - 0.5;
triP2x = triP2x - 0.5;
triP3x = triP3x - 0.5; break;
case GLUT_KEY_RIGHT: if (triP2x < wall1P1x)
{
triP1x = triP1x + 0.5;
triP2x = triP2x + 0.5;
triP3x = triP3x + 0.5;
}
break;
case GLUT_KEY_UP: triP1y = triP1y + 0.5;
triP2y = triP2y + 0.5;
triP3y = triP3y + 0.5; break;
case GLUT_KEY_DOWN: triP1y = triP1y - 0.5;
triP2y = triP2y - 0.5;
triP3y = triP3y - 0.5; break;

}
}

void Timer(int value)
{
glutTimerFunc(50, Timer, value);
glutPostRedisplay();
}

void changeSize(int w, int h)
{
float ratio = 1.0 * w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void main(int argc, char **argv)
{
srand(time(NULL));
GLUTInit(&argc, argv);
}



Thank you very much! :)


Last edited on
Why use time.h instead of ctime . time.h is the c version, ctime is the c++ version.

A struct is a pretty simple thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//this is like defining your own data type.  It is not an instance of that type.
struct Vec2 {
    float x;
    float y;
};

int main() {
  
  //creating an instance of the type you have defined
   Vec2 point;
  
   //access the fields of the struct with <instance>.<field>
   point.x = .1;

   std::cout << point.x;
}


You could do something like this,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Rectangle {
    Vec2 point[4];
}; 

int main( ) {

   Rectangle paddle;
   /*
   paddle.point[0].x = ;
   paddle.point[0].y = ;
   paddle.point[1].x = ;
   paddle.point[1].y = ;
   paddle.point[2].x = ;
   paddle.point[2].y = ;
   paddle.point[3].x = ;
   paddle.point[4].y = ;
  */
}


Really you should probably be using classes though, but you should learn more about object oriented programming later I hope.

What kind of game design program are you in?
Last edited on
Probably he wants you to make it into a struct, and then create the individual paddles seperately, to prevent data duplication. Here is one way of how to do it, using nested structs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Point {
    float x; 
    float y; 
    float z;
};

struct Paddle {
    Point tr;   // Top Right
    Point tl;   // Top Left
    Point bl;   // Bottom Left
    Point br;   // Bottom Right
};

// Declaring instances
Paddle p1 = 
{
    {13.5,  2.5, 0.0},
    {15.0,  2.5, 0.0},
    {15.0, -2.5, 0.0},
    {13.5, -2.5, 0.0}
}


You should probably try to avoid the ridiculous number of global variables you have, that can get messy (especially in game development, where multithreading is the norm). Also, on a side note, just pointing out that that version of OpenGL has been deprecated for a long time... :)

EDIT:
Ninja'd by htirwin...
Last edited on
Topic archived. No new replies allowed.