00001 #include "Collision.h"
00002
00004 int sphereCollisionDetection(vector3Df aLocation, float aRadius, vector3Df bLocation, float bRadius)
00005 {
00006 GLfloat a, b, c, h;
00007 a = aLocation[0] - bLocation[0];
00008 b = aLocation[1] - bLocation[1];
00009 c = aLocation[2] - bLocation[2];
00010
00011 h = a*a + b*b + c*c;
00012
00013 if(aRadius * bRadius >= h)
00014 {
00015 return(1);
00016 }
00017
00018 return(0);
00019 }
00020
00022 int boxAndSpehereCollision( obj *sphere,
00023 const float location[3],
00024 const float XYZ[3])
00025 {
00026 int index, j;
00027 int collided = 0;
00028 float x1, x2, y1, y2, z1, z2;
00029
00030 x1 = location[0]-(XYZ[0]*0.5);
00031 x2 = location[0]+(XYZ[0]*0.5);
00032
00033 y1 = location[1]-(XYZ[1]*0.5);
00034 y2 = location[1]+(XYZ[1]*0.5);
00035
00036 z1 = location[2]-(XYZ[2]*0.5);
00037 z2 = location[2]+(XYZ[2]*0.5);
00038
00039 if(y1 <= sphere->location[1]+sphere->radius &&sphere->location[1]-sphere->radius <= y2)
00040 {
00041 if(x1 <= sphere->location[0]+sphere->radius && sphere->location[0]-sphere->radius <= x2)
00042 {
00043 if(z1 <= sphere->location[2]+sphere->radius && sphere->location[2]-sphere->radius <= z2)
00044 {
00045 return 1;
00046 }
00047 }
00048 }
00049 return 0;
00050 }
00051
00052
00054 int collision222(obj *temp, const float location[3], const float XYZ[3], float coef)
00055 {
00056 int y, i;
00057
00058 if(boxAndSpehereCollision(temp, location, XYZ))
00059 {
00060 y = -1;
00061
00062 for(i = 0; i < 3; i++)
00063 {
00064 temp->location[i] = temp->oldLocation[i];
00065
00066 if(!boxAndSpehereCollision(temp, location, XYZ) && y == -1)
00067 {
00068 y = i;
00069 }
00070 }
00071
00072 temp->velocity[y] = coef*temp->velocity[y];
00073 return 1;
00074 }
00075 return 0;
00076 }
00077
00079 void collisionDetection()
00080 {
00081
00082
00083
00084 const float location[] = {12.5, -0.1, 13};
00085 const float XYZ[] = {25, 0.4, 26};
00086 int i = 1;
00087
00088
00089 const float location2[] = {12.5, 13, -0.1};
00090 const float XYZ2[] = {25.0, 26.0, 0.4};
00091
00092 const float RacketBounding[] = {3.7, 2, 1};
00093
00094 const float netLocation[] = {13, 2.5, 2};
00095 const float NetXYZ[] = {26.0, 5.0, 0.4};
00096
00097
00098 float ballOldVelocity[3];
00099
00100 float tempy;
00101 obj lolcat;
00102 int y = 0;
00103
00104
00105
00106
00107
00108
00109 collision222(&ball, tableDown.center, tableDown.XYZ, tableDown.coef);
00110 collision222(&ball, tableUp.center, tableUp.XYZ, tableUp.coef);
00111
00112 if(collision222(&ball, net.center, net.XYZ, net.coef))
00113 {
00114 printf("Net Collision!!!\n");
00115 }
00116
00117 if(collision222(&ball, racket.location, RacketBounding, -1.0))
00118 {
00119 for(i = 0; i < 3; i++)
00120 {
00121 ballOldVelocity[i] = ball.velocity[i];
00122 }
00123
00124
00125
00126
00127
00128
00129 ball.velocity[0] = ball.velocity[0]+(racket.velocity[0]*1.5);
00130 ball.velocity[1] = -(ball.velocity[1]+(racket.velocity[2]*1.6));
00131 ball.velocity[2] = ball.velocity[2]+(racket.velocity[2]);
00132
00133
00134 racket.velocity[0] = -(ballOldVelocity[0]*0.5);
00135
00136 racket.velocity[2] = -(ballOldVelocity[2]*0.5);
00137
00138
00139 }
00140
00141
00142
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 return;
00157 }
00158
00159
00160 void racketCollision()
00161 {
00162
00163
00164
00165
00166
00167 if (racket.location[0] <= 0.0)
00168 {
00169 racket.velocity[0] = 0;
00170 racket.location[0] = 0;
00171 }
00172 else if (racket.location[0] >= 25.0)
00173 {
00174 racket.velocity[0] = 0;
00175 racket.location[0] = 25;
00176 }
00177
00178 if (racket.location[2] <= 20.0)
00179 {
00180 racket.velocity[2] = 0;
00181 racket.location[2] = 20;
00182 }
00183 else if (racket.location[2] >= 32.0)
00184 {
00185 racket.velocity[2] = 0;
00186 racket.location[2] = 32;
00187 }
00188
00189 if (racket.location[1] <= 2.0)
00190 {
00191 racket.velocity[1] = 0;
00192 racket.location[1] = 2;
00193 }
00194 else if (racket.location[1] >= 10.0)
00195 {
00196 racket.velocity[1] = 0;
00197 racket.location[1] = 9.9;
00198 }
00199 return;
00200 }