src/proto2/Physics.hh
changeset 83 cbba9729e92b
parent 79 295ecb26d8ff
child 85 351cb6b69c04
equal deleted inserted replaced
82:8f60abd6a083 83:cbba9729e92b
     2 #define PHYSICS_HH
     2 #define PHYSICS_HH
     3 
     3 
     4 #include "Vector.hh"
     4 #include "Vector.hh"
     5 
     5 
     6 #include <vector>
     6 #include <vector>
       
     7 #include <queue>
     7 #include <ClanLib/core.h>
     8 #include <ClanLib/core.h>
     8 
     9 
     9 typedef uint16_t TimeMS;
    10 typedef uint16_t TimeMS;
    10 
    11 
    11 const TimeMS PHYSICS_TICK_MS = 10;
    12 const TimeMS PHYSICS_TICK_MS = 10;
    12 
    13 
    13 enum TerrainType {EMPTY, DIRT, ROCK};
    14 enum TerrainType {EMPTY, DIRT, ROCK};
    14 
    15 
    15 // forward-declare
    16 // forward-declare
    16 class PhysicsObject;
    17 class PhysicsObject;
       
    18 class Force;
       
    19 struct Derivative;
    17 
    20 
    18 class PhysicsWorld {
    21 class PhysicsWorld {
    19     friend class PhysicsObject;
    22     friend class PhysicsObject;
    20     
    23     
    21 private:
    24 private:
    43 };
    46 };
    44 
    47 
    45 class PhysicsObject {
    48 class PhysicsObject {
    46 protected:
    49 protected:
    47     PhysicsWorld &world;
    50     PhysicsWorld &world;
       
    51     
    48     float mass;
    52     float mass;
    49     Vector position;
    53     Vector position;
    50     Vector velocity;
    54     Vector velocity;
    51     // Whether the object (worms mainly) is in the air 
    55     // Whether the object (worms mainly) is in the air 
    52     // or firmly on the ground. Affects to physics.
    56     // or firmly on the ground. Affects to physics.
    53     bool inAir;
    57     bool inAir;
    54     
    58 
       
    59     // Force queue that is emptied on every tick
       
    60     std::queue<Force> forceq;
       
    61     Vector posAfterTick;
       
    62     Vector velAfterTick;
       
    63 
    55     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
    64     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
    56     
    65     
    57     virtual void applyForce (Vector force, TimeMS dt);
    66     virtual void applyForce (Vector force, TimeMS dt);
    58     void updatePhysics (Vector position, Vector velocity);
    67     void updatePhysics (Vector position, Vector velocity);
    59     
    68     
    62 
    71 
    63     /**
    72     /**
    64      * Use RK4 to integrate the effects of force over a time intervall.
    73      * Use RK4 to integrate the effects of force over a time intervall.
    65      */
    74      */
    66     void integrate(Vector force, TimeMS dt);
    75     void integrate(Vector force, TimeMS dt);
    67     
    76     Derivative evaluate(Vector force, TimeMS dt, Derivative &d);
       
    77     Vector acceleration(const Vector &force);
       
    78 
    68 public:
    79 public:
    69     Vector getPosition (void);
    80     Vector getPosition (void);
    70     
    81     
    71     void tick (void);
    82     void tick (void);
    72 };
    83 };
    73 
    84 
       
    85 class Force {
       
    86 public:
       
    87     Vector force;
       
    88     TimeMS dt;
       
    89     
       
    90     Force() {}
       
    91     Force(Vector force, TimeMS dt) : force(force), dt(dt) {}
       
    92 };
       
    93 
       
    94 struct Derivative {
       
    95     Vector dx; // Velocity
       
    96     Vector dv; // Acceleration
       
    97 };
       
    98 
       
    99 
       
   100 
    74 #endif
   101 #endif