From 452c5f4e6f258ab0ff86c25c48d6a36b401e5651 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 4 Mar 2009 11:27:19 +0100 Subject: [PATCH] Physics: Added support for applying impulses to Body. --- src/net/habraun/kong/physics/Body.scala | 22 +++++++++++++ test/net/habraun/kong/physics/BodyTest.scala | 46 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/net/habraun/kong/physics/Body.scala b/src/net/habraun/kong/physics/Body.scala index 55eb3cb..1d0ee85 100644 --- a/src/net/habraun/kong/physics/Body.scala +++ b/src/net/habraun/kong/physics/Body.scala @@ -132,6 +132,28 @@ class Body { /** + * Applies an impulse to the body. + * The effect of the impulse is computed when the world simulation is stepped. + * Valling this method several times between steps will simply add the applied impulses up. When the + * simulation is stepped, the sum of all the impulses is applied. + * An impulse must never be null. + */ + + private[this] var _appliedImpulse = Vec2D(0, 0) + + def appliedImpulse = _appliedImpulse + + def applyImpulse(impulse: Vec2D) { + _appliedImpulse += impulse + } + + def resetImpulse { + _appliedImpulse = Vec2D(0, 0) + } + + + + /** * Sets the maximum velocity for this body. * After creation, a body has a maximum velocity of Double.PositiveInfinity. * The maximum velocity must never be negative. diff --git a/test/net/habraun/kong/physics/BodyTest.scala b/test/net/habraun/kong/physics/BodyTest.scala index 3079805..1232a0c 100644 --- a/test/net/habraun/kong/physics/BodyTest.scala +++ b/test/net/habraun/kong/physics/BodyTest.scala @@ -188,6 +188,52 @@ class BodyTest { + @Test + def checkInitialAppliedImpulse { + val body = new Body + assertEquals(Vec2D(0, 0), body.appliedImpulse) + } + + + + @Test + def applyImpulse { + val body = new Body + val impulse = Vec2D(10, 10) + body.applyImpulse(impulse) + assertEquals(impulse, body.appliedImpulse) + } + + + + @Test + def applyTwoImpulses { + val body = new Body + body.applyImpulse(Vec2D(2, 1)) + body.applyImpulse(Vec2D(1, 2)) + assertEquals(Vec2D(3, 3), body.appliedImpulse) + } + + + + @Test { val expected = classOf[NullPointerException] } + def applyNullImpulse { + val body = new Body + body.applyImpulse(null) + } + + + + @Test + def applyAndResetImpulse { + val body = new Body + body.applyImpulse(Vec2D(10, 10)) + body.resetImpulse + assertEquals(Vec2D(0, 0), body.appliedImpulse) + } + + + @Test { val expected = classOf[NullPointerException] } def applyNullForceExpectException { val body = new Body -- 2.11.4.GIT