Sign in to follow this  

How I make ball bounce??

This topic is 3780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I make a pong ball move until game is finished? it is done with a loop? and how to bounce the ball deppending of where it hit the paddle? I dont know how to make collision detection. Right now my 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);

GLfloat yPos = 0.0;	//Leaft Paddle
GLfloat xPos = 0.0;	//Right Paddle

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) 
{ 
	GLfloat yPos = 0.0;
	GLfloat xPos = 0.0;

	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); 

		glLoadIdentity(); // Reset transformation matrix
		glTranslatef(0, yPos, 0); 

//Left Paddle
	glBegin(GL_QUADS);
		glVertex3f(-1.9f, -0.6f, -5.0f);
		glVertex3f(-1.6f, -0.6f, -5.0f);
		glVertex3f(-1.6f, 0.4f, -5.0f);
		glVertex3f(-1.9f, 0.4f, -5.0f);
	glEnd(); 


	glLoadIdentity(); // Reset the transformation matrix 
	glTranslatef(0,xPos,0);
//Right POaddle
	glBegin(GL_QUADS);
		glVertex3f(1.9f, -0.6f, -5.0f); 
		glVertex3f(1.6f, -0.6f, -5.0f);
		glVertex3f(1.6f, 0.4f, -5.0f);
		glVertex3f(1.9f, 0.4f, -5.0f);
	glEnd(); 


//Ball
	glLoadIdentity();
	glTranslatef( 0, 0, 0);

	glBegin(GL_QUADS);
		glVertex3f(-0.1f, -0.3f, -5.0f); 
		glVertex3f(0.1f, -0.3f, -5.0f);
		glVertex3f(0.1f, -0.1f, -5.0f);
		glVertex3f(-0.1f, -0.1f, -5.0f);
	glEnd(); 

//Begin The top wall
	glLoadIdentity();
	glBegin(GL_QUADS);
		glVertex3f(5.0f, 2.0f, -5.0f); 
		glVertex3f(-2.0f, 2.0f, -5.0f);
		glVertex3f(-2.0f, 1.8f, -5.0f);
		glVertex3f(5.0f, 1.8f, -5.0f);
	glEnd();

glutSwapBuffers();
}

void keyboard(unsigned char key,int a,int b)
{
	switch (key)
	{
	 //Player 1
		case 'W': 
		case 'w':        
                               if(yPos>1.28) //"Up Wall Detection"
                               { 
                                   yPos = 1.28;
                               }			
				yPos += 0.1; // Increase the yPos by 0.1
				break;
		case 'S':
		case 's':		
		        yPos -= 0.1; // Decrease the yPos by 0.1 
				if(yPos<-1.4){ yPos = -1.4; } //"Down Wall Detection"
			break;
	  //Player 2 It is gonna be the Computer
		case 'O':
		case 'o':
			xPos += 0.1; // Increase the yPos by 0.1
			break;
		case 'l':
		case 'L':
			xPos -= 0.1; // Decrease the yPos by 0.1 
			break;
	}
	display();
}

Share this post


Link to post
Share on other sites
Just noticed a few things...I didn't see any attempt to do any collision detection or bouncing ball.

I won't get into collision detection. Google AABB (Axis-Aligned Bounding Boxes).

As for making the ball bounce, it's a simple matter reversing it's velocity (might be wrong word). So if your ball is moving at 1,1 and it hits the top, then you'll make it go 1, -1. This is assuming that above 0 is positive on the Y axis, as in my geometry class...in XNA (not sure about others), however, the upper screen is negative and the lower is positive. C'est tres bizarre.

Changing it's reflection based on where it hits the paddle is currently irrelevant. Get the basic collision working then work on that. :) I'm a firm believer in baby steps. If you don't want to wait, though, just take a pen and paper and write some ideas out. Like if the center of the ball hits the paddle's height minus 40 or something. That's just an example, and probably wouldn't work too well. ;)

Share this post


Link to post
Share on other sites

This topic is 3780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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