src/PhysicsObject.hh
changeset 428 712b943195a6
parent 427 01e77fe8c040
--- a/src/PhysicsObject.hh	Sat Jan 24 00:47:54 2009 +0200
+++ b/src/PhysicsObject.hh	Sat Jan 24 01:19:38 2009 +0200
@@ -30,33 +30,41 @@
  * PhysicsObject class. Represents an object in the physics simulation.
  */
 class PhysicsObject {
-public:
+protected:
     /** Reference to PhysicsWorld. */
     PhysicsWorld &world;
 
-protected:
-    /** Object position. */
+private:
+    /** Position */
     Vector position;
 
-    /** Object position on previous physics tick. */
+    /** Position at previous tick */
     Vector previousPosition;
 
-    /** Object velocity */
+    /** Velocity */
     Vector velocity;
 
-    /** Object mass. */
-    float mass;
+    /** Mass */
+    float mass; 
+ 
+    /** Object's shape, polygon vertices */
+    std::vector<Vector> shape;
 
+    /** Force queue, emptied on every physics tick */
+    std::queue<std::pair<Force, TimeMS> > forceq;
+
+
+protected:
     /** Tells if the object is "on the ground" */
     bool inAir;
 
-    /** Object elasticity. */
+    /** Object elasticity */
     float collision_elasticity;
 
-    /** Aim angle in radians. */
+    /** Aim angle in radians */
     float aim; 
 
-    /** Player facing. */
+    /** Player facing */
     FacingDirection facing; 
 
     /** Specifies if the object should be simulated */
@@ -111,7 +119,9 @@
      *
      * @param facingRight True if player is facing right.
      */
-    void setFacing (FacingDirection facing);
+    void setFacing (FacingDirection facing) {
+        this->facing = facing;
+    }
 
     /**
      * Makes the player jump in the air.
@@ -152,13 +162,6 @@
     void disable (void);
 
 private:
-    /** Objects shape. Edgepoints of the polygon */
-    std::vector<Vector> shape;
-
-    /** Object force queue. The queue is emptied on every physics
-        tick.*/
-    std::queue<std::pair<Force, TimeMS> > forceq;
-
     /**
      * Handle player movement and apply forces.
      *
@@ -184,7 +187,7 @@
      * @param d Previous derivative
      * @return Derivative
      */
-    Derivative evaluate (Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick);
+    Derivative evaluate (Force force, TimeMS dt, const Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick);
 
     /**
      * Return object acceleration with given force.
@@ -221,7 +224,9 @@
      *
      * @return Object type
      */
-    ObjectType getType (void) const;
+    ObjectType getType (void) const {
+        return type;
+    }
 
     /**
      * Checks if it is possible for the object to be in the given
@@ -236,14 +241,18 @@
      *
      * @return position vector
      */
-    Vector getPosition (void) const;
+    Vector getPosition (void) const {
+        return position;
+    }
 
     /**
      * Get previous object position.
      *
      * @return position vector
      */
-    Vector getPreviousPosition (void) const;
+    Vector getPreviousPosition (void) const {
+        return previousPosition;
+    }
 
     /**
      * Get current object screen coordinates
@@ -257,36 +266,27 @@
      *
      * @return Velocity vector
      */
-    Vector getVelocity (void) const;
-
-
-    /**
-     * Return object shape.
-     *
-     * @return Polygon points
-     */
-    const std::vector<Vector>& getShape (void) const;
-
-    /**
-     * Set object shape.
-     *
-     * @param shape Vector containing polygon points
-     */
-    void setShape(std::vector<Vector> shape);
+    Vector getVelocity (void) const {
+        return velocity;
+    }
 
     /**
      * Return object facing.
      *
      * @return Object facing (true if facing right)
      */
-    FacingDirection getFacing (void) const;
+    FacingDirection getFacing (void) const {
+        return facing;
+    }
 
     /**
      * Return object aim angle.
      *
      * @return Object aim angle
      */
-    float getAim (void) const;
+    float getAim (void) const {
+        return aim;
+    }
 
     /**
      * Get object direction.
@@ -305,7 +305,9 @@
      *
      * @return Is the object alive?
      */
-    bool isAlive (void);
+    bool isAlive (void) const {
+        return alive;
+    }
     
     /**
      * Tells the state of the object.
@@ -319,12 +321,24 @@
      *
      * @param pivot Pivot object
      */
-    void setPivot (PhysicsObject *pivot);
+    void setPivot (PhysicsObject *pivot) {
+        this->pivot = pivot;
+    }
 
     /**
      * Return the pivot object pointer.
      */
-    PhysicsObject *getPivot (void);
+    PhysicsObject *getPivot (void) {
+        return pivot;
+    }
+
+    /**
+     * Compute the force that this object (as a pivot) exerts on the given object
+     *
+     * @param bob Othe object
+     * @return Force
+     */
+    virtual Vector getPivotForce (void);
 
     /**
      * Checks if object collides with other objects
@@ -335,14 +349,6 @@
     bool collides (const PhysicsObject &obj);
 
     /**
-     * Compute the force that this object (as a pivot) exerts on the given object
-     *
-     * @param bob Othe object
-     * @return Force
-     */
-    virtual Vector getPivotForce (void);
-
-    /**
      * Update object in physics simulation.
      *
      * @param tick_length Length of the physics tick
@@ -351,6 +357,17 @@
 
 protected:
     /**
+     * Set object shape.
+     *
+     * XXX: constructor
+     *
+     * @param shape Vector containing polygon points
+     */
+    void setShape(std::vector<Vector> shape) {
+        this->shape = shape;
+    }
+
+    /**
      * Update object position, also updating our previous position
      *
      * @param pos new position
@@ -362,13 +379,35 @@
      *
      * @param velocity new velocity
      */
-    void setVelocity (Vector velocity);
+    void setVelocity (Vector velocity) {
+        this->velocity = velocity;
+    }
+
+    /**
+     * Reset state and disable, ready to be resume()'d again in a different place
+     */
+    void reset (void);
+
+    /**
+     * Resume after a reset() at the given position with a zero velocity
+     */
+    void resume (Vector position);
 };
 
 /** Helper struct for the integration */
 struct Derivative {
-    Vector dx; // Velocity
-    Vector dv; // Acceleration
+    /**
+     * Velocity
+     */
+    Vector dx;
+
+    /**
+     * Acceleration
+     */ 
+    Vector dv; 
+    
+    Derivative () : dx(), dv() { }
+    Derivative (Vector dx, Vector dv) : dx(dx), dv(dv) { }
 };
 
 /**