Physics: Changed the way line segments are defined. They are now defined by the posit...
[kong.git] / test / net / habraun / kong / physics / SimpleNarrowPhaseTest.scala
blobfe195c6f451f14c9245c38cf9be4e12de6411216
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.kong.physics
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 inspectCircleAndLineSegmentExpectNoCollision {
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(0.0, b1, b2))
228 @Test
229 def inspectLineSegmentAndCircleExpectNoCollision {
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(0.0, b2, b1))
244 @Test
245 def inspectCircleAndLineSegmentExpectCollision {
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(1.0, Contact(b1, b2, Vec2D(0, 1), Vec2D(0, -1), Vec2D(0, 0)))
257 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(0.0, b1, b2))
262 @Test
263 def inspectLineSegmentAndCirlceExpectCollision {
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(0, 0.5)
271 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(2, 0))
273 val expectedCollision = Collision(1.0, Contact(b2, b1, Vec2D(0, -1), Vec2D(0, 1), Vec2D(0, 0)))
275 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(0.0, b2, b1))
280 @Test
281 def inspectTwoLineSegmentsExpectNoCollisionEvenIfTheyCollide {
282 val narrowPhase = new SimpleNarrowPhase
284 val b1 = new Body
285 b1.position = Vec2D(0, 1)
286 b1.shape = LineSegment(Vec2D(0, 0), Vec2D(2, -2))
287 val b2 = new Body
288 b2.position = Vec2D(0, -1)
289 b2.shape = LineSegment(Vec2D(0, 0), Vec2D(2, 2))
291 assertEquals(None, narrowPhase.inspectCollision(0.0, b1, b2))