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
.sd
27 import org
.junit
.Assert
._
31 class SimpleNarrowPhaseTest
{
33 var narrowPhase
: NarrowPhase
= null
39 narrowPhase
= new SimpleNarrowPhase
45 def verifyUsesContinuousCircleCircleTest
{
46 val simpleNarrowPhase
= new SimpleNarrowPhase
47 assertTrue(simpleNarrowPhase
.testCircleCircle
.isInstanceOf
[ContinuousCircleCircleTest
])
53 def verifyUsesContinuousCircleLineSegmentTest
{
54 val simpleNarrowPhase
= new SimpleNarrowPhase
55 assertTrue(simpleNarrowPhase
.testCircleLineSegment
.isInstanceOf
[ContinuousCircleLineSegmentTest
])
61 def inspectTwoNoShapesExpectNoCollision
{
67 assertEquals(None
, narrowPhase
.inspectCollision(0.0, b1
, b2
))
73 def inspectCircleAndNoShapeExpectNoCollision
{
75 b1
.position
= Vec2D(0, 0)
78 b2
.position
= Vec2D(0, 0.5)
81 assertEquals(None
, narrowPhase
.inspectCollision(0.0, b1
, b2
))
87 def inspectTwoCirclesVerifyParametersArePassed
{
88 val test
= new CircleCircleTest
{
95 def apply (_c1
: Circle
, _c2
: Circle
, _p1
: Vec2D
, _p2
: Vec2D
, _v1
: Vec2D
, _v2
: Vec2D
) = {
96 c1
= _c1
; c2
= _c2
; p1
= _p1
; p2
= _p2
; v1
= _v1
; v2
= _v2
101 val simpleNarrowPhase
= new SimpleNarrowPhase
102 simpleNarrowPhase
.testCircleCircle
= test
108 b1
.position
= Vec2D(1, 1)
109 b2
.position
= Vec2D(2, 2)
110 b1
.velocity
= Vec2D(3, 3)
111 b2
.velocity
= Vec2D(4, 4)
115 simpleNarrowPhase
.inspectCollision(delta
, b1
, b2
)
117 assertEquals(b1
.shape
, test
.c1
)
118 assertEquals(b2
.shape
, test
.c2
)
119 assertEquals(b1
.position
, test
.p1
)
120 assertEquals(b2
.position
, test
.p2
)
121 assertEquals(b1
.velocity
* delta
, test
.v1
)
122 assertEquals(b2
.velocity
* delta
, test
.v2
)
128 def inspectTwoCirclesExpectCollisionIsCorrect
{
129 val simpleNarrowPhase
= new SimpleNarrowPhase
130 simpleNarrowPhase
.testCircleCircle
= new CircleCircleTest
{
131 def apply(c1
: Circle
, c2
: Circle
, p1
: Vec2D
, p2
: Vec2D
, v1
: Vec2D
, v2
: Vec2D
) = {
132 Some(TestResult(0.5, Vec2D(5, 5), Vec2D(1, 0)))
141 val collision
= simpleNarrowPhase
.inspectCollision(2.0, b1
, b2
)
143 val expected
= Collision(0.5, Contact(b1
, b2
, Vec2D(1, 0), Vec2D(-1, 0), Vec2D(5, 5)))
145 assertEquals(Some(expected
), collision
)
151 def inspectTwoCirclesExpectNone
{
152 val simpleNarrowPhase
= new SimpleNarrowPhase
153 simpleNarrowPhase
.testCircleCircle
= new CircleCircleTest
{
154 def apply(c1
: Circle
, c2
: Circle
, p1
: Vec2D
, p2
: Vec2D
, v1
: Vec2D
, v2
: Vec2D
) = {
164 val collision
= simpleNarrowPhase
.inspectCollision(2.0, b1
, b2
)
166 assertEquals(None
, collision
)
172 def inspectCircleLineSegmentVerifyParametersArePassed
{
173 val test
= new CircleLineSegmentTest
{
175 var ls
: LineSegment
= null
177 var pls
: Vec2D
= null
179 var vls
: Vec2D
= null
180 def apply (_c
: Circle
, _ls
: LineSegment
, _pc
: Vec2D
, _pls
: Vec2D
, _vc
: Vec2D
, _vls
: Vec2D
) = {
181 c
= _c
; ls
= _ls
; pc
= _pc
; pls
= _pls
; vc
= _vc
; vls
= _vls
186 val simpleNarrowPhase
= new SimpleNarrowPhase
187 simpleNarrowPhase
.testCircleLineSegment
= test
192 b2
.shape
= LineSegment(Vec2D(-1, -1), Vec2D(-2, -2))
193 b1
.position
= Vec2D(1, 1)
194 b2
.position
= Vec2D(2, 2)
195 b1
.velocity
= Vec2D(3, 3)
196 b2
.velocity
= Vec2D(4, 4)
200 simpleNarrowPhase
.inspectCollision(delta
, b1
, b2
)
202 assertEquals(b1
.shape
, test
.c
)
203 assertEquals(b2
.shape
, test
.ls
)
204 assertEquals(b1
.position
, test
.pc
)
205 assertEquals(b2
.position
, test
.pls
)
206 assertEquals(b1
.velocity
* delta
, test
.vc
)
207 assertEquals(b2
.velocity
* delta
, test
.vls
)
213 def inspectLineSegmentCircleVerifyParametersArePassed
{
214 val test
= new CircleLineSegmentTest
{
216 var ls
: LineSegment
= null
218 var pls
: Vec2D
= null
220 var vls
: Vec2D
= null
221 def apply (_c
: Circle
, _ls
: LineSegment
, _pc
: Vec2D
, _pls
: Vec2D
, _vc
: Vec2D
, _vls
: Vec2D
) = {
222 c
= _c
; ls
= _ls
; pc
= _pc
; pls
= _pls
; vc
= _vc
; vls
= _vls
227 val simpleNarrowPhase
= new SimpleNarrowPhase
228 simpleNarrowPhase
.testCircleLineSegment
= test
233 b2
.shape
= LineSegment(Vec2D(-1, -1), Vec2D(-2, -2))
234 b1
.position
= Vec2D(1, 1)
235 b2
.position
= Vec2D(2, 2)
236 b1
.velocity
= Vec2D(3, 3)
237 b2
.velocity
= Vec2D(4, 4)
241 simpleNarrowPhase
.inspectCollision(delta
, b2
, b1
)
243 assertEquals(b1
.shape
, test
.c
)
244 assertEquals(b2
.shape
, test
.ls
)
245 assertEquals(b1
.position
, test
.pc
)
246 assertEquals(b2
.position
, test
.pls
)
247 assertEquals(b1
.velocity
* delta
, test
.vc
)
248 assertEquals(b2
.velocity
* delta
, test
.vls
)
254 def inspectCircleLineSegmentExpectCollisionIsCorrect
{
255 val simpleNarrowPhase
= new SimpleNarrowPhase
256 simpleNarrowPhase
.testCircleLineSegment
= new CircleLineSegmentTest
{
257 def apply(c
: Circle
, ls
: LineSegment
, pc
: Vec2D
, pls
: Vec2D
, vc
: Vec2D
, vls
: Vec2D
) = {
258 Some(TestResult(0.5, Vec2D(5, 5), Vec2D(1, 0)))
265 b2
.shape
= LineSegment(Vec2D(1, 1), Vec2D(2, 2))
267 val collision
= simpleNarrowPhase
.inspectCollision(2.0, b1
, b2
)
269 val expected
= Collision(0.5, Contact(b1
, b2
, Vec2D(1, 0), Vec2D(-1, 0), Vec2D(5, 5)))
271 assertEquals(Some(expected
), collision
)
277 def inspectCircleLineSegmentExpectNone
{
278 val simpleNarrowPhase
= new SimpleNarrowPhase
279 simpleNarrowPhase
.testCircleLineSegment
= new CircleLineSegmentTest
{
280 def apply(c
: Circle
, ls
: LineSegment
, pc
: Vec2D
, pls
: Vec2D
, vc
: Vec2D
, vls
: Vec2D
) = {
288 b2
.shape
= LineSegment(Vec2D(1, 1), Vec2D(2, 2))
290 val collision
= simpleNarrowPhase
.inspectCollision(2.0, b1
, b2
)
292 assertEquals(None
, collision
)
298 def inspectTwoLineSegmentsExpectNoCollisionEvenIfTheyCollide
{
300 b1
.position
= Vec2D(0, 1)
301 b1
.shape
= LineSegment(Vec2D(0, 0), Vec2D(2, -2))
303 b2
.position
= Vec2D(0, -1)
304 b2
.shape
= LineSegment(Vec2D(0, 0), Vec2D(2, 2))
306 assertEquals(None
, narrowPhase
.inspectCollision(2.0, b1
, b2
))