-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collision.java
179 lines (114 loc) · 5.15 KB
/
Collision.java
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
import java.util.List;
import java.util.ArrayList;
// *****************************************************************************
// *****************************************************************************
// Class: CollisionEvent
// Description: Object class, representing a "collision event"
// between entities in a simulation
class Collision implements Comparable<Collision> {
// List of entities involved in the collision:
List<Entity> entities = new ArrayList<>();
// x & y coordinates of the collision point:
double xC, yC;
// Time until the collision occurs:
double tC;
// *************************************************************************
// Method: Collision
// Description: Default constructor for the class
// Parameters: e1, e2 - Entities that are colliding
// xC, yC - x & y coordinates of collision
// tC - Time at which collision will occur
// Returns: A new instance of the class
// Calls: Vector
// Globals: entities
// xC, yC, tC
Collision (Entity e1, Entity e2, double xC, double yC, double tC) {
entities.add(e1);
entities.add(e2);
this.xC = xC;
this.yC = yC;
// Round the collision time to the nearest ms:
this.tC = Vector.roundDouble(tC, 3);
}
// *************************************************************************
// Method: computeCollisionTime
// Description: Computes the collision time from the given
// acceleration, velocity, and distance values
// Returns the value of the smallest, positive root,
// or -1 if there is none
// Parameters: a - Acceleration
// v - Velocity
// d - Distance
// Returns: The value of the smallest, positive root,
// or -1 if there is none
// Calls: Vector
// Globals: None
static double computeCollisionTime (double a, double v, double d) {
// Round inputs to the nano scale:
a = Vector.roundDouble(a, 9);
v = Vector.roundDouble(v, 9);
// If the distance is 0:
if (d == 0) {
// Then no time is needed to travel the distance
return 0;
// If the acceleration is 0:
} else if (a == 0) {
// If the velocity is 0:
if (v == 0) {
// Then the entity is not moving
return -1;
// If the velocity is non-zero:
} else {
// Then the quadratic formula is not required:
return d/v;
}
// Otherwise, use the quadratic formula:
} else {
// Do not take the square root of a negative number:
double sqrt = v*v - 4*a*d;
// If the value under the square root is negative:
if (sqrt < 0) {
// Return failure:
return -1;
// Otherwise:
} else {
// Find both roots
double t1 = (-v + Math.sqrt(sqrt)) / (2*a);
double t2 = (-v - Math.sqrt(sqrt)) / (2*a);
// If both roots are negative:
if (t1 < 0 && t2 < 0) {
return -1;
}
// If both roots are positive, return the smallest:
if (t1 > 0 && t2 > 0) {
return Math.min(t1, t2);
}
// Otherwise, return the positive root:
return Math.max(t1, t2);
}
}
}
// *************************************************************************
// Method: compareTo
// Description: Implementation of Comparable defining natural order
// Parameters: o - Collision being compared with this one
// Returns: -1: This object should be sorted earlier than o
// +1: This object should be sorted later than o
// 0: This object is equal to o (in ordering)
// Calls: Nothing
// Globals: tC
@Override
public int compareTo(Collision o) {
/* Sort by increasing collisionTime */
if (tC < o.tC) {
return -1;
}
if (tC > o.tC) {
return +1;
}
return 0;
}
// *************************************************************************
}
// *****************************************************************************
// *****************************************************************************