src/proto2/Physics.hh
changeset 135 d5624d698a78
parent 134 d45109cf5e9d
child 136 3a15a5937f7a
--- a/src/proto2/Physics.hh	Sat Nov 29 17:37:51 2008 +0000
+++ b/src/proto2/Physics.hh	Sat Nov 29 17:59:49 2008 +0000
@@ -1,180 +1,220 @@
 #ifndef PHYSICS_HH
 #define PHYSICS_HH
 
-#include "Vector.hh"
-
 #include <vector>
 #include <queue>
 #include <ClanLib/core.h>
 
-typedef uint16_t TimeMS;
+#include "Vector.hh"
 
-const TimeMS PHYSICS_TICK_MS = 10;
+// Forward declares
+class PhysicsWorld;
+class PhysicsObject;
+class PlayerObject;
+class ProjectileObject;
+class Shape;
+struct Derivative;
 
+// Type definitions
+typedef uint16_t TimeMS;
+typedef Vector Force;
+
+// TODO: Random definitions. Should these be somewhere else?
 enum TerrainType {EMPTY, DIRT, ROCK};
 
-const Vector DIRECTIONS[] = { Vector(0,-1), Vector(1,-1), Vector(1,0), Vector(1,1),
-							Vector(0,1), Vector(-1,1), Vector(-1,0), Vector(-1,-1) };
+// This probably should come from somewhere else
+const TimeMS PHYSICS_TICK_MS = 10;
 
-// forward-declare
-class PhysicsObject;
-typedef Vector Force;
-struct Derivative;
+// Yeah?!?!?! Atleast this could be documented. Contains vectors
+// presenting all the 8 directions in a square grid?
+const Vector DIRECTIONS[] = { Vector(0,-1), Vector(1,-1), Vector(1,0), 
+                              Vector(1,1), Vector(0,1), Vector(-1,1), 
+                              Vector(-1,0), Vector(-1,-1) };
 
+
+/**
+ * PhysicsWorld class. PhysicsWorld contains PhysicsObjects that are
+ * simulated in the PhysicsWorld.
+ */
 class PhysicsWorld {
     friend class PhysicsObject;
-    
+
 private:
     CL_Timer tick_timer;
+    uint32_t tick_counter;
 
-    uint32_t tick_counter;
-    
 protected:
+    //std::vector<PlayerObject*> players;
+    //std::vector<ProjectileObject*> projectiles;
     std::vector<PhysicsObject*> objects;
+
+    // Contains connections between signals and slots
+    CL_SlotContainer slots;
+
+    PhysicsWorld(Vector gravity, Vector dimensions);
+
+    // TODO: Should these be somewhere else?
+    Vector dimensions;
     Vector gravity;
-    Vector dimensions;
 
-
+    // TODO: Should this be it's own class?
     std::vector<std::vector<TerrainType> > terrain;
 
-    CL_SlotContainer slots;
-    
-    PhysicsWorld (Vector gravity, Vector dimensions);
-
-
 public:
+    // TODO: Replace addObject with these?
+    //void addPlayerObject(PlayerObject *object);
+    //void addProjectileObject(ProjectileObject *object);
     
     /**
-     * Adds objects to the physicsworld.
+     * Add object to the PhysicsWorld.
      *
-     * @param object Pointer to PhysicsObject to add.
+     * @param object Pointer to the PhysicsObject to add.
      */
-    void addObject (PhysicsObject *object);
-    
+    void addObject(PhysicsObject *object);
+
     /**
      * Advance one time step in physics simulation.
      */
-    void tick (void);
+    void tick();
+
     /**
      * Get current tick in physics simulation.
+     *
+     * @return tick Current simulation tick.
      */
-    uint32_t getTick (void);
+    uint32_t getTick();
 
-  
+    // ?!!?!?!?!?!?!
+    // TODO: If there were a terrain class, these could it's members.
     /**
      * Generate random terrain.
+     *
+     * @param seed Random generator seed.
      */
-    void generateTerrain (int seed);
-
+    void generateTerrain(int seed);
     /**
-     * Return a normal for the wall that has been hit.
+     * Remove ground from the terrain. Removes a circle.
+     *
+     * @param x Circle x-coordinate
+     * @param y Circle y-coordinate
+     * @param r Circle radius
+     */
+    void removeGround(int x, int y, float r);
+    /**
+     * Remove ground from the terrain. Removes a circle.
+     *
+     * @param pos Circle location
+     * @param r Circle radius
+     */
+    void removeGround(Vector pos, float r);
+    /**
+     * Return normal for the wall that has been hit.
      *
      * @param hitPoint The point of the wall that has been hit.
      * @param prevPoint The point from where we were coming.
      */
     Vector getNormal(Vector hitPoint, Vector prevPoint);
-
     /**
-     * Return terrain type in specific position.
+     * Return terrain type in given position.
      *
-     * @param x x-coordinate
-     * @param y y-coordinate
+     * @param x X-coordinate
+     * @param y Y-coordinate
      */
     TerrainType getType(int x, int y) const;
     /**
-     * Return terrain type in specific position.
+     * Return terrain type in given position.
      *
-     * @param pos Position vector
+     * @param pos Coordinate vector
      */
     TerrainType getType(Vector pos) const;
 
-    /**
-     * Remove ground from the terrain. Removes a circle with given
-     * radius.
-     *
-     * @param x center x coordinate
-     * @param y center y coordinate
-     * @param r circle radius
-     */
-    void removeGround(int x, int y, float r);
-    
-    /**
-     * Remove ground from the terrain. Removes a circle with given
-     * radius.
-     *
-     * @param pos circle center
-     * @param r circle radius
-     */
-    void removeGround(Vector pos, float r);
 };
 
+/**
+ * PhysicObject class. A basic PhysicsObject class.
+ */
 class PhysicsObject {
 protected:
+    // This probably shouldn't be done this way.
     PhysicsWorld &world;
-    
-    float mass;
+
     Vector position;
     Vector velocity;
-    float aim; // Aim direction (half circle)
-    bool facingRight; // Player facing
+    float mass;
     bool inAir; // Is the object "on the ground"
 
-     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
-    ~PhysicsObject() {}   
+    // Attributes for players
+    float aim; // Aim direction (half circle)
+    bool facingRight; // Player facing
+
+    PhysicsObject(PhysicsWorld &world, float mass, Vector position, 
+                  Vector velocity);
+    ~PhysicsObject() {}
+
 
     /**
-     * Adds force to the force queue. Force queue is emptied on each
-     * tick. Forces that last over one tick are also handled. This
-     * function is only used to handle in air movement.
+     * Add force to the force queue to be applied on next tick.
      *
-     * @param force Force vector.  
+     * @param force Force vector
      */
-    void applyForce (Force force);
+    void applyForce(Force force);
 
     /**
-     * Changes player aim
+     * Change player aim
      *
      * @param da Aim angle change
      */
     void changeAim(float da);
-
+   
     /**
      * Set player facing.
      *
      * @param facingRight True if player is facing right.
-     */ 
+     */
     void setFacing(bool facingRight);
 
     /**
-     * Called on network clients to sync state from server
-     */
-    void updatePhysics (Vector position, Vector velocity, bool inAir);
-
-    /**
      * Handle ground-jumping
      */
-    void jump (void);
+    void jump();
 
-    /**
+    /** 
      * Handle ground-bounce
      *
      * @param normal Normal vector relative to which to bounce
      */
-    void bounce (Vector normal);
+    void bounce(Vector normal);
+
+    /**
+     * Called on network clients to sync state from server
+     *
+     * @param position New position
+     * @param velocity New velocity
+     * @param inAir New inAir value
+     */
+    void updatePhysics(Vector position, Vector velocity, bool inAir);
 
 private:
-    /**
-     * Handle player movement and applying forces.
-     */
-    void updatePosition (void);
+    // TODO: I'd be tempted to use some already made ClanLib structure
+    // here.  
+    // Shape of the object. Edge points of the shape polygon.
+    std::vector<Vector> shape;
 
-    /*
-     * TODO: Documentation
-     */
-    bool possibleLocation (Vector loc);
+    // TODO: Should these operations be moved to PhysicsWorld?
+    // Force queue that is emptied on every tick
+    std::queue<Force> forceq;
+    // Helper variables for integration
+    Vector posAfterTick;
+    Vector velAfterTick;
 
     /**
-     * Use RK4 to integrate the effects of force over a time intervall.
+     * Handle player movement and apply forces.
+     */
+    void updatePosition();
+
+    // TODO: Should these be moved to PhysicsWorld?
+    /**
+     * Use RK4 to integrate the effects of force over a time interwall.
      *
      * @param force Force to integrate
      * @param dt Time intervall
@@ -186,7 +226,7 @@
      *
      * @param force Force
      * @param dt Time
-     * @param d Last derivative
+     * @param d Previous derivative
      */
     Derivative evaluate(Force force, TimeMS dt, Derivative &d);
 
@@ -194,81 +234,96 @@
      * Return object acceleration with given force.
      *
      * @param force Force
-     * @return acceleration
+     * @return Acceleration
      */
     Vector acceleration(const Force &force);
 
+    // TODO: If integration is moved to PhysicsWorld then this should
+    // also move there.
     /**
-     * Handle ground movement
+     * Handle ground movement.
      *
      * @param right Boolean describing the movement direction.
-     * @return new position
+     * @return New position
      */
-    Vector walk (bool right);
+    Vector walk(bool right);
 
     /*
-     * Handle collision. TODO: This is not used. It probably should be?
+     * Handle collision. TODO: This is not used. It probably should
+     * be?
      */
     virtual void onCollision() {}
 
-    // Shape of the object. The vector contains the polygon edge
-    // points relative to the object location.
-    std::vector<Vector> shape;
-    
-    // Force queue that is emptied on every tick
-    std::queue<Force> forceq;
-    // Helper variables for integration (TODO: should these be
-    // somewhere else?)
-    Vector posAfterTick;
-    Vector velAfterTick;
+    /*
+     * TODO: This probably does some kind of collision
+     * detection. Could be named/documented better.
+     */
+    bool possibleLocation(Vector loc);
 
 public:
     /**
      * Get current object position.
-
-     * @return position vector
+     *
+     * @return Position vector
      */
-    Vector getPosition (void);
+    Vector getPosition();
 
     /**
      * Return object shape.
      *
      * @return Polygon points
      */
-    std::vector<Vector>& getShape(void);
+    std::vector<Vector>& getShape();
 
     /**
      * Set object shape.
      *
-     * @param shape Vector containing polygon points
+     * @param shape Vector containing polygon poinst
      */
-    void setShape (std::vector<Vector> shape);
+    void setShape(std::vector<Vector> shape);
 
     /**
      * Return object facing.
      *
-     * @return Object facing (true if facing is right)
+     * @return Object facing (true if facing right)
      */
-    bool getFacing(void);
+    bool getFacing();
 
     /**
      * Return object aim angle.
      *
      * @return Object aim angle
      */
-    float getAim(void);
+    float getAim();
 
     /**
      * Update object in physics simulation.
      */
-    void tick (void);
+    void tick();
 };
 
+// TODO: This could probably be moved somewhere else or removed
+// completely.
 struct Derivative {
     Vector dx; // Velocity
     Vector dv; // Acceleration
 };
 
 
+// TODO: These are drafts
+/**
+ * PlayerObject class. Represents a player in the physics engine.
+ */
+//class PlayerObject : public PhysicsObject {
+
+//};
+
+/**
+ * ProjectileObject class. Represents different projectiles in the
+ * physics (i.e. not players) in the physics engine.
+ */
+//class ProjectileObject : public PhysicsObject {
+
+//};
 
 #endif