src/proto2/Physics.hh
branchno-netsession
changeset 41 ca80cd67785d
parent 35 e21cfda0edde
equal deleted inserted replaced
40:4b2867fb5c12 41:ca80cd67785d
     1 #ifndef PHYSICS_HH
     1 #ifndef PHYSICS_HH
     2 #define PHYSICS_HH
     2 #define PHYSICS_HH
     3 
     3 
     4 #include "Vector.hh"
     4 #include "Vector.hh"
     5 
     5 
       
     6 #include <vector>
       
     7 #include <queue>
     6 #include <ClanLib/core.h>
     8 #include <ClanLib/core.h>
     7 
     9 
     8 const uint16_t PHYSICS_TICK_MS = 10;
    10 typedef uint16_t TimeMS;
       
    11 
       
    12 const TimeMS PHYSICS_TICK_MS = 10;
       
    13 
       
    14 enum TerrainType {EMPTY, DIRT, ROCK};
     9 
    15 
    10 // forward-declare
    16 // forward-declare
    11 class PhysicsObject;
    17 class PhysicsObject;
       
    18 typedef Vector Force;
       
    19 struct Derivative;
    12 
    20 
    13 class PhysicsWorld {
    21 class PhysicsWorld {
    14     friend class PhysicsObject;
    22     friend class PhysicsObject;
    15             
    23     
    16     private:
    24 private:
    17         CL_Timer tick_timer;
    25     CL_Timer tick_timer;
       
    26     
       
    27 protected:
       
    28     std::vector<PhysicsObject*> objects;
       
    29     Vector gravity;
       
    30     Vector dimensions;
    18 
    31 
    19     protected:
       
    20         std::vector<PhysicsObject*> objects;
       
    21         Vector gravity;
       
    22         Vector dimensions;
       
    23 
    32 
    24         CL_SlotContainer slots;
    33     std::vector<std::vector<TerrainType> > terrain;
    25         
       
    26         PhysicsWorld (Vector gravity, Vector dimensions);
       
    27 
    34 
    28     public:
    35     CL_SlotContainer slots;
    29         void addObject (PhysicsObject *object);
    36     
       
    37     PhysicsWorld (Vector gravity, Vector dimensions);
    30 
    38 
    31         void tick (void);
    39 
       
    40 public:
       
    41     
       
    42     void addObject (PhysicsObject *object);
       
    43 
       
    44     void tick (void);
       
    45     void generateTerrain (int seed);
       
    46     bool collided (Vector oldPos, Vector newPos);
       
    47 
       
    48     TerrainType getType(Vector pos) const;
    32 };
    49 };
    33 
    50 
    34 class PhysicsObject {
    51 class PhysicsObject {
    35     protected:
    52 protected:
    36         PhysicsWorld &world;
    53     PhysicsWorld &world;
    37         float mass;
       
    38         Vector position;
       
    39         Vector velocity;
       
    40     
    54     
    41         PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
    55     float mass;
       
    56     Vector position;
       
    57     Vector velocity;
       
    58     // Whether the object (worms mainly) is in the air 
       
    59     // or firmly on the ground. Affects to physics.
       
    60     bool inAir;
    42 
    61 
    43         virtual void applyForce (Vector force, uint16_t dt);
    62 	// Shape of the object.
    44         void updatePhysics (Vector position, Vector velocity);
    63 	// //TODO
       
    64 	std::vector<Vector> edges;
       
    65 
       
    66     // Force queue that is emptied on every tick
       
    67     std::queue<Force> forceq;
       
    68     Vector posAfterTick;
       
    69     Vector velAfterTick;
       
    70 
       
    71     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
    45     
    72     
    46     private:
    73     virtual void applyForce (Force force, TimeMS dt);
    47         void updatePosition (void);
    74     void updatePhysics (Vector position, Vector velocity);
       
    75     
       
    76 private:
       
    77     void updatePosition (void);
    48 
    78 
    49     public:
    79     /**
    50         Vector getPosition (void);
    80      * Use RK4 to integrate the effects of force over a time intervall.
       
    81      */
       
    82     void integrate(Force force, TimeMS dt);
       
    83     Derivative evaluate(Force force, TimeMS dt, Derivative &d);
       
    84     Vector acceleration(const Force &force);
    51 
    85 
    52         void tick (void);
    86 public:
       
    87     Vector getPosition (void);
       
    88     
       
    89     void tick (void);
    53 };
    90 };
    54 
    91 
       
    92 struct Derivative {
       
    93     Vector dx; // Velocity
       
    94     Vector dv; // Acceleration
       
    95 };
       
    96 
       
    97 
       
    98 
    55 #endif
    99 #endif