World: Use an integrator to integrate the bodies. Extracted the integration code...
[scd.git] / test / net / habraun / sd / EulerIntegratorTest.scala
blob93592513f3c8285664f57fcdd58ee1a81d8bb58a
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 math._
25 import org.junit._
26 import org.junit.Assert._
30 class EulerIntegratorTest {
32 val t = 2.0
34 var integrate: Integrator = null
35 var body: Body = null
39 @Before
40 def setup {
41 integrate = new EulerIntegrator
42 body = new Body
47 @Test
48 def integrateCheckPosition {
49 body.velocity = Vec2D(1, 0)
50 body = integrate(t, body)
51 assertEquals(Vec2D(2, 0), body.position)
56 @Test
57 def applyForceIntegrateCheckVelocity {
58 body.mass = 5
59 body.applyForce(Vec2D(5, 0))
60 body = integrate(t, body)
61 assertEquals(Vec2D(2, 0), body.velocity)
66 @Test
67 def applyForceIntegrateTwiceCheckVelocity {
68 body.mass = 5
69 body.applyForce(Vec2D(5, 0))
70 body = integrate(t, body)
71 body = integrate(t, body)
72 assertEquals(Vec2D(2, 0), body.velocity)
77 @Test
78 def applyForceIntegrateCheckPosition {
79 body.mass = 5
80 body.applyForce(Vec2D(5, 0))
81 body = integrate(t, body)
82 assertEquals(Vec2D(4, 0), body.position)
87 @Test
88 def applyImpulseIntegrateCheckVelocity {
89 body.mass = 5
90 body.velocity = Vec2D(3, 0)
91 body.applyImpulse(Vec2D(5, 0))
92 body = integrate(t, body)
93 assertEquals(Vec2D(4, 0), body.velocity)
98 @Test
99 def applyImpulseIntegrateCheckVelocity2 {
100 body.mass = 5
101 body.velocity = Vec2D(3, 0)
102 body.applyImpulse(Vec2D(5, 0))
103 body = integrate(5.0, body)
104 assertEquals(Vec2D(4, 0), body.velocity)
109 @Test
110 def applyImpulseIntegrateCheckImpulse {
111 body.applyImpulse(Vec2D(10, 10))
112 body = integrate(t, body)
113 assertEquals(Vec2D(0, 0), body.appliedImpulse)
118 @Test
119 def applyImpulseToStaticBodyIntegrateCheckVelocity {
120 body.mass = Double.PositiveInfinity
121 body.applyImpulse(Vec2D(2, 0))
122 body = integrate(t, body)
123 assertEquals(Vec2D(0, 0), body.velocity)
128 @Test
129 def disallowXMovementIntegrateCheckPosition {
130 body.allowXMovement(false)
131 body.applyForce(Vec2D(1, 1))
132 body = integrate(t, body)
133 assertEquals(Vec2D(0, 4), body.position)
138 @Test
139 def disallowYMovementIntegrateCheckPosition {
140 body.allowYMovement(false)
141 body.applyForce(Vec2D(1, 1))
142 body = integrate(t, body)
143 assertEquals(Vec2D(4, 0), body.position)