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
24 import org
.junit
.Assert
._
28 class SimpleNarrowPhaseTest
{
31 def verifyIsNarrowPhase
{
32 val narrowPhase
= new SimpleNarrowPhase
33 assertTrue(narrowPhase
.isInstanceOf
[NarrowPhase
])
39 def inspectTwoNoShapesExpectNoCollision
{
40 val narrowPhase
= new SimpleNarrowPhase
47 assertEquals(None
, narrowPhase
.inspectCollision(0.0, b1
, b2
))
53 def inspectTwoStationaryCirclesExpectNoCollision
{
54 val narrowPhase
= new SimpleNarrowPhase
57 b1
.position
= Vec2D(0, 0)
60 b2
.position
= Vec2D(3, 0)
63 assertEquals(None
, narrowPhase
.inspectCollision(2.0, b1
, b2
))
69 def inspectTwoStationaryCirclesExpectCollision
{
70 val narrowPhase
= new SimpleNarrowPhase
73 b1
.position
= Vec2D(0, 0)
76 b2
.position
= Vec2D(3, 0)
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
))
87 def inspectTwoCirclesOneMovingExpectCollision
{
88 val narrowPhase
= new SimpleNarrowPhase
91 b1
.position
= Vec2D(-1, 0)
92 b1
.velocity
= Vec2D(1, 0)
95 b2
.position
= Vec2D(2, 0)
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
))
106 def inspectTwoCirclesOneMovingExpectNoCollision
{
107 val narrowPhase
= new SimpleNarrowPhase
110 b1
.position
= Vec2D(-3, 0)
111 b1
.velocity
= Vec2D(1, 0)
114 b2
.position
= Vec2D(2, 0)
117 assertEquals(None
, narrowPhase
.inspectCollision(2.0, b1
, b2
))
123 def inspectTwoMovingCirclesExpectCollision
{
124 val narrowPhase
= new SimpleNarrowPhase
128 b1
.position
= Vec2D(-3, 0)
129 b1
.velocity
= Vec2D(1, 0)
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
))
143 def inspectTwoMovingCirclesExpectNoCollision
{
144 val narrowPhase
= new SimpleNarrowPhase
148 b1
.position
= Vec2D(-3, 0)
149 b1
.velocity
= Vec2D(1, 0)
152 b2
.position
= Vec2D(4, 0)
153 b2
.velocity
= Vec2D(-1, 0)
155 assertEquals(None
, narrowPhase
.inspectCollision(2.0, b1
, b2
))
161 def inspectTwoMovingCirclesExpectNoCollision2
{
162 val narrowPhase
= new SimpleNarrowPhase
166 b1
.position
= Vec2D(-2, 0)
167 b1
.velocity
= Vec2D(-1, 0)
170 b2
.position
= Vec2D(2, 0)
171 b2
.velocity
= Vec2D(1, 0)
173 assertEquals(None
, narrowPhase
.inspectCollision(2.0, b1
, b2
))
179 def inspectTwoMovingCirclesWithIntersectingCoursesExpectNoCollision
{
180 val narrowPhase
= new SimpleNarrowPhase
184 b1
.position
= Vec2D(0, 0)
185 b1
.velocity
= Vec2D(0, 5)
188 b2
.position
= Vec2D(5, 0)
189 b2
.velocity
= Vec2D(-5, 0)
191 assertEquals(None
, narrowPhase
.inspectCollision(2.0, b1
, b2
))
197 def inspectCircleAndNoShapeExpectNoCollision
{
198 val narrowPhase
= new SimpleNarrowPhase
201 b1
.position
= Vec2D(0, 0)
204 b2
.position
= Vec2D(0, 0.5)
207 assertEquals(None
, narrowPhase
.inspectCollision(0.0, b1
, b2
))
213 def inspectStationaryCircleAndLineSegmentExpectNoCollision
{
214 val narrowPhase
= new SimpleNarrowPhase
217 b1
.position
= Vec2D(0, 0)
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
))
229 def inspectStationaryLineSegmentAndCircleExpectNoCollision
{
230 val narrowPhase
= new SimpleNarrowPhase
233 b1
.position
= Vec2D(0, 0)
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
))
245 def inspectStationaryCircleAndLineSegmentExpectCollision
{
246 val narrowPhase
= new SimpleNarrowPhase
249 b1
.position
= Vec2D(0, 0)
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
))
263 def inspectStationaryCircleAndLineSegmentWithCircleOnLineExpectNoCollision
{
264 val narrowPhase
= new SimpleNarrowPhase
267 b1
.position
= Vec2D(0, 0)
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
))
279 def inspectStationaryLineSegmentAndCirlceExpectCollision
{
280 val narrowPhase
= new SimpleNarrowPhase
283 b1
.position
= Vec2D(0, 0)
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
))
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
))
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
))
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
))
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
))
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),
383 assertEquals(Some(expectedCollision
), narrowPhase
.inspectCollision(2.0, circle
, segment
))
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
))
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),
420 assertEquals(Some(expectedCollision
), narrowPhase
.inspectCollision(2.0, circle
, segment
))
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
))
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),
459 assertEquals(Some(expectedCollision
), narrowPhase
.inspectCollision(2.0, circle
, segment
))
465 def inspectTwoLineSegmentsExpectNoCollisionEvenIfTheyCollide
{
466 val narrowPhase
= new SimpleNarrowPhase
469 b1
.position
= Vec2D(0, 1)
470 b1
.shape
= LineSegment(Vec2D(0, 0), Vec2D(2, -2))
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
))