Moved Vec2D into the new math subpackage.
[scd.git] / test / net / habraun / scd / SimpleNarrowPhaseTest.scala
blob23be8c3580ca12109bd7506c8f8fbc56574a46a4
1 /*
2 Copyright (c) 2009 Hanno Braun <hanno@habraun.net>
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
19 package net.habraun.scd
23 import org.junit._
24 import org.junit.Assert._
28 class SimpleNarrowPhaseTest {
30 @Test
31 def verifyIsNarrowPhase {
32 val narrowPhase = new SimpleNarrowPhase
33 assertTrue(narrowPhase.isInstanceOf[NarrowPhase])
38 @Test
39 def inspectTwoNoShapesExpectNoCollision {
40 val narrowPhase = new SimpleNarrowPhase
42 val b1 = new Body
43 b1.shape = NoShape
44 val b2 = new Body
45 b2.shape = NoShape
47 assertEquals(None, narrowPhase.inspectCollision(0.0, b1, b2))
52 @Test
53 def inspectTwoStationaryCirclesExpectNoCollision {
54 val narrowPhase = new SimpleNarrowPhase
56 val b1 = new Body
57 b1.position = Vec2D(0, 0)
58 b1.shape = Circle(1)
59 val b2 = new Body
60 b2.position = Vec2D(3, 0)
61 b2.shape = Circle(1)
63 assertEquals(None, narrowPhase.inspectCollision(2.0, b1, b2))
68 @Test
69 def inspectTwoStationaryCirclesExpectCollision {
70 val narrowPhase = new SimpleNarrowPhase
72 val b1 = new Body
73 b1.position = Vec2D(0, 0)
74 b1.shape = Circle(2)
75 val b2 = new Body
76 b2.position = Vec2D(3, 0)
77 b2.shape = Circle(2)
79 val expectedCollision = Collision(0.0, Contact(b1, b2, Vec2D(1, 0), Vec2D(-1, 0), Vec2D(0, 0)))
81 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(2.0, b1, b2))
86 @Test
87 def inspectTwoCirclesOneMovingExpectCollision {
88 val narrowPhase = new SimpleNarrowPhase
90 val b1 = new Body
91 b1.position = Vec2D(-1, 0)
92 b1.velocity = Vec2D(1, 0)
93 b1.shape = Circle(1)
94 val b2 = new Body
95 b2.position = Vec2D(2, 0)
96 b2.shape = Circle(1)
98 val expectedCollision = Collision(0.5, Contact(b1, b2, Vec2D(1, 0), Vec2D(-1, 0), Vec2D(1, 0)))
100 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(2.0, b1, b2))
105 @Test
106 def inspectTwoCirclesOneMovingExpectNoCollision {
107 val narrowPhase = new SimpleNarrowPhase
109 val b1 = new Body
110 b1.position = Vec2D(-3, 0)
111 b1.velocity = Vec2D(1, 0)
112 b1.shape = Circle(1)
113 val b2 = new Body
114 b2.position = Vec2D(2, 0)
115 b2.shape = Circle(1)
117 assertEquals(None, narrowPhase.inspectCollision(2.0, b1, b2))
122 @Test
123 def inspectTwoMovingCirclesExpectCollision {
124 val narrowPhase = new SimpleNarrowPhase
126 val b1 = new Body
127 b1.shape = Circle(1)
128 b1.position = Vec2D(-3, 0)
129 b1.velocity = Vec2D(1, 0)
130 val b2 = new Body
131 b2.shape = Circle(1)
132 b2.position = Vec2D(1, 0)
133 b2.velocity = Vec2D(-1, 0)
135 val expectedCollision = Collision(0.5, Contact(b1, b2, Vec2D(1, 0), Vec2D(-1, 0), Vec2D(-1, 0)))
137 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(2.0, b1, b2))
142 @Test
143 def inspectTwoMovingCirclesExpectNoCollision {
144 val narrowPhase = new SimpleNarrowPhase
146 val b1 = new Body
147 b1.shape = Circle(1)
148 b1.position = Vec2D(-3, 0)
149 b1.velocity = Vec2D(1, 0)
150 val b2 = new Body
151 b2.shape = Circle(1)
152 b2.position = Vec2D(4, 0)
153 b2.velocity = Vec2D(-1, 0)
155 assertEquals(None, narrowPhase.inspectCollision(2.0, b1, b2))
160 @Test
161 def inspectTwoMovingCirclesExpectNoCollision2 {
162 val narrowPhase = new SimpleNarrowPhase
164 val b1 = new Body
165 b1.shape = Circle(1)
166 b1.position = Vec2D(-2, 0)
167 b1.velocity = Vec2D(-1, 0)
168 val b2 = new Body
169 b2.shape = Circle(1)
170 b2.position = Vec2D(2, 0)
171 b2.velocity = Vec2D(1, 0)
173 assertEquals(None, narrowPhase.inspectCollision(2.0, b1, b2))
178 @Test
179 def inspectTwoMovingCirclesWithIntersectingCoursesExpectNoCollision {
180 val narrowPhase = new SimpleNarrowPhase
182 val b1 = new Body
183 b1.shape = Circle(1)
184 b1.position = Vec2D(0, 0)
185 b1.velocity = Vec2D(0, 5)
186 val b2 = new Body
187 b2.shape = Circle(1)
188 b2.position = Vec2D(5, 0)
189 b2.velocity = Vec2D(-5, 0)
191 assertEquals(None, narrowPhase.inspectCollision(2.0, b1, b2))
196 @Test
197 def inspectCircleAndNoShapeExpectNoCollision {
198 val narrowPhase = new SimpleNarrowPhase
200 val b1 = new Body
201 b1.position = Vec2D(0, 0)
202 b1.shape = Circle(1)
203 val b2 = new Body
204 b2.position = Vec2D(0, 0.5)
205 b2.shape = NoShape
207 assertEquals(None, narrowPhase.inspectCollision(0.0, b1, b2))
212 @Test
213 def inspectStationaryCircleAndLineSegmentExpectNoCollision {
214 val narrowPhase = new SimpleNarrowPhase
216 val b1 = new Body
217 b1.position = Vec2D(0, 0)
218 b1.shape = Circle(1)
219 val b2 = new Body
220 b2.position = Vec2D(0, 2)
221 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(2, 0))
223 assertEquals(None, narrowPhase.inspectCollision(2.0, b1, b2))
228 @Test
229 def inspectStationaryLineSegmentAndCircleExpectNoCollision {
230 val narrowPhase = new SimpleNarrowPhase
232 val b1 = new Body
233 b1.position = Vec2D(0, 0)
234 b1.shape = Circle(1)
235 val b2 = new Body
236 b2.position = Vec2D(0, 2)
237 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(2, 0))
239 assertEquals(None, narrowPhase.inspectCollision(2.0, b2, b1))
244 @Test
245 def inspectStationaryCircleAndLineSegmentExpectCollision {
246 val narrowPhase = new SimpleNarrowPhase
248 val b1 = new Body
249 b1.position = Vec2D(0, 0)
250 b1.shape = Circle(1)
251 val b2 = new Body
252 b2.position = Vec2D(0, 0.5)
253 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(2, 0))
255 val expectedCollision = Collision(0.0, Contact(b1, b2, Vec2D(0, 1), Vec2D(0, -1), Vec2D(0, 0)))
257 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(2.0, b1, b2))
262 @Test
263 def inspectStationaryCircleAndLineSegmentWithCircleOnLineExpectNoCollision {
264 val narrowPhase = new SimpleNarrowPhase
266 val b1 = new Body
267 b1.position = Vec2D(0, 0)
268 b1.shape = Circle(1)
269 val b2 = new Body
270 b2.position = Vec2D(2, 0)
271 b2.shape = LineSegment(Vec2D(0, 0), Vec2D(2, 0))
273 assertEquals(None, narrowPhase.inspectCollision(2.0, b1, b2))
278 @Test
279 def inspectStationaryLineSegmentAndCirlceExpectCollision {
280 val narrowPhase = new SimpleNarrowPhase
282 val b1 = new Body
283 b1.position = Vec2D(0, 0)
284 b1.shape = Circle(1)
285 val b2 = new Body
286 b2.position = Vec2D(0, 0.5)
287 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(2, 0))
289 val expectedCollision = Collision(0.0, Contact(b1, b2, Vec2D(0, 1), Vec2D(0, -1), Vec2D(0, 0)))
291 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(2.0, b2, b1))
296 @Test
297 def inspectMovingCircleAndStationaryLineSegmentExpectNoCollision1 {
298 // Circle moving parallel to the line segment.
299 val narrowPhase = new SimpleNarrowPhase
301 val circle = new Body
302 circle.shape = Circle(1)
303 circle.position = Vec2D(0, 0)
304 circle.velocity = Vec2D(1, 0)
305 val segment = new Body
306 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
307 segment.position = Vec2D(0, 2)
309 assertEquals(None, narrowPhase.inspectCollision(2.0, circle, segment))
314 @Test
315 def inspectMovingCircleAndStationaryLineSegmentExpectNoCollision2 {
316 // Circle moving away from the line segment.
317 val narrowPhase = new SimpleNarrowPhase
319 val circle = new Body
320 circle.shape = Circle(1)
321 circle.position = Vec2D(0, 0)
322 circle.velocity = Vec2D(0, -1)
323 val segment = new Body
324 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
325 segment.position = Vec2D(0, 2)
327 assertEquals(None, narrowPhase.inspectCollision(2.0, circle, segment))
332 @Test
333 def inspectMovingCircleAndStationaryLineSegmentExpectNoCollision3 {
334 // Circle stopping before reaching the line segment.
335 val narrowPhase = new SimpleNarrowPhase
337 val circle = new Body
338 circle.shape = Circle(1)
339 circle.position = Vec2D(0, -5)
340 circle.velocity = Vec2D(0, 1)
341 val segment = new Body
342 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
343 segment.position = Vec2D(0, 2)
345 assertEquals(None, narrowPhase.inspectCollision(2.0, circle, segment))
350 @Test
351 def inspectMovingCircleAndStationaryLineSegmentExpectNoCollision4 {
352 // Circle missing the line segment.
353 val narrowPhase = new SimpleNarrowPhase
355 val circle = new Body
356 circle.shape = Circle(1)
357 circle.position = Vec2D(-2, 0)
358 circle.velocity = Vec2D(0, 1)
359 val segment = new Body
360 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
361 segment.position = Vec2D(0, 2)
363 assertEquals(None, narrowPhase.inspectCollision(2.0, circle, segment))
368 @Test
369 def inspectMovingCircleAndStationaryLineSegmentExpectCollision {
370 val narrowPhase = new SimpleNarrowPhase
372 val circle = new Body
373 circle.shape = Circle(1)
374 circle.position = Vec2D(2, 0)
375 circle.velocity = Vec2D(0, 1)
376 val segment = new Body
377 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
378 segment.position = Vec2D(0, 2)
380 val expectedCollision = Collision(0.5, Contact(circle, segment, Vec2D(0, 1), Vec2D(0, -1),
381 Vec2D(2, 2)))
383 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(2.0, circle, segment))
388 @Test
389 def inspectStationaryCircleAndMovingLineSegmentExpectNoCollision {
390 val narrowPhase = new SimpleNarrowPhase
392 val circle = new Body
393 circle.shape = Circle(1)
394 circle.position = Vec2D(0, 0)
395 val segment = new Body
396 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
397 segment.position = Vec2D(0, 2)
398 segment.velocity = Vec2D(0, 1)
400 assertEquals(None, narrowPhase.inspectCollision(2.0, circle, segment))
405 @Test
406 def inspectStationaryCircleAndMovingLineSegmentExpectCollision {
407 val narrowPhase = new SimpleNarrowPhase
409 val circle = new Body
410 circle.shape = Circle(1)
411 circle.position = Vec2D(2, 0)
412 val segment = new Body
413 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
414 segment.position = Vec2D(0, 2)
415 segment.velocity = Vec2D(0, -1)
417 val expectedCollision = Collision(0.5, Contact(circle, segment, Vec2D(0, 1), Vec2D(0, -1),
418 Vec2D(2, 1)))
420 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(2.0, circle, segment))
425 @Test
426 def inspectMovingCircleAndLineSegmentExpectNoCollision {
427 val narrowPhase = new SimpleNarrowPhase
429 val circle = new Body
430 circle.shape = Circle(1)
431 circle.position = Vec2D(0, 0)
432 circle.velocity = Vec2D(1, 0)
433 val segment = new Body
434 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
435 segment.position = Vec2D(0, 2)
436 segment.velocity = Vec2D(-1, 0)
438 assertEquals(None, narrowPhase.inspectCollision(2.0, circle, segment))
443 @Test
444 def inspectMovingCircleAndLineSegmentExpectCollision {
445 val narrowPhase = new SimpleNarrowPhase
447 val circle = new Body
448 circle.shape = Circle(1)
449 circle.position = Vec2D(2, -1)
450 circle.velocity = Vec2D(0, 1)
451 val segment = new Body
452 segment.shape = LineSegment(Vec2D(0, 0), Vec2D(5, 0))
453 segment.position = Vec2D(0, 2)
454 segment.velocity = Vec2D(0, -1)
456 val expectedCollision = Collision(0.5, Contact(circle, segment, Vec2D(0, 1), Vec2D(0, -1),
457 Vec2D(2, 1)))
459 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(2.0, circle, segment))
464 @Test
465 def inspectTwoLineSegmentsExpectNoCollisionEvenIfTheyCollide {
466 val narrowPhase = new SimpleNarrowPhase
468 val b1 = new Body
469 b1.position = Vec2D(0, 1)
470 b1.shape = LineSegment(Vec2D(0, 0), Vec2D(2, -2))
471 val b2 = new Body
472 b2.position = Vec2D(0, -1)
473 b2.shape = LineSegment(Vec2D(0, 0), Vec2D(2, 2))
475 assertEquals(None, narrowPhase.inspectCollision(2.0, b1, b2))