http://i301.photobucket.com/albums/n.../Prongject.jpg
And the source code that makes it all work:
Code:
#include "Paddle.h"
#include "Ball.h"
#include "Textures.h"
#include "Boundaries.h"
#include "vector2.h"
Paddle paddle1;
Paddle paddle2;
Ball ball;
Boundaries border;
class Mechanics
{
public:
Mechanics(){paddle1.location = Player_1_Start; paddle2.location = Player_2_Start;}
void check_ball_collision(){
vector2 a_min, a_max, b_min, b_max;
if(ball.velocity.x > 0){
a_min = ball.location + ball.d_min;
a_max = ball.location + ball.d_max;
b_min = paddle1.location + paddle1.d_min;
b_max = paddle1.location + paddle1.d_max;
if (collision2d(a_min, a_max, b_min, b_max))
{reverse_x_velocity(ball.velocity);}
else if(ball.location.x > 75)
{reverse_x_velocity(ball.velocity);}}
if(ball.velocity.x < 0){
a_min = ball.location + ball.d_min;
a_max = ball.location + ball.d_max;
b_min = paddle2.location + paddle2.d_min;
b_max = paddle2.location + paddle2.d_max;
if (collision2d(a_min, a_max, b_min, b_max))
{reverse_x_velocity(ball.velocity);}
else if(ball.location.x < -75)
{reverse_x_velocity(ball.velocity);}}
if(ball.velocity.y > 0){
if (ball.location.y > 75)
{reverse_y_velocity(ball.velocity);}}
if(ball.velocity.y < 0){
if (ball.location.y < -75)
{reverse_y_velocity(ball.velocity);}}}
void check_paddle_collision(){
if (paddle1.location.y == 75)
{reverse_y_velocity(paddle1.location);}
else if(paddle1.location.y == -75)
{reverse_y_velocity(paddle1.location);}
if (paddle2.location.y == 75)
{reverse_y_velocity(paddle2.location);}
else if(paddle2.location.y == -75)
{reverse_y_velocity(paddle2.location);}}
private:
bool collision2d(vector2 & a_min, vector2 & a_max, vector2 & b_min, vector2 & b_max) {
return (a_min.x <= b_max.x) && (a_min.y <= b_max.y) && (a_max.x >= b_min.x) && (a_max.y >= b_min.y);}
void reverse_y_velocity(vector2 & velocity)
{velocity.y *= -1;}
void reverse_x_velocity(vector2 & velocity)
{velocity.x *= -1;}
};
Mechanics game;
Code:
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
game.check_ball_collision();
game.check_paddle_collision();
border.draw();
ball.draw(ball.location);
paddle1.draw();
paddle2.draw();
glLoadIdentity();
return TRUE;
}