Minor refactoring.
[scd.git] / test / net / habraun / sd / SimpleNarrowPhaseTest.scala
blob5dee5b84be6c97f1d1a0cfb72359f4e22db168f0
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.sd
23 import collision._
24 import math._
26 import org.junit._
27 import org.junit.Assert._
31 class SimpleNarrowPhaseTest {
33 var narrowPhase: NarrowPhase = null
37 @Before
38 def setup {
39 narrowPhase = new SimpleNarrowPhase
44 @Test
45 def verifyUsesContinuousCircleCircleTest {
46 val simpleNarrowPhase = new SimpleNarrowPhase
47 assertTrue(simpleNarrowPhase.testCircleCircle.isInstanceOf[ContinuousCircleCircleTest])
52 @Test
53 def verifyUsesContinuousCircleLineSegmentTest {
54 val simpleNarrowPhase = new SimpleNarrowPhase
55 assertTrue(simpleNarrowPhase.testCircleLineSegment.isInstanceOf[ContinuousCircleLineSegmentTest])
60 @Test
61 def inspectTwoNoShapesExpectNoCollision {
62 val b1 = new Body
63 b1.shape = NoShape
64 val b2 = new Body
65 b2.shape = NoShape
67 assertEquals(None, narrowPhase.inspectCollision(0.0, b1, b2))
72 @Test
73 def inspectCircleAndNoShapeExpectNoCollision {
74 val b1 = new Body
75 b1.position = Vec2D(0, 0)
76 b1.shape = Circle(1)
77 val b2 = new Body
78 b2.position = Vec2D(0, 0.5)
79 b2.shape = NoShape
81 assertEquals(None, narrowPhase.inspectCollision(0.0, b1, b2))
86 @Test
87 def inspectTwoCirclesVerifyParametersArePassed {
88 val test = new CircleCircleTest {
89 var c1: Circle = null
90 var c2: Circle = null
91 var p1: Vec2D = null
92 var p2: Vec2D = null
93 var v1: Vec2D = null
94 var v2: Vec2D = null
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
97 None
101 val simpleNarrowPhase = new SimpleNarrowPhase
102 simpleNarrowPhase.testCircleCircle = test
104 val b1 = new Body
105 val b2 = new Body
106 b1.shape = Circle(1)
107 b2.shape = Circle(2)
108 b1.position = Vec2D(1, 1)
109 b2.position = Vec2D(2, 2)
110 b1.velocity = Vec2D(3, 3)
111 b2.velocity = Vec2D(4, 4)
113 val delta = 2.0
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)
127 @Test
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)))
136 val b1 = new Body
137 val b2 = new Body
138 b1.shape = Circle(1)
139 b2.shape = Circle(1)
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)
150 @Test
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) = {
155 None
159 val b1 = new Body
160 val b2 = new Body
161 b1.shape = Circle(1)
162 b2.shape = Circle(1)
164 val collision = simpleNarrowPhase.inspectCollision(2.0, b1, b2)
166 assertEquals(None, collision)
171 @Test
172 def inspectCircleLineSegmentVerifyParametersArePassed {
173 val test = new CircleLineSegmentTest {
174 var c: Circle = null
175 var ls: LineSegment = null
176 var pc: Vec2D = null
177 var pls: Vec2D = null
178 var vc: 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
182 None
186 val simpleNarrowPhase = new SimpleNarrowPhase
187 simpleNarrowPhase.testCircleLineSegment = test
189 val b1 = new Body
190 val b2 = new Body
191 b1.shape = Circle(1)
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)
198 val delta = 2.0
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)
212 @Test
213 def inspectLineSegmentCircleVerifyParametersArePassed {
214 val test = new CircleLineSegmentTest {
215 var c: Circle = null
216 var ls: LineSegment = null
217 var pc: Vec2D = null
218 var pls: Vec2D = null
219 var vc: 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
223 None
227 val simpleNarrowPhase = new SimpleNarrowPhase
228 simpleNarrowPhase.testCircleLineSegment = test
230 val b1 = new Body
231 val b2 = new Body
232 b1.shape = Circle(1)
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)
239 val delta = 2.0
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)
253 @Test
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)))
262 val b1 = new Body
263 val b2 = new Body
264 b1.shape = Circle(1)
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)
276 @Test
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) = {
281 None
285 val b1 = new Body
286 val b2 = new Body
287 b1.shape = Circle(1)
288 b2.shape = LineSegment(Vec2D(1, 1), Vec2D(2, 2))
290 val collision = simpleNarrowPhase.inspectCollision(2.0, b1, b2)
292 assertEquals(None, collision)
297 @Test
298 def inspectTwoLineSegmentsExpectNoCollisionEvenIfTheyCollide {
299 val b1 = new Body
300 b1.position = Vec2D(0, 1)
301 b1.shape = LineSegment(Vec2D(0, 0), Vec2D(2, -2))
302 val b2 = new Body
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))