Physics: Implemented SimpleNarrowPhase, a narrow phase that supports discrete collisi...
[kong.git] / test / net / habraun / kong / physics / SimpleNarrowPhaseTest.scala
blob18254c333b8193601622522b0b236d49a3931c58
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(b1, b2, Vec2D(1, 0), Vec2D(-1, 0), Vec2D(0, 0))
81 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(b1, b2))
86 @Test
87 def inspectCircleAndNoShapeExpectNoCollision {
88 val narrowPhase = new SimpleNarrowPhase
90 val b1 = new Body
91 b1.position = Vec2D(0, 0)
92 b1.shape = Circle(1)
93 val b2 = new Body
94 b2.position = Vec2D(0, 0.5)
95 b2.shape = NoShape
97 assertEquals(None, narrowPhase.inspectCollision(b1, b2))
102 @Test
103 def inspectCircleAndLineSegmentExpectNoCollision {
104 val narrowPhase = new SimpleNarrowPhase
106 val b1 = new Body
107 b1.position = Vec2D(0, 0)
108 b1.shape = Circle(1)
109 val b2 = new Body
110 b2.position = Vec2D(0, 2)
111 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(1, 0))
113 assertEquals(None, narrowPhase.inspectCollision(b1, b2))
118 @Test
119 def inspectCircleAndLineSegmentExpectCollision {
120 val narrowPhase = new SimpleNarrowPhase
122 val b1 = new Body
123 b1.position = Vec2D(0, 0)
124 b1.shape = Circle(1)
125 val b2 = new Body
126 b2.position = Vec2D(0, 0.5)
127 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(1, 0))
129 val expectedCollision = Collision(b1, b2, Vec2D(0, 1), Vec2D(0, -1), Vec2D(0, 0))
131 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(b1, b2))
136 @Test
137 def inspectLineSegmentAndCirlceExpectCollision {
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, 0.5)
145 b2.shape = LineSegment(Vec2D(-1, 0), Vec2D(1, 0))
147 val expectedCollision = Collision(b2, b1, Vec2D(0, -1), Vec2D(0, 1), Vec2D(0, 0))
149 assertEquals(Some(expectedCollision), narrowPhase.inspectCollision(b2, b1))
154 @Test
155 def inspectTwoLineSegmentsExpectNoCollisionEvenIfTheyCollide {
156 val narrowPhase = new SimpleNarrowPhase
158 val b1 = new Body
159 b1.position = Vec2D(0, 1)
160 b1.shape = LineSegment(Vec2D(0, 0), Vec2D(2, -2))
161 val b2 = new Body
162 b2.position = Vec2D(0, -1)
163 b2.shape = LineSegment(Vec2D(0, 0), Vec2D(2, 2))
165 assertEquals(None, narrowPhase.inspectCollision(b1, b2))