From b81a947eb0d6195729031a2e692abcb68f8029c8 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 23 Mar 2009 12:44:29 +0100 Subject: [PATCH] Body: Store the previous position every time the position is set. --- src/net/habraun/sd/Body.scala | 12 ++++++++++++ test/net/habraun/sd/BodyTest.scala | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/net/habraun/sd/Body.scala b/src/net/habraun/sd/Body.scala index b217c1c..c6e79e0 100644 --- a/src/net/habraun/sd/Body.scala +++ b/src/net/habraun/sd/Body.scala @@ -45,12 +45,24 @@ class Body { def position_=(p: Vec2D) { if (p == null) throw new NullPointerException + _previousPosition = _position _position = p } /** + * The previous position of the body, before the position was set. + * This attribute is initialized with the value Vec2D(0, 0). + */ + + private[this] var _previousPosition = Vec2D(0, 0) + + def previousPosition = _previousPosition + + + + /** * The body's shape. * The shape is needed for collision detection. * Must not be null. diff --git a/test/net/habraun/sd/BodyTest.scala b/test/net/habraun/sd/BodyTest.scala index 4105e40..3d50d57 100644 --- a/test/net/habraun/sd/BodyTest.scala +++ b/test/net/habraun/sd/BodyTest.scala @@ -57,6 +57,28 @@ class BodyTest { @Test + def setPositionCheckPreviousPosition { + val previousPosition = Vec2D(5, 0) + + val body = new Body + body.position = previousPosition + body.position = Vec2D(0, 5) + + assertEquals(previousPosition, body.previousPosition) + } + + + + @Test + def checkInitialPreviousPosition { + val body = new Body + + assertEquals(body.position, body.previousPosition) + } + + + + @Test def checkInitialVelocity { val body = new Body assertEquals(Vec2D(0, 0), body.velocity) -- 2.11.4.GIT