-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLAGO.cpp
252 lines (195 loc) · 8.06 KB
/
BLAGO.cpp
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
//*****************************************************************************
// 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 Double 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(double R, double m1, double m2, double r1, double r2,
double& x1, double& y1,double& z1,
double& x2, double& y2, double& z2,
double& vx1, double& vy1, double& vz1,
double& vx2, double& vy2, double& vz2,
int& error) {
double pi,r12,m21,d,v,theta2,phi2,st,ct,sp,cp,vx1r,vy1r,vz1r,fvz1r,
thetav,phiv,dr,alpha,beta,sbeta,cbeta,dc,sqs,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;
}
///2D
void collision2Ds(double m1, double m2, double R,
double x1, double y1, double x2, double y2,
double& vx1, double& vy1, double& vx2, double& vy2) {
double m21,dvx2,a,x21,y21,vx21,vy21,fy21,sign,vx_cm,vy_cm;
m21=m2/m1;
x21=x2-x1;
y21=y2-y1;
vx21=vx2-vx1;
vy21=vy2-vy1;
vx_cm = (m1*vx1+m2*vx2)/(m1+m2) ;
vy_cm = (m1*vy1+m2*vy2)/(m1+m2) ;
// *** return old velocities if balls are not approaching ***
if ( (vx21*x21 + vy21*y21) >= 0) return;
// *** I have inserted the following statements to avoid a zero divide;
// (for single precision calculations,
// 1.0E-12 should be replaced by a larger value). **************
fy21=1.0E-12*fabs(y21);
if ( fabs(x21)<fy21 ) {
if (x21<0) { sign=-1; } else { sign=1;}
x21=fy21*sign;
}
// *** update velocities ***
a=y21/x21;
dvx2= -2*(vx21 +a*vy21)/((1+a*a)*(1+m21)) ;
vx2=vx2+dvx2;
vy2=vy2+a*dvx2;
vx1=vx1-m21*dvx2;
vy1=vy1-a*m21*dvx2;
// *** velocity correction for inelastic collisions ***
vx1=(vx1-vx_cm)*R + vx_cm;
vy1=(vy1-vy_cm)*R + vy_cm;
vx2=(vx2-vx_cm)*R + vx_cm;
vy2=(vy2-vy_cm)*R + vy_cm;
return;
}