-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysics.java
More file actions
235 lines (166 loc) · 7 KB
/
Physics.java
File metadata and controls
235 lines (166 loc) · 7 KB
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
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
*
* @author Alex Weeks
*
*/
public class Physics {
public static final double DOUBLE_THRESHOLD = 0.000000000001;
/**
* Updates the velocity vectors of both actors to those the moment after a collision. Does not check for an actual collision, will give incorrect results if called for actors that are not very close to each other.
* @param b1 The first ball
* @param b2 The second ball
*/
public static void doElasticCollision( Ball b1, Ball b2 ) {
double m1 = b1.mass;
double m2 = b2.mass;
//Compute the basis vector upon which all further calculations will be based.
//The unit position vector from b1 to b2
DoubleVector pHat = ( b2.pos.subtract(b1.pos) ).unitVector();
//Compute initial scalar velocities of actor1 and actor2 in the new coordinate system
double v1Init = DoubleVector.dotProduct(b1.vel, pHat);
double v2Init = DoubleVector.dotProduct(b2.vel, pHat);
//Compute the vector components of actor1 and actor2 parallel to the surface of collision
//Will not be affected by the collision
DoubleVector b1ParVel = b1.vel.subtract( pHat.scalarMult( v1Init ) );
DoubleVector b2ParVel = b2.vel.subtract( pHat.scalarMult( v2Init ) );
//Compute final scalar velocities of actor1 and actor2 in the new coordinate system
double v1Final = ( v1Init * ( m1 - m2 ) + 2 * m2 * v2Init ) / ( m1 + m2 );
double v2Final = ( v2Init * ( m2 - m1 ) + 2 * m1 * v1Init ) / ( m1 + m2 );
//Put everything back together to yield the final vector velocities for both objects:
b1.vel = b1ParVel.add( pHat.scalarMult( v1Final ) );
b2.vel = b2ParVel.add( pHat.scalarMult( v2Final ) );
}
/**
* Performs a wall collision
* @param ball The ball
* @param compIndex Direction of the collision
*/
public static void doWallCollision(Ball ball, int compIndex ) {
ball.vel.setComp( compIndex, -1 * ball.vel.getComp( compIndex ) );
}
/**
*
* @param ball The ball
* @param lowerBounds Lower boundary vector
* @param upperBounds Upper boundary vector
* @param accelVec acceleration (gravity) vector of the Universe
* @return Returns a new Collision object
*/
public static Collision checkWallCollision( Ball ball, DoubleVector lowerBounds, DoubleVector upperBounds, DoubleVector accelVec ) {
double r = ball.radius;
ArrayList<Collision> collisions = new ArrayList<Collision>();
Double lowerT = null;
Double upperT = null;
//Check collisions
for( int n = 0; n < lowerBounds.order; n++ ) {
double a = accelVec.getComp( n );
double v = ball.vel.getComp( n );
double p = ball.pos.getComp( n );
double lowerBound = lowerBounds.getComp( n );
double upperBound = upperBounds.getComp( n );
//If the object is accelerating in this component direction, then we have a quadratic equation:
//1/2 * t^2 + v * t + p - x = lowerBound +- r
if ( a != 0 ) {
lowerT = Physics.leastPositiveQuadraticSolution(0.5 * a, v, -lowerBound - r + p);
upperT = Physics.leastPositiveQuadraticSolution(0.5 * a, v, -upperBound + r + p);
}
//Otherwise, if the object has no velocity, then it can never collide with a wall
else if ( v == 0) {
lowerT = null;
upperT = null;
}
//Finally, if v is not zero and there is no acceleration, then we have a linear equation:
// v * t + p = x +- r
else {
lowerT = ( lowerBound + r - p ) / v;
upperT = ( upperBound - r - p ) / v;
//System.out.println("lower: " + lowerT);
//System.out.println("upper: " + upperT);
}
Double t = null;
if ( ( lowerT != null) && ( upperT == null ) ) {
if ( lowerT - DOUBLE_THRESHOLD > 0 ) t = lowerT;
}
if ( ( upperT != null) && ( lowerT == null ) ) {
if( upperT - DOUBLE_THRESHOLD > 0 ) t = upperT;
}
if ( ( upperT != null ) && ( lowerT != null ) ) {
if ( (upperT - DOUBLE_THRESHOLD < 0) && (lowerT -DOUBLE_THRESHOLD> 0) ) {
t = lowerT;
}
if ( (lowerT - DOUBLE_THRESHOLD < 0) && (upperT -DOUBLE_THRESHOLD > 0) ){
t = upperT;
}
if ( ( lowerT -DOUBLE_THRESHOLD> 0 ) && ( upperT -DOUBLE_THRESHOLD> 0) ) {
if ( ( upperT < lowerT )) t = upperT;
else t = lowerT;
}
}
//System.out.println("t = " + t + " n = " + n);
if ( ( t!= null ) && ( t - DOUBLE_THRESHOLD > 0) ) collisions.add( new Collision(ball, t.doubleValue(), n) );
}
if ( collisions.size() > 0 ) {
Collision result = collisions.get(0);
for ( Collision c: collisions ) {
if ( ( c.deltaT < result.deltaT ) && ( c.deltaT > 0 ) ) result = c;
}
return result;
}
else return null;
}
/**
*
* @param b1 The first ball
* @param b2 The second ball
* @return Returns a new Collision object containing the next collision between the balls. Returns null if no such collision exists
*/
public static Collision checkCollision( Ball b1, Ball b2 ) {
//Difference in position vectors
DoubleVector deltaP = b1.pos.subtract( b2.pos );
//Difference in velocity vectors
DoubleVector deltaV = b1.vel.subtract( b2.vel );
//The difference in acceleration vectors is always zero, because the acceleration field is linear and static. See detailed explanation above.
//Dot product of deltaV with itself (its magnitude squared)
double vDotv = DoubleVector.dotProduct(deltaV, deltaV);
//deltaV dot deltaP
double vDotp = DoubleVector.dotProduct(deltaV, deltaP);
//Dot product of deltaP with itself (its magnitude squared)
double pDotp = DoubleVector.dotProduct(deltaP, deltaP);
//Sum of the radii of the actors. The actors will be at exactly this distance when a collision occurs.
double radiusSum = b1.radius + b2.radius;
Double t = Physics.leastPositiveQuadraticSolution( vDotv, 2 * vDotp, pDotp - radiusSum * radiusSum);
if ( t != null ) return new Collision(b1, b2, t.doubleValue() );
else return null;
}
/**
*
* @param a
* @param b
* @param c
* @return Returns the smallest positive solution to a quadratic in the form a*x^2 + b*x + c = 0. Returns null if there is no positive solution
*/
public static Double leastPositiveQuadraticSolution (double a, double b, double c ) {
//The discriminant b^2 - 4ac
double discriminant = b * b - 4 * a * c;
//If the discriminant is negative, then there are no real solutions to the equation, return null
if ( !(discriminant > 0) ) return null;
//Otherwise, there are two solutions, by the quadratic formula:
double x1 = (-b + Math.sqrt(discriminant) ) / ( 2 * a);
double x2 = (-b - Math.sqrt(discriminant) ) / ( 2 * a);
if( x1 - Physics.DOUBLE_THRESHOLD > 0 && x2 - Physics.DOUBLE_THRESHOLD > 0 ) {
if ( x1 < x2 ) return x1;
else return x2;
}
if ( x1 > 0 && x2 - Physics.DOUBLE_THRESHOLD < 0 ) {
if ( x1 - Physics.DOUBLE_THRESHOLD > 0 ) return x1;
else return null;
}
if ( x2 > 0 && x1 - Physics.DOUBLE_THRESHOLD < 0 ) {
if ( x2 - Physics.DOUBLE_THRESHOLD > 0 ) return x2;
else return null;
}
return null;
}
}