-
Notifications
You must be signed in to change notification settings - Fork 160
/
common.hpp
331 lines (257 loc) · 9.09 KB
/
common.hpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
We should also look into: https://github.com/g-truc/glm
*/
#ifndef COMMON
#define COMMON
#include <iostream>
GLfloat WHITE[] = {1.0, 1.0, 1.0};
GLfloat GRAY[] = {0.5, 0.5, 0.5};
GLfloat DARK_GRAY[] = {0.15, 0.15, 0.15};
GLfloat BLACK[] = {0.0, 0.0, 0.0};
GLfloat RED[] = {1.0, 0.0, 0.0};
GLfloat GREEN[] = {0.0, 1.0, 0.0};
GLfloat BLUE[] = {0.0, 0.0, 1.0};
GLfloat SPHERE_RADIUS = 0.2;
GLint SPHERE_SLICES = 10;
GLint SPHERE_STACKS = 10;
class Vector3D{
public:
GLfloat x;
GLfloat y;
GLfloat z;
Vector3D() {
this->x = 0.0;
this->y = 0.0;
this->z = 0.0;
}
Vector3D(GLfloat x, GLfloat y, GLfloat z) {
this->x = x;
this->y = y;
this->z = z;
}
Vector3D operator+(Vector3D otherv) {
return Vector3D( this->x + otherv.x, this->y + otherv.y, this->z + otherv.z);
}
Vector3D operator-(Vector3D otherv) {
return Vector3D( this->x - otherv.x, this->y - otherv.y, this->z - otherv.z);
}
Vector3D operator*(GLfloat a) {
return Vector3D( this->x * a, this->y * a, this->z * a);
}
Vector3D operator/(GLfloat a) {
return Vector3D( this->x / a, this->y / a, this->z / a);
}
GLfloat dot(Vector3D otherv) {
return this->x * otherv.x + this->y * otherv.y + this->z * otherv.z;
}
GLfloat norm() {
return sqrt( this->dot(*this) );
}
// TODO
GLfloat taxi_norm() {
//return max(abs(x), abs(y), abs(z));
return 0.0;
}
Vector3D unit() {
return (*this) / this->norm();
}
GLfloat eucl(Vector3D other) {
return (*this - other).norm();
}
std::string str() {
char out[64];
sprintf(out, "%4.2f\n%4.2f\n%4.2f\n", x, y, z);
return std::string(out);
}
};
class Sphere {
public:
Vector3D center;
Vector3D speed;
GLfloat* color;
Sphere() {
this->center = Vector3D();
this->speed = Vector3D();
this->color = new GLfloat[3];
this->color[0] = 0.0;
this->color[1] = 0.0;
this->color[2] = 0.0;
}
Sphere(Vector3D center, Vector3D speed, GLfloat color[]) {
this->center = center;
this->speed = speed;
this->color = new GLfloat[3];
this->color[0] = color[0];
this->color[1] = color[1];
this->color[2] = color[2];
}
/* Draw the sphere. */
void draw(){
glPushMatrix();
glTranslatef( center.x, center.y, center.z );
glLineWidth(1.0);
glColor3fv(BLACK);
/* TODO don't hardcode this here! Use the constructor! */
glutWireSphere(SPHERE_RADIUS, SPHERE_SLICES, SPHERE_STACKS);
glColor3fv(color);
glutSolidSphere(SPHERE_RADIUS, SPHERE_SLICES, SPHERE_STACKS);
glPopMatrix();
}
};
float rand_range(float a, float b) {
return ((b-a)*((float)rand()/RAND_MAX)) + a;
}
/*
http://www.plasmaphysics.org.uk/programs/coll3d_cpp.htm
This program is a 'remote' 3D-collision detector for two balls on linear
trajectories and returns, if applicable, the location of the collision for
both balls as well as the new velocity vectors (assuming a partially elastic
collision as defined by the restitution coefficient).
All variables apart from 'error' are of float Precision Floating Point type.
The Parameters are:
R (restitution coefficient) between 0 and 1 (1=perfectly elastic collision)
m1 (mass of ball 1)
m2 (mass of ball 2)
r1 (radius of ball 1)
r2 (radius of ball 2)
& x1 (x-coordinate of ball 1)
& y1 (y-coordinate of ball 1)
& z1 (z-coordinate of ball 1)
& x2 (x-coordinate of ball 2)
& y2 (y-coordinate of ball 2)
& z2 (z-coordinate of ball 2)
& vx1 (velocity x-component of ball 1)
& vy1 (velocity y-component of ball 1)
& vz1 (velocity z-component of ball 1)
& vx2 (velocity x-component of ball 2)
& vy2 (velocity y-component of ball 2)
& vz2 (velocity z-component of ball 2)
& error (int) (0: no error
1: balls do not collide
2: initial positions impossible (balls overlap))
Note that the parameters with an ampersand (&) are passed by reference,
i.e. the corresponding arguments in the calling program will be updated
(the positions and velocities however only if 'error'=0).
All variables should have the same data types in the calling program
and all should be initialized before calling the function.
This program is free to use for everybody. However, you use it at your own
risk and I do not accept any liability resulting from incorrect behaviour.
I have tested the program for numerous cases and I could not see anything
wrong with it but I can not guarantee that it is bug-free under any
circumstances.
I would appreciate if you could report any problems to me
(for contact details see http://www.plasmaphysics.org.uk/feedback.htm ).
Thomas Smid February 2004
December 2005 (a few minor changes to improve speed)
December 2009 (generalization to partially inelastic collisions)
July 2011 (fix for possible rounding errors)
*/
void collision3D(float R, float m1, float m2, float r1, float r2,
float& x1, float& y1,float& z1,
float& x2, float& y2, float& z2,
float& vx1, float& vy1, float& vz1,
float& vx2, float& vy2, float& vz2,
int& error) {
float pi,r12,m21,d,v,theta2,phi2,st,ct,sp,cp,vx1r,vy1r,vz1r,fvz1r,
thetav,phiv,dr,alpha,beta,sbeta,cbeta,t,a,dvz2,
vx2r,vy2r,vz2r,x21,y21,z21,vx21,vy21,vz21,vx_cm,vy_cm,vz_cm;
// **** initialize some variables ****
pi=acos(-1.0E0);
error=0;
r12=r1+r2;
m21=m2/m1;
x21=x2-x1;
y21=y2-y1;
z21=z2-z1;
vx21=vx2-vx1;
vy21=vy2-vy1;
vz21=vz2-vz1;
vx_cm = (m1*vx1+m2*vx2)/(m1+m2) ;
vy_cm = (m1*vy1+m2*vy2)/(m1+m2) ;
vz_cm = (m1*vz1+m2*vz2)/(m1+m2) ;
// **** calculate relative distance and relative speed ***
d=sqrt(x21*x21 +y21*y21 +z21*z21);
v=sqrt(vx21*vx21 +vy21*vy21 +vz21*vz21);
// **** return if distance between balls smaller than sum of radii ****
if (d<r12) {error=2; return;}
// **** return if relative speed = 0 ****
if (v==0) {error=1; return;}
// **** shift coordinate system so that ball 1 is at the origin ***
x2=x21;
y2=y21;
z2=z21;
// **** boost coordinate system so that ball 2 is resting ***
vx1=-vx21;
vy1=-vy21;
vz1=-vz21;
// **** find the polar coordinates of the location of ball 2 ***
theta2=acos(z2/d);
if (x2==0 && y2==0) {phi2=0;} else {phi2=atan2(y2,x2);}
st=sin(theta2);
ct=cos(theta2);
sp=sin(phi2);
cp=cos(phi2);
// **** express the velocity vector of ball 1 in a rotated coordinate
// system where ball 2 lies on the z-axis ******
vx1r=ct*cp*vx1+ct*sp*vy1-st*vz1;
vy1r=cp*vy1-sp*vx1;
vz1r=st*cp*vx1+st*sp*vy1+ct*vz1;
fvz1r = vz1r/v ;
if (fvz1r>1) {fvz1r=1;} // fix for possible rounding errors
else if (fvz1r<-1) {fvz1r=-1;}
thetav=acos(fvz1r);
if (vx1r==0 && vy1r==0) {phiv=0;} else {phiv=atan2(vy1r,vx1r);}
// **** calculate the normalized impact parameter ***
dr=d*sin(thetav)/r12;
// **** return old positions and velocities if balls do not collide ***
if (thetav>pi/2 || fabs(dr)>1) {
x2=x2+x1;
y2=y2+y1;
z2=z2+z1;
vx1=vx1+vx2;
vy1=vy1+vy2;
vz1=vz1+vz2;
error=1;
return;
}
// **** calculate impact angles if balls do collide ***
alpha=asin(-dr);
beta=phiv;
sbeta=sin(beta);
cbeta=cos(beta);
// **** calculate time to collision ***
t=(d*cos(thetav) -r12*sqrt(1-dr*dr))/v;
// **** update positions and reverse the coordinate shift ***
x2=x2+vx2*t +x1;
y2=y2+vy2*t +y1;
z2=z2+vz2*t +z1;
x1=(vx1+vx2)*t +x1;
y1=(vy1+vy2)*t +y1;
z1=(vz1+vz2)*t +z1;
// *** update velocities ***
a=tan(thetav+alpha);
dvz2=2*(vz1r+a*(cbeta*vx1r+sbeta*vy1r))/((1+a*a)*(1+m21));
vz2r=dvz2;
vx2r=a*cbeta*dvz2;
vy2r=a*sbeta*dvz2;
vz1r=vz1r-m21*vz2r;
vx1r=vx1r-m21*vx2r;
vy1r=vy1r-m21*vy2r;
// **** rotate the velocity vectors back and add the initial velocity
// vector of ball 2 to retrieve the original coordinate system ****
vx1=ct*cp*vx1r-sp*vy1r+st*cp*vz1r +vx2;
vy1=ct*sp*vx1r+cp*vy1r+st*sp*vz1r +vy2;
vz1=ct*vz1r-st*vx1r +vz2;
vx2=ct*cp*vx2r-sp*vy2r+st*cp*vz2r +vx2;
vy2=ct*sp*vx2r+cp*vy2r+st*sp*vz2r +vy2;
vz2=ct*vz2r-st*vx2r +vz2;
// *** velocity correction for inelastic collisions ***
vx1=(vx1-vx_cm)*R + vx_cm;
vy1=(vy1-vy_cm)*R + vy_cm;
vz1=(vz1-vz_cm)*R + vz_cm;
vx2=(vx2-vx_cm)*R + vx_cm;
vy2=(vy2-vy_cm)*R + vy_cm;
vz2=(vz2-vz_cm)*R + vz_cm;
return;
}
#endif