src/proto2/Physics.hh
author saiam
Fri, 28 Nov 2008 15:27:51 +0000
changeset 122 16a73ebca810
parent 116 0d36aade845e
child 123 7efb63402b2b
permissions -rw-r--r--
No warnings anymore, but well have to think about that applyForce
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
     1
#ifndef PHYSICS_HH
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
     2
#define PHYSICS_HH
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
     3
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
     4
#include "Vector.hh"
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
     5
72
04428c5e548c Enum fix
ekku
parents: 71
diff changeset
     6
#include <vector>
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
     7
#include <queue>
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
     8
#include <ClanLib/core.h>
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
     9
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
    10
typedef uint16_t TimeMS;
78
bbc21da84813 This is probably better way of representing time
saiam
parents: 77
diff changeset
    11
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
    12
const TimeMS PHYSICS_TICK_MS = 10;
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
    13
72
04428c5e548c Enum fix
ekku
parents: 71
diff changeset
    14
enum TerrainType {EMPTY, DIRT, ROCK};
71
b5dbb83daa94 En takaa et k??ntyy en?? 8)
ekku
parents: 70
diff changeset
    15
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
    16
const Vector DIRECTIONS[] = { Vector(0,-1), Vector(1,-1), Vector(1,0), Vector(1,1),
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
    17
							Vector(0,1), Vector(-1,1), Vector(-1,0), Vector(-1,-1) };
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
    18
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
    19
// forward-declare
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
    20
class PhysicsObject;
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 83
diff changeset
    21
typedef Vector Force;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
    22
struct Derivative;
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
    23
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
    24
class PhysicsWorld {
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    25
    friend class PhysicsObject;
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    26
    
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    27
private:
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    28
    CL_Timer tick_timer;
105
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 98
diff changeset
    29
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 98
diff changeset
    30
    uint32_t tick_counter;
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    31
    
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    32
protected:
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    33
    std::vector<PhysicsObject*> objects;
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    34
    Vector gravity;
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 85
diff changeset
    35
    Vector dimensions;
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 85
diff changeset
    36
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 85
diff changeset
    37
72
04428c5e548c Enum fix
ekku
parents: 71
diff changeset
    38
    std::vector<std::vector<TerrainType> > terrain;
71
b5dbb83daa94 En takaa et k??ntyy en?? 8)
ekku
parents: 70
diff changeset
    39
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    40
    CL_SlotContainer slots;
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    41
    
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    42
    PhysicsWorld (Vector gravity, Vector dimensions);
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 85
diff changeset
    43
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 85
diff changeset
    44
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 85
diff changeset
    45
public:
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    46
    
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    47
    void addObject (PhysicsObject *object);
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
    48
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    49
    void tick (void);
105
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 98
diff changeset
    50
    uint32_t getTick (void);
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 98
diff changeset
    51
74
279788e460fd changed some tabs from Physics.hh
nireco
parents: 73
diff changeset
    52
    void generateTerrain (int seed);
279788e460fd changed some tabs from Physics.hh
nireco
parents: 73
diff changeset
    53
    bool collided (Vector oldPos, Vector newPos);
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 75
diff changeset
    54
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
    55
    Vector getNormal(Vector hitPoint, Vector prevPoint);
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
    56
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 108
diff changeset
    57
    TerrainType getType(int x, int y) const;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 75
diff changeset
    58
    TerrainType getType(Vector pos) const;
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 108
diff changeset
    59
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 108
diff changeset
    60
    void removeGround(int x, int y, float r);
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 108
diff changeset
    61
    void removeGround(Vector pos, float r);
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
    62
};
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
    63
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
    64
class PhysicsObject {
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    65
protected:
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    66
    PhysicsWorld &world;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
    67
    
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    68
    float mass;
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    69
    Vector position;
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    70
    Vector velocity;
74
279788e460fd changed some tabs from Physics.hh
nireco
parents: 73
diff changeset
    71
    // Whether the object (worms mainly) is in the air 
279788e460fd changed some tabs from Physics.hh
nireco
parents: 73
diff changeset
    72
    // or firmly on the ground. Affects to physics.
279788e460fd changed some tabs from Physics.hh
nireco
parents: 73
diff changeset
    73
    bool inAir;
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    74
    float aim;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    75
    uint8_t facing;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
    76
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    77
    // Shape of the object. We use a polygon with 4 edges
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    78
    // to make easy to draw with Clanlib. The coordinates
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    79
    // are relative to the center point.
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    80
    std::vector<Vector> shape;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    81
    
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
    82
    // Force queue that is emptied on every tick
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
    83
    std::queue<Force> forceq;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
    84
    Vector posAfterTick;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
    85
    Vector velAfterTick;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
    86
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    87
    /**
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    88
     * @param shape Corners of the four sided polygon.
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    89
     */
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    90
    PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
113
1b8ade19eedd PhysicsObject now has virtual destructor
nireco
parents: 112
diff changeset
    91
    ~PhysicsObject() {}   
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    92
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
    93
    /**
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    94
     * Adds force to the force queue. Force queue is emptied on each
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    95
     * tick. Forces that last over one tick are also handled. This
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    96
     * function is only used to handle in air movement.
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    97
     *
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    98
     * @param force Force vector.  
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
    99
     * @param dt The time the force is applied.
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   100
     */
122
16a73ebca810 No warnings anymore, but well have to think about that applyForce
saiam
parents: 116
diff changeset
   101
    virtual void applyForce (Force force);
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   102
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   103
    /**
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   104
     * Changes player aim
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   105
     */
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   106
    void changeAim(float da);
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   107
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   108
    /**
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   109
     * Set player facing.
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   110
     */ 
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   111
    void setFacing(bool right);
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   112
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   113
    /**
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   114
     * Called on network clients to sync state from server
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   115
     */
107
505bfa531496 send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents: 105
diff changeset
   116
    void updatePhysics (Vector position, Vector velocity, bool inAir);
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   117
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   118
    /**
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   119
     * Handle ground movement
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   120
     *
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   121
     * @return new position
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   122
     */
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   123
    Vector walk (bool right);
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   124
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   125
    /**
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   126
     * Handle ground-jumping
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   127
     */
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   128
    void jump (void);
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   129
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   130
    /**
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   131
     * Handle ground-bounce
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   132
     */
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   133
    void bounce (Vector normal);
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   134
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   135
private:
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   136
    void updatePosition (void);
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 91
diff changeset
   137
	bool possibleLocation (Vector loc);
69
309c11126949 little cleanup
saiam
parents: 66
diff changeset
   138
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   139
    /**
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   140
     * Use RK4 to integrate the effects of force over a time intervall.
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   141
     */
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 83
diff changeset
   142
    void integrate(Force force, TimeMS dt);
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 83
diff changeset
   143
    Derivative evaluate(Force force, TimeMS dt, Derivative &d);
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 83
diff changeset
   144
    Vector acceleration(const Force &force);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   145
116
0d36aade845e some stuff, don't remember what
nireco
parents: 113
diff changeset
   146
    virtual void onCollision() {}
0d36aade845e some stuff, don't remember what
nireco
parents: 113
diff changeset
   147
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   148
public:
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   149
    Vector getPosition (void);
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   150
    std::vector<Vector>& getShape(void);
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   151
    void setShape (std::vector<Vector> shape);
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   152
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   153
    bool getFacing(void);
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   154
    float getAim(void);
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   155
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   156
    void tick (void);
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   157
};
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   158
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   159
struct Derivative {
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   160
    Vector dx; // Velocity
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   161
    Vector dv; // Acceleration
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   162
};
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   163
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   164
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   165
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   166
#endif