src/proto2/Physics.hh
branchno-netsession
changeset 41 ca80cd67785d
parent 35 e21cfda0edde
--- a/src/proto2/Physics.hh	Thu Nov 20 23:20:00 2008 +0000
+++ b/src/proto2/Physics.hh	Thu Nov 20 23:45:33 2008 +0000
@@ -3,53 +3,97 @@
 
 #include "Vector.hh"
 
+#include <vector>
+#include <queue>
 #include <ClanLib/core.h>
 
-const uint16_t PHYSICS_TICK_MS = 10;
+typedef uint16_t TimeMS;
+
+const TimeMS PHYSICS_TICK_MS = 10;
+
+enum TerrainType {EMPTY, DIRT, ROCK};
 
 // forward-declare
 class PhysicsObject;
+typedef Vector Force;
+struct Derivative;
 
 class PhysicsWorld {
     friend class PhysicsObject;
-            
-    private:
-        CL_Timer tick_timer;
+    
+private:
+    CL_Timer tick_timer;
+    
+protected:
+    std::vector<PhysicsObject*> objects;
+    Vector gravity;
+    Vector dimensions;
 
-    protected:
-        std::vector<PhysicsObject*> objects;
-        Vector gravity;
-        Vector dimensions;
 
-        CL_SlotContainer slots;
-        
-        PhysicsWorld (Vector gravity, Vector dimensions);
+    std::vector<std::vector<TerrainType> > terrain;
 
-    public:
-        void addObject (PhysicsObject *object);
+    CL_SlotContainer slots;
+    
+    PhysicsWorld (Vector gravity, Vector dimensions);
 
-        void tick (void);
+
+public:
+    
+    void addObject (PhysicsObject *object);
+
+    void tick (void);
+    void generateTerrain (int seed);
+    bool collided (Vector oldPos, Vector newPos);
+
+    TerrainType getType(Vector pos) const;
 };
 
 class PhysicsObject {
-    protected:
-        PhysicsWorld &world;
-        float mass;
-        Vector position;
-        Vector velocity;
+protected:
+    PhysicsWorld &world;
     
-        PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
+    float mass;
+    Vector position;
+    Vector velocity;
+    // Whether the object (worms mainly) is in the air 
+    // or firmly on the ground. Affects to physics.
+    bool inAir;
 
-        virtual void applyForce (Vector force, uint16_t dt);
-        void updatePhysics (Vector position, Vector velocity);
+	// Shape of the object.
+	// //TODO
+	std::vector<Vector> edges;
+
+    // Force queue that is emptied on every tick
+    std::queue<Force> forceq;
+    Vector posAfterTick;
+    Vector velAfterTick;
+
+    PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
     
-    private:
-        void updatePosition (void);
+    virtual void applyForce (Force force, TimeMS dt);
+    void updatePhysics (Vector position, Vector velocity);
+    
+private:
+    void updatePosition (void);
 
-    public:
-        Vector getPosition (void);
+    /**
+     * Use RK4 to integrate the effects of force over a time intervall.
+     */
+    void integrate(Force force, TimeMS dt);
+    Derivative evaluate(Force force, TimeMS dt, Derivative &d);
+    Vector acceleration(const Force &force);
 
-        void tick (void);
+public:
+    Vector getPosition (void);
+    
+    void tick (void);
 };
 
+struct Derivative {
+    Vector dx; // Velocity
+    Vector dv; // Acceleration
+};
+
+
+
 #endif