World: Use an integrator to integrate the bodies. Extracted the integration code...
[scd.git] / src / net / habraun / sd / EulerIntegrator.scala
blob53dab518041701a53e1acf6b56ed1ce79a27ed78
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._
27 class EulerIntegrator extends Integrator {
29 def apply(t: Double, body: Body) = {
30 var velocity = body.velocity
32 // Apply forces.
33 velocity += body.appliedForce / body.mass * t
34 body.resetForce
36 // Apply impulses.
37 velocity += body.appliedImpulse / body.mass
38 body.resetImpulse
40 // Solve movement constraints.
41 val constrainedXVelocity = if (body.xMovementAllowed) velocity.x else 0.0
42 val constrainedYVelocity = if (body.yMovementAllowed) velocity.y else 0.0
43 val constrainedVelocity = Vec2D(constrainedXVelocity, constrainedYVelocity)
45 // Set new velocity.
46 body.velocity = constrainedVelocity
48 // Set new position.
49 body.position = body.position + (body.velocity * t)
51 body