Physics: Changed Collision. It now consists of a time of impact and a contact point.
[kong.git] / test / net / habraun / kong / physics / SimpleNarrowPhaseTest.scala
blobc33e2bebe2d7c6b61382b90bfd664c360430c6dc
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(b1, b2))
52 @Test
53 def inspectTwoCirclesExpectNoCollision {
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(b1, b2))
68 @Test
69 def inspectTwoCirclesExpectCollision {
70 val narrowPhase = new SimpleNarrowPhase
72 val b1 = new Body
73 b1.position = Vec2D(0, 0)
74 b1.shape = Circle(1)
75 val b2 = new Body
76 b2.position = Vec2D(0.5, 0)
77 b2.shape = Circle(1)
79 val expectedCollision = Collision(1.0, Contact(b1, b2, Vec2D(1, 0), Vec2D(-1, 0), Vec2D(0, 0)))
81 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(b1, b2))
86 @Test
87 def inspectTwoCirclesExpectCollision2 {
88 val narrowPhase = new SimpleNarrowPhase
90 val b1 = new Body
91 b1.position = Vec2D(0, 0)
92 b1.shape = Circle(2)
93 val b2 = new Body
94 b2.position = Vec2D(3, 0)
95 b2.shape = Circle(2)
97 val expectedCollision = Collision(1.0, Contact(b1, b2, Vec2D(1, 0), Vec2D(-1, 0), Vec2D(0, 0)))
99 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(b1, b2))
104 @Test
105 def inspectCircleAndNoShapeExpectNoCollision {
106 val narrowPhase = new SimpleNarrowPhase
108 val b1 = new Body
109 b1.position = Vec2D(0, 0)
110 b1.shape = Circle(1)
111 val b2 = new Body
112 b2.position = Vec2D(0, 0.5)
113 b2.shape = NoShape
115 assertEquals(None, narrowPhase.inspectCollision(b1, b2))
120 @Test
121 def inspectCircleAndLineSegmentExpectNoCollision {
122 val narrowPhase = new SimpleNarrowPhase
124 val b1 = new Body
125 b1.position = Vec2D(0, 0)
126 b1.shape = Circle(1)
127 val b2 = new Body
128 b2.position = Vec2D(0, 2)
129 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(1, 0))
131 assertEquals(None, narrowPhase.inspectCollision(b1, b2))
136 @Test
137 def inspectLineSegmentAndCircleExpectNoCollision {
138 val narrowPhase = new SimpleNarrowPhase
140 val b1 = new Body
141 b1.position = Vec2D(0, 0)
142 b1.shape = Circle(1)
143 val b2 = new Body
144 b2.position = Vec2D(0, 2)
145 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(1, 0))
147 assertEquals(None, narrowPhase.inspectCollision(b2, b1))
152 @Test
153 def inspectCircleAndLineSegmentExpectCollision {
154 val narrowPhase = new SimpleNarrowPhase
156 val b1 = new Body
157 b1.position = Vec2D(0, 0)
158 b1.shape = Circle(1)
159 val b2 = new Body
160 b2.position = Vec2D(0, 0.5)
161 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(1, 0))
163 val expectedCollision = Collision(1.0, Contact(b1, b2, Vec2D(0, 1), Vec2D(0, -1), Vec2D(0, 0)))
165 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(b1, b2))
170 @Test
171 def inspectLineSegmentAndCirlceExpectCollision {
172 val narrowPhase = new SimpleNarrowPhase
174 val b1 = new Body
175 b1.position = Vec2D(0, 0)
176 b1.shape = Circle(1)
177 val b2 = new Body
178 b2.position = Vec2D(0, 0.5)
179 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(1, 0))
181 val expectedCollision = Collision(1.0, Contact(b2, b1, Vec2D(0, -1), Vec2D(0, 1), Vec2D(0, 0)))
183 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(b2, b1))
188 @Test
189 def inspectTwoLineSegmentsExpectNoCollisionEvenIfTheyCollide {
190 val narrowPhase = new SimpleNarrowPhase
192 val b1 = new Body
193 b1.position = Vec2D(0, 1)
194 b1.shape = LineSegment(Vec2D(0, 0), Vec2D(2, -2))
195 val b2 = new Body
196 b2.position = Vec2D(0, -1)
197 b2.shape = LineSegment(Vec2D(0, 0), Vec2D(2, 2))
199 assertEquals(None, narrowPhase.inspectCollision(b1, b2))