fix ticket:1 and make PhysicsObject setPosition/setVelocity protected
authorTero Marttila <terom@fixme.fi>
Sat, 24 Jan 2009 00:47:54 +0200
changeset 427 01e77fe8c040
parent 426 510c83aab425
child 428 712b943195a6
fix ticket:1 and make PhysicsObject setPosition/setVelocity protected
src/PhysicsObject.cc
src/PhysicsObject.hh
src/Rope.cc
--- a/src/PhysicsObject.cc	Thu Jan 22 20:21:10 2009 +0200
+++ b/src/PhysicsObject.cc	Sat Jan 24 00:47:54 2009 +0200
@@ -348,12 +348,7 @@
 Vector PhysicsObject::getPreviousPosition (void) const {
     return previousPosition;
 }
-    
-void PhysicsObject::setPosition (Vector pos) {
-    this->previousPosition = this->position;
-    this->position = pos;
-}
-    
+       
 PixelCoordinate PhysicsObject::getCoordinate (void) const {
     return world.terrain.getPixelCoordinate(position);
 }
@@ -469,3 +464,12 @@
     else
         return 1;
 }
+
+void PhysicsObject::setPosition (Vector pos) {
+    this->previousPosition = this->position;
+    this->position = pos;
+}
+
+void PhysicsObject::setVelocity (Vector velocity) {
+    this->velocity = velocity;
+}
--- a/src/PhysicsObject.hh	Thu Jan 22 20:21:10 2009 +0200
+++ b/src/PhysicsObject.hh	Sat Jan 24 00:47:54 2009 +0200
@@ -37,32 +37,38 @@
 protected:
     /** Object position. */
     Vector position;
+
     /** Object position on previous physics tick. */
     Vector previousPosition;
+
     /** Object velocity */
     Vector velocity;
+
     /** Object mass. */
     float mass;
+
     /** Tells if the object is "on the ground" */
     bool inAir;
+
     /** Object elasticity. */
     float collision_elasticity;
 
-    // Attributes for players
     /** Aim angle in radians. */
     float aim; 
+
     /** Player facing. */
     FacingDirection facing; 
 
-    /** Specifies if the player is alive (or dead). */
+    /** Specifies if the object should be simulated */
     bool alive;
-    /** True if player object should be removed from the game. */
+
+    /** True if !alive, and the object can be delete'd */
     bool shouldDelete;
     
     /** Type of the object */
     ObjectType type;
 
-    /** Pivot object for this object. */
+    /** Pivot object for this object */
     PhysicsObject *pivot;
 
     /** 
@@ -78,16 +84,19 @@
      */
     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity, ObjectType type, 
             float collision_elasticity, bool enabled = true);
+    
+    /**
+     * Virtual destructor
+     */
     virtual ~PhysicsObject (void);
 
-
     /**
      * Apply a force to the object. The force is applied to the object
      * on the next physics tick.
      *
      * @param force Force vector
      */
-    void applyForce(Force force, TimeMS dt = PHYSICS_TICK_MS);
+    void applyForce (Force force, TimeMS dt = PHYSICS_TICK_MS);
 
     /**
      * Change player aim. This function takes care that aim angle
@@ -95,7 +104,7 @@
      *
      * @param da Aim angle change in radians
      */
-    void changeAim(float da);
+    void changeAim (float da);
    
     /**
      * Set player facing.
@@ -192,6 +201,10 @@
      * @return New position
      */
     void walk (TimeMS, bool right);
+
+    /*
+     * Undocumented private method
+     */
     Vector walk_one_step (float, bool);
 
 public:
@@ -221,21 +234,14 @@
     /**
      * Get current object position.
      *
-     * @return Position vector
+     * @return position vector
      */
     Vector getPosition (void) const;
 
     /**
-     * Set previous object position.
-     *
-     * @param Position vector
-     */
-    void setPosition (Vector pos);
-
-    /**
      * Get previous object position.
      *
-     * @return Position vector
+     * @return position vector
      */
     Vector getPreviousPosition (void) const;
 
@@ -253,6 +259,7 @@
      */
     Vector getVelocity (void) const;
 
+
     /**
      * Return object shape.
      *
@@ -272,14 +279,14 @@
      *
      * @return Object facing (true if facing right)
      */
-    FacingDirection getFacing() const;
+    FacingDirection getFacing (void) const;
 
     /**
      * Return object aim angle.
      *
      * @return Object aim angle
      */
-    float getAim() const;
+    float getAim (void) const;
 
     /**
      * Get object direction.
@@ -341,6 +348,21 @@
      * @param tick_length Length of the physics tick
      */
     virtual void tick (TimeMS tick_length);
+
+protected:
+    /**
+     * Update object position, also updating our previous position
+     *
+     * @param pos new position
+     */
+    void setPosition (Vector pos);
+
+    /**
+     * Update current object velocity.
+     *
+     * @param velocity new velocity
+     */
+    void setVelocity (Vector velocity);
 };
 
 /** Helper struct for the integration */
--- a/src/Rope.cc	Thu Jan 22 20:21:10 2009 +0200
+++ b/src/Rope.cc	Sat Jan 24 00:47:54 2009 +0200
@@ -23,14 +23,20 @@
 }
 
 void Rope::throwRope (void) {
+    if (state == ROPE_FIXED) {
+        // unset pivot if we re-throw rope
+        player.setPivot(NULL);
+    }
+
+    // update state
     state = ROPE_FLYING;
 
     // XXX: this should probably be more dynamic?
     length = ROPE_LENGTH;
     
     // copy position + velocity from player
-    setPosition (player.getPosition());
-    velocity = player.getVelocity() + player.getDirection() * ROPE_VELOCITY;
+    setPosition(player.getPosition());
+    setVelocity(player.getVelocity() + player.getDirection() * ROPE_VELOCITY);
     
     // we are FLYING
     inAir = true;