Sign in to follow this  
alx123

Moving one pong paddle

Recommended Posts

This is my first opengl game a lot of parts of the code are from other code but I did somethings I want to know how can I move only one paddle? Here is the code:
#include <iostream>
#include <glut.h>
void init();
void display();
void keyboard(unsigned char key,int a,int b);
void handleResize(int w,int y);

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400); 
	
	glutCreateWindow("Pong");
	init(); 
	glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);
	glutReshapeFunc(handleResize);
	
	glutMainLoop(); 
}

void init() {	glEnable(GL_DEPTH_TEST);    }


void handleResize(int w, int h) 
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 
	gluPerspective(45.0,(double)w / (double)h, 1.0,     200.0);                
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW); 
	
 glBegin(GL_QUADS);
	 glVertex3f(-1.9f, -1.5f, -5.0f);
	 glVertex3f(-1.6f, -1.5f, -5.0f);
     glVertex3f(-1.6f, -0.5f, -5.0f);
     glVertex3f(-1.9f, -0.5f, -5.0f);

	 glVertex3f(1.9f, -1.5f, -5.0f);
	 glVertex3f(1.6f, -1.5f, -5.0f);
     glVertex3f(1.6f, -0.5f, -5.0f);
     glVertex3f(1.9f, -0.5f, -5.0f);
 glEnd(); 
	glutSwapBuffers();
}

void keyboard(unsigned char key,int a,int b)
{
	switch (key)
	{
	case 'W':
	case 'w':
		glTranslatef(0.0, 0.1, 0.0); 
		break;
	case 'S':
	case 's':
		glTranslatef(0.0, -0.1, 0.0); 
		break;
	}

	display();
}

Everitime I move the paddle it moves both I only want to move one Thanks in advice

Share this post


Link to post
Share on other sites
Hey,

The reason you're having this problem is because you are translating both paddles when you call: glTranslatef(0.0, 0.1, 0.0); What this does is it modifies your transformation matrix (the matrix which causes your objects to move and rotate). This matrix is used to transform all of your objects. What you want to do is call glTranslatef before you draw your first paddle, then reset the transformation matrix before drawing the second.

The way you do this is by calling glLoadIdentity(); (load the identity matrix) which effectively resets your transformation matrix. However, in the case of your code, you will need to keep track of of your paddles current location and then translate relative to that. This is how it should look:

First declare: GLfloat yPos = 0; right below void handleResize(int w,int y); and above your main method declaration.

Then modify the following methods according to what I did (read the comments).

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Reset transformation matrix

glTranslatef(0, yPos, 0); // Translate the y coordinate accordingly
glBegin(GL_QUADS);
glVertex3f(-1.9f, -1.5f, -5.0f); // This quad will be translated
glVertex3f(-1.6f, -1.5f, -5.0f);
glVertex3f(-1.6f, -0.5f, -5.0f);
glVertex3f(-1.9f, -0.5f, -5.0f);

glLoadIdentity(); // Reset the transformation matrix (so that the translation is cleared from the matrix)
glVertex3f(1.9f, -1.5f, -5.0f); // This quad will not be translated
glVertex3f(1.6f, -1.5f, -5.0f);
glVertex3f(1.6f, -0.5f, -5.0f);
glVertex3f(1.9f, -0.5f, -5.0f);
glEnd();
glutSwapBuffers();
}

void keyboard(unsigned char key,int a,int b)
{
switch (key)
{
case 'W':
case 'w':
yPos += 0.1; // Increase the yPos by 0.1
break;
case 'S':
case 's':
yPos -= 0.1; // Decrease the yPos by 0.1
break;
}

display();
}

It should work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this