Help with collision

So this is my code for pong...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include <conio.h>

#include <glut.h>

int x;
int y;

float BallX = 0.0, BallY = 0.0, BallXY = 0.0;
bool TopHit = false;
bool DownHit = false;

float BX = 0.0, BY = 0.0; //center of ball
float B1X = -1.0, B1Y = 1.0, B2X = 1.0, B2Y = -1.0; //bounding box of ball

float DX = -16.0, DY = 0.0; //center of paddle 3 (DOWN)
float D1X = 2.5, D1Y = -12.5, D2X = -3.5, D2Y = -11.5; //bounding box of paddle 3

float TX = 0, TY = 16.0; // center of paddle 4 (TOP)
float T1X = 2.5, T1Y = 12.5, T2X = -3.5, T2Y = 11.5; // bounding box of paddle 4

float Angle = 0.0f;

void GameScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix();
	glPopMatrix();
	glLineWidth(2.0);
	glColor3f(1.0,1.0,1.0);
	glPushMatrix();
	glBegin(GL_LINE_LOOP);
	glVertex3f(D1X, D1Y, 0.0);
	glVertex3f(D2X, D1Y, 0.0);
	glVertex3f(D2X, D2Y, 0.0);
	glVertex3f(D1X, D2Y, 0.0);
	glEnd();
	glBegin(GL_POINTS);
	glVertex3f(BX, BY, 0.0);
	glEnd();
	glPopMatrix();

	glColor3f(1.0,1.0,1.0);
	glPushMatrix();
	glBegin(GL_LINE_LOOP);
	glVertex3f(T1X, T1Y, 0.0);
	glVertex3f(T2X, T1Y, 0.0);
	glVertex3f(T2X, T2Y, 0.0);
	glVertex3f(T1X, T2Y, 0.0);
	glEnd();
	glPopMatrix();

	glPushMatrix();
	if((B1X <= T2X && B2X <= T2X) && (B1Y >= T2Y && B2Y <= T1Y))
	TopHit = true;

	if((B2X >= D1X && B1X <= D2X) && (B2Y <= D1Y && B1Y >= D2Y))
		DownHit = true;

	if(B1Y <= T1X)
		DownHit = true;
	if(B2Y >= D2Y)
		DownHit = false;

	if(BallY < -5)
		TopHit = true;
	if(BallY > 2)
		TopHit = false;
	
	if(BallY > -5)
		DownHit = true;
	if(BallY < 2)
		DownHit = false;
	if(TopHit)
	{
		BY -= 0.05;
		B1Y -= 0.05;
		B2Y -= 0.05;
	}
	else
	{
		BY += 0.05;
		B1Y += 0.05;
		B2Y += 0.05;
	}
	if(DownHit)
	{
		BallY += 0.05;
		B1Y += 0.05;
		B2Y += 0.05;
	}
	else
	{
		BallY -= 0.05;
		B1Y -= 0.05;
		B2Y -= 0.05;
	}

	//ball
	glPushMatrix();
	glColor3f(1.0,1.00,0);
	glBegin(GL_LINE_LOOP);
	glVertex3f(B1X, B1Y, 0.0);
	glVertex3f(B2X, B1Y, 0.0);
	glVertex3f(B2X, B2Y, 0.0);
	glVertex3f(B1X, B2Y, 0.0);
	glEnd();
	glPopMatrix();

	glutSwapBuffers();
}

void Keys (unsigned char key, int x, int y)
{
	switch (key)
	{
	case 'w':
		TX += 0.7;
		T1X += 0.7;
		T2X += 0.7;

		break;
	case 's':
		TX -= 0.7;
		T1X -= 0.7;
		T2X -= 0.7;

		break;
	case 'd':
		DX += 0.7;
		D1X += 0.7;
		D2X += 0.7;

		break;
	case 'a':
		DX -= 0.7;
		D1X -= 0.7;
		D2X -= 0.7;
		break;
	}
}

void ResizeWindow(int w, int h)
{
	float ratio = 1.0 * w / h;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glViewport(0,0,w,h);
	gluPerspective(50.0, ratio, 1, 1000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	gluLookAt(
		0.0, 00.0, 65.0, //camera position
		0.0, 0.0, 0.0, // where the camera is looking
		0.0, 1.0, 0.0); //direction of up
}

void main(int argc, char **argv)
{
	//OpenGL Setup
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

	//setup drawing window
	glutInitWindowPosition(0,0);
	glutInitWindowSize(500,500);
	glutCreateWindow("One Player Pong");

	//Resize objects in the OpenGL window
	glutReshapeFunc(ResizeWindow);
	glutDisplayFunc(GameScene);
	glutIdleFunc(GameScene);

	//*Let there be light! */
	//glEnable (GL_LIGHT0);
	//glEnable (GL_LIGHTING);
	//gl

	glEnable(GL_DEPTH_TEST);

	//Starts OpenGL
	glutKeyboardFunc(Keys);
	glutMainLoop();
}



and I believe this is the collision part...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
if((B1X <= T2X && B2X <= T2X) && (B1Y >= T2Y && B2Y <= T1Y))
	TopHit = true;

	if((B2X >= D1X && B1X <= D2X) && (B2Y <= D1Y && B1Y >= D2Y))
		DownHit = true;

	if(B1Y <= T1X)
		DownHit = true;
	if(B2Y >= D2Y)
		DownHit = false;

	if(BallY < -5)
		TopHit = true;
	if(BallY > 2)
		TopHit = false;
	
	if(BallY > -5)
		DownHit = true;
	if(BallY < 2)
		DownHit = false;
	if(TopHit)
	{
		BY -= 0.05;
		B1Y -= 0.05;
		B2Y -= 0.05;
	}
	else
	{
		BY += 0.05;
		B1Y += 0.05;
		B2Y += 0.05;
	}
	if(DownHit)
	{
		BallY += 0.05;
		B1Y += 0.05;
		B2Y += 0.05;
	}
	else
	{
		BallY -= 0.05;
		B1Y -= 0.05;
		B2Y -= 0.05;
	}


After several tries on trying to fix that area and trying to see what went wrong, I dont know what else I can do. Could anyone point out what are my mistakes and where did i go wrong? And what do you think should I replace it with?

Another question, how do I make the ball (which is actually the square box) move in a limited area freely? So that I would make it look like it's real pong.
Topic archived. No new replies allowed.