src/proto2/Physics.hh
author saiam
Sat, 29 Nov 2008 20:59:50 +0000
changeset 136 3a15a5937f7a
parent 135 d5624d698a78
child 138 cc326b64ae20
permissions -rw-r--r--
Moved constants to one header Config.hh, we probably should use somekind of resourcemanager. But do we have time for that? :)
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
72
04428c5e548c Enum fix
ekku
parents: 71
diff changeset
     4
#include <vector>
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
     5
#include <queue>
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
     6
#include <ClanLib/core.h>
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
     7
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
     8
#include "Vector.hh"
136
3a15a5937f7a Moved constants to one header Config.hh, we probably should use somekind of resourcemanager. But do we have time for that? :)
saiam
parents: 135
diff changeset
     9
#include "Config.hh"
78
bbc21da84813 This is probably better way of representing time
saiam
parents: 77
diff changeset
    10
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    11
// Forward declares
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    12
class PhysicsWorld;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    13
class PhysicsObject;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    14
class PlayerObject;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    15
class ProjectileObject;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    16
class Shape;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    17
struct Derivative;
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
    18
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    19
// Type definitions
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    20
typedef uint16_t TimeMS;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    21
typedef Vector Force;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    22
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    23
// TODO: Random definitions. Should these be somewhere else?
72
04428c5e548c Enum fix
ekku
parents: 71
diff changeset
    24
enum TerrainType {EMPTY, DIRT, ROCK};
71
b5dbb83daa94 En takaa et k??ntyy en?? 8)
ekku
parents: 70
diff changeset
    25
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    26
// Yeah?!?!?! Atleast this could be documented. Contains vectors
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    27
// presenting all the 8 directions in a square grid?
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    28
const Vector DIRECTIONS[] = { Vector(0,-1), Vector(1,-1), Vector(1,0), 
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    29
                              Vector(1,1), Vector(0,1), Vector(-1,1), 
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    30
                              Vector(-1,0), Vector(-1,-1) };
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 48
diff changeset
    31
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    32
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    33
/**
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    34
 * PhysicsWorld class. PhysicsWorld contains PhysicsObjects that are
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    35
 * simulated in the PhysicsWorld.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    36
 */
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
    37
class PhysicsWorld {
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    38
    friend class PhysicsObject;
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    39
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    40
private:
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    41
    CL_Timer tick_timer;
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    42
    uint32_t tick_counter;
105
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 98
diff changeset
    43
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    44
protected:
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    45
    //std::vector<PlayerObject*> players;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    46
    //std::vector<ProjectileObject*> projectiles;
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    47
    std::vector<PhysicsObject*> objects;
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    48
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    49
    // Contains connections between signals and slots
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    50
    CL_SlotContainer slots;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    51
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    52
    PhysicsWorld(Vector gravity, Vector dimensions);
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    53
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    54
    // TODO: Should these be somewhere else?
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    55
    Vector dimensions;
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    56
    Vector gravity;
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 85
diff changeset
    57
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    58
    // TODO: Should this be it's own class?
72
04428c5e548c Enum fix
ekku
parents: 71
diff changeset
    59
    std::vector<std::vector<TerrainType> > terrain;
71
b5dbb83daa94 En takaa et k??ntyy en?? 8)
ekku
parents: 70
diff changeset
    60
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 85
diff changeset
    61
public:
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    62
    // TODO: Replace addObject with these?
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    63
    //void addPlayerObject(PlayerObject *object);
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    64
    //void addProjectileObject(ProjectileObject *object);
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
    65
    
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    66
    /**
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    67
     * Add object to the PhysicsWorld.
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    68
     *
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    69
     * @param object Pointer to the PhysicsObject to add.
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    70
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    71
    void addObject(PhysicsObject *object);
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    72
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    73
    /**
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    74
     * Advance one time step in physics simulation.
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    75
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    76
    void tick();
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    77
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    78
    /**
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    79
     * Get current tick in physics simulation.
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    80
     *
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    81
     * @return tick Current simulation tick.
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    82
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    83
    uint32_t getTick();
105
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 98
diff changeset
    84
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    85
    // ?!!?!?!?!?!?!
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    86
    // TODO: If there were a terrain class, these could it's members.
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    87
    /**
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    88
     * Generate random terrain.
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    89
     *
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    90
     * @param seed Random generator seed.
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    91
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    92
    void generateTerrain(int seed);
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    93
    /**
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    94
     * Remove ground from the terrain. Removes a circle.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    95
     *
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    96
     * @param x Circle x-coordinate
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    97
     * @param y Circle y-coordinate
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    98
     * @param r Circle radius
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
    99
     */
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   100
    void removeGround(int x, int y, float r);
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   101
    /**
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   102
     * Remove ground from the terrain. Removes a circle.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   103
     *
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   104
     * @param pos Circle location
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   105
     * @param r Circle radius
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   106
     */
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   107
    void removeGround(Vector pos, float r);
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   108
    /**
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   109
     * Return normal for the wall that has been hit.
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   110
     *
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   111
     * @param hitPoint The point of the wall that has been hit.
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   112
     * @param prevPoint The point from where we were coming.
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   113
     */
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   114
    Vector getNormal(Vector hitPoint, Vector prevPoint);
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   115
    /**
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   116
     * Return terrain type in given position.
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   117
     *
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   118
     * @param x X-coordinate
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   119
     * @param y Y-coordinate
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   120
     */
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 108
diff changeset
   121
    TerrainType getType(int x, int y) const;
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   122
    /**
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   123
     * Return terrain type in given position.
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   124
     *
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   125
     * @param pos Coordinate vector
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   126
     */
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 75
diff changeset
   127
    TerrainType getType(Vector pos) const;
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 108
diff changeset
   128
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   129
};
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   130
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   131
/**
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   132
 * PhysicObject class. A basic PhysicsObject class.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   133
 */
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   134
class PhysicsObject {
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   135
protected:
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   136
    // This probably shouldn't be done this way.
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   137
    PhysicsWorld &world;
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   138
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   139
    Vector position;
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   140
    Vector velocity;
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   141
    float mass;
128
890ac82cdcc0 Documenting more, cleaning variables. This code needs some serious
saiam
parents: 123
diff changeset
   142
    bool inAir; // Is the object "on the ground"
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   143
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   144
    // Attributes for players
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   145
    float aim; // Aim direction (half circle)
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   146
    bool facingRight; // Player facing
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   147
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   148
    PhysicsObject(PhysicsWorld &world, float mass, Vector position, 
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   149
                  Vector velocity);
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   150
    ~PhysicsObject() {}
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   151
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   152
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   153
    /**
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   154
     * Add force to the force queue to be applied on next tick.
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   155
     *
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   156
     * @param force Force vector
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   157
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   158
    void applyForce(Force force);
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   159
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   160
    /**
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   161
     * Change player aim
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   162
     *
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   163
     * @param da Aim angle change
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   164
     */
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   165
    void changeAim(float da);
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   166
   
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   167
    /**
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   168
     * Set player facing.
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   169
     *
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   170
     * @param facingRight True if player is facing right.
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   171
     */
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   172
    void setFacing(bool facingRight);
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   173
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   174
    /**
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   175
     * Handle ground-jumping
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   176
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   177
    void jump();
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   178
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   179
    /** 
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   180
     * Handle ground-bounce
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   181
     *
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   182
     * @param normal Normal vector relative to which to bounce
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 96
diff changeset
   183
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   184
    void bounce(Vector normal);
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   185
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   186
    /**
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   187
     * Called on network clients to sync state from server
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   188
     *
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   189
     * @param position New position
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   190
     * @param velocity New velocity
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   191
     * @param inAir New inAir value
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   192
     */
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   193
    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
   194
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   195
private:
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   196
    // TODO: I'd be tempted to use some already made ClanLib structure
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   197
    // here.  
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   198
    // Shape of the object. Edge points of the shape polygon.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   199
    std::vector<Vector> shape;
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   200
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   201
    // TODO: Should these operations be moved to PhysicsWorld?
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   202
    // Force queue that is emptied on every tick
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   203
    std::queue<Force> forceq;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   204
    // Helper variables for integration
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   205
    Vector posAfterTick;
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   206
    Vector velAfterTick;
69
309c11126949 little cleanup
saiam
parents: 66
diff changeset
   207
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   208
    /**
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   209
     * Handle player movement and apply forces.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   210
     */
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   211
    void updatePosition();
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   212
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   213
    // TODO: Should these be moved to PhysicsWorld?
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   214
    /**
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   215
     * Use RK4 to integrate the effects of force over a time interwall.
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   216
     *
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   217
     * @param force Force to integrate
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   218
     * @param dt Time intervall
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   219
     */
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 83
diff changeset
   220
    void integrate(Force force, TimeMS dt);
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   221
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   222
    /**
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   223
     * Evaluate the value of the derivative at given time
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   224
     *
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   225
     * @param force Force
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   226
     * @param dt Time
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   227
     * @param d Previous derivative
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   228
     */
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 83
diff changeset
   229
    Derivative evaluate(Force force, TimeMS dt, Derivative &d);
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   230
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   231
    /**
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   232
     * Return object acceleration with given force.
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   233
     *
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   234
     * @param force Force
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   235
     * @return Acceleration
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   236
     */
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 83
diff changeset
   237
    Vector acceleration(const Force &force);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   238
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   239
    // TODO: If integration is moved to PhysicsWorld then this should
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   240
    // also move there.
134
d45109cf5e9d Moved unnecessarily protected function to private
saiam
parents: 133
diff changeset
   241
    /**
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   242
     * Handle ground movement.
134
d45109cf5e9d Moved unnecessarily protected function to private
saiam
parents: 133
diff changeset
   243
     *
d45109cf5e9d Moved unnecessarily protected function to private
saiam
parents: 133
diff changeset
   244
     * @param right Boolean describing the movement direction.
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   245
     * @return New position
134
d45109cf5e9d Moved unnecessarily protected function to private
saiam
parents: 133
diff changeset
   246
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   247
    Vector walk(bool right);
134
d45109cf5e9d Moved unnecessarily protected function to private
saiam
parents: 133
diff changeset
   248
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   249
    /*
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   250
     * Handle collision. TODO: This is not used. It probably should
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   251
     * be?
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   252
     */
116
0d36aade845e some stuff, don't remember what
nireco
parents: 113
diff changeset
   253
    virtual void onCollision() {}
0d36aade845e some stuff, don't remember what
nireco
parents: 113
diff changeset
   254
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   255
    /*
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   256
     * TODO: This probably does some kind of collision
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   257
     * detection. Could be named/documented better.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   258
     */
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   259
    bool possibleLocation(Vector loc);
132
957c3c184ea0 Changed some variables that were protected to private from PhysicsObject
saiam
parents: 131
diff changeset
   260
70
a5b7499219a4 Some drafts
saiam
parents: 69
diff changeset
   261
public:
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   262
    /**
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   263
     * Get current object position.
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   264
     *
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   265
     * @return Position vector
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   266
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   267
    Vector getPosition();
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   268
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   269
    /**
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   270
     * Return object shape.
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   271
     *
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   272
     * @return Polygon points
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   273
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   274
    std::vector<Vector>& getShape();
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   275
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   276
    /**
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   277
     * Set object shape.
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   278
     *
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   279
     * @param shape Vector containing polygon poinst
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   280
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   281
    void setShape(std::vector<Vector> shape);
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   282
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   283
    /**
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   284
     * Return object facing.
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   285
     *
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   286
     * @return Object facing (true if facing right)
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   287
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   288
    bool getFacing();
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   289
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   290
    /**
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   291
     * Return object aim angle.
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   292
     *
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   293
     * @return Object aim angle
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   294
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   295
    float getAim();
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   296
129
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   297
    /**
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   298
     * Update object in physics simulation.
b1ae79a2d2f0 More documentation
saiam
parents: 128
diff changeset
   299
     */
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   300
    void tick();
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   301
};
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   302
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   303
// TODO: This could probably be moved somewhere else or removed
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   304
// completely.
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   305
struct Derivative {
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   306
    Vector dx; // Velocity
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   307
    Vector dv; // Acceleration
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   308
};
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   309
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   310
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   311
// TODO: These are drafts
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   312
/**
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   313
 * PlayerObject class. Represents a player in the physics engine.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   314
 */
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   315
//class PlayerObject : public PhysicsObject {
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   316
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   317
//};
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   318
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   319
/**
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   320
 * ProjectileObject class. Represents different projectiles in the
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   321
 * physics (i.e. not players) in the physics engine.
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   322
 */
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   323
//class ProjectileObject : public PhysicsObject {
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   324
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 134
diff changeset
   325
//};
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 79
diff changeset
   326
42
eb1a93a38cde lazy commit that breaks everything, should be a branch
terom
parents:
diff changeset
   327
#endif