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
26 import scala
.collection
.mutable
._
29 import org
.junit
.Assert
._
35 var world
: World
= null
47 def verifyBodyIterableIsAccessible
{
48 assertTrue(world
.bodies
.isInstanceOf
[Iterable
[Body
]])
54 def verifyBodyIterableCantChangeWorld
{
56 world
.bodies
.asInstanceOf
[HashSet
[Body
]].addEntry(body
)
57 assertFalse(world
.bodies
.exists(_
== body
))
63 def verifyInitialIntegrator
{
64 assertTrue(world
.integrator
.isInstanceOf
[EulerIntegrator
])
69 @Test { val expected
= classOf
[NullPointerException
] }
70 def setIntegratorNullExpectException() {
71 world
.integrator
= null
77 def verifyInitialBroadPhase
{
78 assertTrue(world
.broadPhase
.isInstanceOf
[SimpleBroadPhase
])
83 @Test { val expected
= classOf
[NullPointerException
] }
84 def setBroadPhaseNullExpectException
{
85 world
.broadPhase
= null
91 def verifyInitialNarrowPhase
{
92 assertTrue(world
.narrowPhase
.isInstanceOf
[SimpleNarrowPhase
])
97 @Test { val expected
= classOf
[NullPointerException
] }
98 def setNarrowPhaseNullExpectException
{
99 world
.narrowPhase
= null
105 def verifyInitialConstraintSolver
{
106 assertTrue(world
.constraintSolver
.isInstanceOf
[ImpulseSolver
])
111 @Test { val expected
= classOf
[NullPointerException
] }
112 def setConstraintSolverNullExpectException
{
113 world
.constraintSolver
= null
118 @Test { val expected
= classOf
[IllegalArgumentException
] }
119 def stepPassNegativeDelta
{
126 def addBodyVerifyIsIntegrated
{
127 val integrate
= new Integrator
{
128 var _t
: Double
= Double
.NaN
129 var _body
: Body
= null
130 def apply(t
: Double
, body
: Body
) = {
136 world
.integrator
= integrate
144 assertEquals(t
, integrate
._t
, 0.0)
145 assertEquals(body
, integrate
._body
)
151 def addAndRemoveBodyVerifyIsNotIntegrated
{
154 val integrate
= new Integrator
{
155 var integrated
= false
156 def apply(t
: Double
, b
: Body
) = {
157 integrated
= b
== body
161 world
.integrator
= integrate
167 assertFalse(integrate
.integrated
)
173 def addBodyVerifyItIsPassedToBroadPhase
{
174 val broadPhase
= new BroadPhase
{
175 var passedBodies
: List
[Body
] = null
176 def detectPossibleCollisions(bodies
: List
[Body
]) = { passedBodies
= bodies
; Nil
}
178 world
.broadPhase
= broadPhase
184 assertEquals(body
::Nil
, broadPhase
.passedBodies
)
190 def addBroadPhaseReturningBodyPairsVerifyTheyArePassedToNarrowPhase
{
196 world
.broadPhase
= new BroadPhase
{
197 def detectPossibleCollisions(bodies
: List
[Body
]) = (b1
, b2
)::(b3
, b4
)::Nil
200 val narrowPhase
= new NarrowPhase
{
201 var passedPairs
: List
[(Body
, Body
)] = Nil
202 def inspectCollision(b1
: Body
, b2
: Body
) = {
203 passedPairs
= passedPairs
:::List((b1
, b2
))
207 world
.narrowPhase
= narrowPhase
211 assertEquals((b1
, b2
)::(b3
, b4
)::Nil
, narrowPhase
.passedPairs
)
217 def verifyForceIsAppliedBeforeCollisionDetection
{
218 val broadPhase
= new BroadPhase
{
220 def detectPossibleCollisions(bodies
: List
[Body
]) = {
221 v
= bodies(0).velocity
225 world
.broadPhase
= broadPhase
229 body
.applyForce(Vec2D(10, 0))
234 assertEquals(Vec2D(4, 0), broadPhase
.v
)
240 def verifyImpulseIsAppliedBeforeCollisionDetection
{
241 val broadPhase
= new BroadPhase
{
243 def detectPossibleCollisions(bodies
: List
[Body
]) = {
244 v
= bodies(0).velocity
248 world
.broadPhase
= broadPhase
252 body
.applyImpulse(Vec2D(10, 0))
257 assertEquals(Vec2D(2, 0), broadPhase
.v
)
263 def verifyCollisionReturnedByNarrowPhaseIsPassedToSolver
{
269 val collision
= Collision(0.0, Contact(b1
, Vec2D(1, 1), Vec2D(1, 0), b2
),
270 Contact(b2
, Vec2D(1, 1), Vec2D(-1, 0), b1
))
272 world
.narrowPhase
= new NarrowPhase
{
273 def inspectCollision(b1
: Body
, b2
: Body
) = {
278 val solver
= new ConstraintSolver
{
279 var collision
: Collision
= null
280 def apply(t
: Double
, constraint
: Collision
) {
281 collision
= constraint
284 world
.constraintSolver
= solver
288 assertEquals(collision
, solver
.collision
)