src/proto2/Physics.cc
author ekku
Mon, 01 Dec 2008 22:32:58 +0000
changeset 164 1d4772967cd0
parent 163 e3db8a0bb89f
child 165 26e6a87a9085
permissions -rw-r--r--
gfoo
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
     1
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
     2
#include "Physics.hh"
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
     3
#include "Engine.hh"
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
     4
#include "GameState.hh"
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 148
diff changeset
     5
#include "Terrain.hh"
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
     6
45
32c876923cac I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents: 44
diff changeset
     7
#include <algorithm>
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 49
diff changeset
     8
#include <functional>
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
     9
#include <cmath>
104
5cb116dc0759 Pomppii paremmin
ekku
parents: 103
diff changeset
    10
#include <assert.h>
45
32c876923cac I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents: 44
diff changeset
    11
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    12
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions)
161
ea2f295c279f Made PhysicsWorld to inherit Terrain instead of having it as an attribute.
saiam
parents: 160
diff changeset
    13
    : Terrain(1337), tick_timer(PHYSICS_TICK_MS), tick_counter(0), dimensions(dimensions),
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 148
diff changeset
    14
      gravity(gravity) {
54
b8b043ba0abd fix some more compiler errors...
terom
parents: 52
diff changeset
    15
    slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick);
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 49
diff changeset
    16
    tick_timer.enable();
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 49
diff changeset
    17
}
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    18
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    19
void PhysicsWorld::addObject (PhysicsObject *object) {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
    20
    objects.push_back(object);
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    21
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    22
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    23
void PhysicsWorld::tick () {
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    24
    //    Engine::log(DEBUG, "physics.apply_force") << "*tick*";
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
    25
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    26
    for (std::vector<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) {
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    27
        (*i)->tick(); 
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    28
    }
105
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 104
diff changeset
    29
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 104
diff changeset
    30
    tick_counter++;
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 104
diff changeset
    31
}
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 104
diff changeset
    32
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 104
diff changeset
    33
uint32_t PhysicsWorld::getTick (void) {
91e3f3806b31 set UDP sockets as nonblocking
terom
parents: 104
diff changeset
    34
    return tick_counter;
48
fa1da22db8a0 It looks like physics could now work, but I doubt it...
saiam
parents: 47
diff changeset
    35
}
fa1da22db8a0 It looks like physics could now work, but I doubt it...
saiam
parents: 47
diff changeset
    36
128
890ac82cdcc0 Documenting more, cleaning variables. This code needs some serious
saiam
parents: 124
diff changeset
    37
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, 
890ac82cdcc0 Documenting more, cleaning variables. This code needs some serious
saiam
parents: 124
diff changeset
    38
                              Vector position, Vector velocity)
135
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 131
diff changeset
    39
    : world(world), position(position), velocity(velocity),
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 131
diff changeset
    40
      mass(mass), inAir(true), aim(0), facingRight(true) {
d5624d698a78 Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents: 131
diff changeset
    41
    // TODO: Is thir the right way to do this?
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    42
    world.addObject(this);
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    43
}
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    44
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
    45
Vector PhysicsObject::walk (bool right) {
163
e3db8a0bb89f sheippii
ekku
parents: 161
diff changeset
    46
    Vector cursor = this->position + shape[2] + (right ? Vector(1,0) : Vector(-1,0));
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    47
    Vector reached = this->position;
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    48
    
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    49
    //for(int steps = 0; steps < 3; steps++) {
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    50
    
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    51
    // Go up but not if the wall is over two pixels
164
ekku
parents: 163
diff changeset
    52
    if(world.collides(cursor)) {
ekku
parents: 163
diff changeset
    53
   
163
e3db8a0bb89f sheippii
ekku
parents: 161
diff changeset
    54
        for(int height = 0, max = 2; height < max+42; height++) {
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    55
            
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    56
            if(height >= max)
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    57
                return reached;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    58
            
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    59
            cursor.y--;
164
ekku
parents: 163
diff changeset
    60
            if(!world.collides(cursor)) {
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    61
                // Check that the other parts of the worm don't collide with anything
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    62
                if(possibleLocation(cursor)) {
163
e3db8a0bb89f sheippii
ekku
parents: 161
diff changeset
    63
                    reached = cursor - shape[2];
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    64
                    continue;
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    65
                } else {
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    66
                    // Can't get any further
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    67
                    return reached;
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    68
                }
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    69
            }
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    70
        }
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    71
    } else {
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    72
        if(possibleLocation(cursor)) {
163
e3db8a0bb89f sheippii
ekku
parents: 161
diff changeset
    73
            reached = cursor - shape[2];
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    74
        }
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    75
        // Start checking if the lower squares are empty
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    76
        
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    77
        for(int depth = 0, max = 3; depth < max+42; depth++) {
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    78
            
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    79
            if(depth >= max) {
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    80
                // We can start a free fall now
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
    81
                //
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    82
                // TODO it should be set inAir if it falls from a cliff
116
0d36aade845e some stuff, don't remember what
nireco
parents: 115
diff changeset
    83
                this->inAir = true;
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    84
                
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    85
                // Put some speed there to make loke smoother
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    86
                //this->velocity.y = -5;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    87
                return reached;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    88
            }
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    89
            
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    90
            cursor.y++;
164
ekku
parents: 163
diff changeset
    91
            if(!world.collides(cursor)) {
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    92
                // Check that the other parts of the worm don't collide with anything
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    93
                if(possibleLocation(cursor)) {
163
e3db8a0bb89f sheippii
ekku
parents: 161
diff changeset
    94
                    reached = cursor - shape[2];
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    95
                    continue;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    96
                } else {
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
    97
                    // Can't get any further
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    98
                    return reached;
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
    99
                }
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   100
            }
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   101
        }
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   102
    }      
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   103
    
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   104
    //    cursor.x += right ? 1 : -1;
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   105
    //}
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   106
    return reached;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   107
}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   108
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   109
void PhysicsObject::jump () {
114
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   110
    // Jump only if player is "on the ground"
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   111
    if (!this->inAir) {
159
109c7612ae2d hyppy vinoon
ekku
parents: 158
diff changeset
   112
 	    velocity.y = -100;
109c7612ae2d hyppy vinoon
ekku
parents: 158
diff changeset
   113
        velocity.x += facingRight ? 20 : -20;
109c7612ae2d hyppy vinoon
ekku
parents: 158
diff changeset
   114
	    inAir = true;
114
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   115
    }
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   116
}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   117
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   118
bool PhysicsObject::possibleLocation (Vector loc) {
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   119
    for(unsigned int i = 0; i < this->shape.size(); i++) {
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   120
        if(world.getType(loc+shape[i]) != EMPTY)
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   121
            return false;
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   122
    }
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   123
    return true;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   124
}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   125
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   126
/**
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   127
 * Updates object speed and position. This function organises force
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   128
 * integration and collision detection.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   129
 */   
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   130
void PhysicsObject::updatePosition () {
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   131
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   132
    // Add gravity to the force queue
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   133
    forceq.push(world.gravity);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   134
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   135
    // Go trough every force in the queue
84
3cb862028a24 No nyt se toimii v?h?n paremmin. Koodia pit?? kyll? viel? siisti? aika huolella.
saiam
parents: 83
diff changeset
   136
    Force total;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   137
    posAfterTick = position;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   138
    velAfterTick = velocity;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   139
    while (!forceq.empty()) {
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   140
        total += forceq.front();
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   141
        forceq.pop();
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   142
    }
114
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   143
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   144
    // TODO: This is _ugly_ (but not so ugly as the last one I think)
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   145
    // hack. I think we should handle walking from the collision
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   146
    // detection code.
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   147
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   148
    if (!this->inAir) {
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   149
        if (total.x != 0)
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   150
            this->position = walk(total.x > 0);
154
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   151
        return; 
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   152
        //total.x = 0;
114
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   153
    }
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   154
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   155
    integrate(total, PHYSICS_TICK_MS);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   156
84
3cb862028a24 No nyt se toimii v?h?n paremmin. Koodia pit?? kyll? viel? siisti? aika huolella.
saiam
parents: 83
diff changeset
   157
    Vector newPosition = posAfterTick /*+ (velAfterTick * PHYSICS_TICK_MS)/1000*/;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   158
    this->velocity = velAfterTick;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   159
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   160
   
114
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   161
    // Collision detection
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   162
    bool collided = false;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   163
   
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   164
    const Vector diffVec = newPosition-position;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   165
    const Vector unitVector = diffVec / diffVec.length();
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   166
    Vector reached = position;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   167
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   168
    while ((position-reached).length() < diffVec.length()) {
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   169
        // Check if any of the shapes points collide
154
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   170
        for (uint64_t i = 0; i < shape.size(); i++) {
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   171
            if (world.getType(reached+shape[i]) != EMPTY) {  // Collision
154
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   172
                if (inAir) {
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   173
                    //                    Engine::log(DEBUG, "Here");
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   174
                    this->bounce(world.getNormal(reached+shape[i], 
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   175
                                                 reached-unitVector+shape[i]));
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   176
                    //this->velocity *= COLLISION_ELASTICITY;
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   177
                }
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   178
                reached = reached - unitVector; // Return to last point
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   179
                collided = true;
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   180
                if (this->velocity.length() < PLAYER_MIN_SPEED) {
115
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   181
                    this->inAir = false;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   182
                    this->velocity = Vector(0,0);
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   183
                }
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   184
                break;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   185
            }
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   186
        }
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   187
        if (collided)
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   188
            break;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   189
        reached += unitVector;
237ea0bb125a Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents: 114
diff changeset
   190
    }
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   191
   
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   192
    
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   193
    /*
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   194
      bool collided = false;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   195
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   196
      //goes 1 unit forward every step and check if has hit anything
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   197
      Vector unitVector = (newPosition-position) / (newPosition-position).length();
114
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   198
    
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   199
      Vector tmpVector = position;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   200
      Vector reached = position;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   201
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   202
      int steps = (int) (newPosition-position).length() + 2;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   203
        
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   204
      //Engine::log(DEBUG, "physics.update_position") << unitVector-newPosition;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   205
      //Vector foo = position+unitVector*steps-newPosition;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   206
      //Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Virhe: "<< foo;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   207
    
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   208
      for(int i = 0; i < steps; i++) {
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   209
      tmpVector += unitVector;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   210
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   211
      float minVelocity = 10;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   212
      // Check if any of the four corners of the worm collide
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   213
      for(int sh = 0; sh < 4; sh++) {
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   214
      if(world.getType(tmpVector+shape[sh]) != EMPTY) {
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   215
      reached = position + unitVector*(i-1);
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   216
      collided = true;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   217
      this->bounce(world.getNormal(tmpVector+shape[sh], tmpVector-unitVector+shape[sh]));
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   218
      this->velocity *= 0.4;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   219
      if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   220
      this->inAir = false;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   221
      this->velocity = Vector(0,0);
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   222
      }
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   223
      break;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   224
      }
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   225
      }
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   226
      if(collided)
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   227
      break;
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   228
      }
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   229
    */
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   230
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   231
    // In case of some float error check the final coordinate
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   232
    if(!collided) {
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   233
        if(world.getType(newPosition+shape[0]) != EMPTY || (world.getType(newPosition+shape[1]) != EMPTY) 
114
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 112
diff changeset
   234
           || (world.getType(newPosition+shape[2]) != EMPTY) || (world.getType(newPosition+shape[3]) != EMPTY)) {
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   235
            
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 148
diff changeset
   236
            //  Engine::log(DEBUG, "physics.update_position") << "didnt hit";
112
1b9ad61bdf2d added method for removing ground, not used anywhere
nireco
parents: 111
diff changeset
   237
            // There was error, and there is ground
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   238
            //newPosition = tmpVector;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   239
        } else {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   240
            // This means everything was ok, so no need to do anything
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   241
        }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   242
    } else {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   243
        newPosition = reached;
116
0d36aade845e some stuff, don't remember what
nireco
parents: 115
diff changeset
   244
        onCollision();
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   245
        //this->velocity = Vector(0, 0);
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   246
        //TODO: it shouldn't just stop on collision
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   247
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   248
    this->position = newPosition;
158
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   249
//    Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Pos: " << this->position;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   250
}
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   251
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   252
/**
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   253
 * Bounces from straight wall in any direction.
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   254
 * Direction given as normal of that wall
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   255
 */
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   256
void PhysicsObject::bounce (Vector normal) {
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 148
diff changeset
   257
    if (normal.length() != 0) {
154
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   258
        // Engine::log(DEBUG, "PhysicsObject.bounce") << "Velocity: " << velocity;
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   259
        Vector nvel = velocity;
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   260
        //        Engine::log(DEBUG, "PhysicsObject.bounce") << "New Velocity: " << nvel;
160
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   261
//        nvel = nvel - ((1+COLLISION_ELASTICITY)*((nvel*normal)/(normal*normal))*normal);
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   262
        
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   263
        nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal);
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   264
//        Engine::log(DEBUG, "PhysicsObject.bounce") << "Projection: " << nvel;
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   265
        velocity = nvel;
154
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   266
        this->velocity *= COLLISION_ELASTICITY;
78d144a48354 This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents: 153
diff changeset
   267
    }
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   268
}
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   269
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   270
/**
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   271
 * Integrates given force over time and stores new position to
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   272
 * posAfterTick and new velocity to velAfterTick.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   273
 * @param force Force vector.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   274
 * @param dt The time the force is applied (<=PHYSICS_TICK_MS)
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   275
 */
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   276
void PhysicsObject::integrate(Force force, TimeMS dt) {
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   277
    Derivative tmpd;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   278
    Derivative k1 = evaluate(force, 0, tmpd);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   279
    Derivative k2 = evaluate(force, 0.5f*dt, k1);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   280
    Derivative k3 = evaluate(force, 0.5f*dt, k2);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   281
    Derivative k4 = evaluate(force, dt, k3);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   282
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   283
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   284
    const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   285
    const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   286
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   287
    //    Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   288
    posAfterTick = posAfterTick + (dxdt * dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   289
    velAfterTick = velAfterTick + (dvdt * dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   290
    //Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick;
70
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   291
}
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   292
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   293
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d) {
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   294
    Vector curPos = posAfterTick + (d.dx*dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   295
    Vector curVel = velAfterTick + (d.dv*dt)/1000;
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
   296
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   297
    Derivative out;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   298
    out.dx = curVel;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   299
    out.dv = acceleration(force);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   300
    //Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   301
    return out;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   302
}
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   303
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   304
Vector PhysicsObject::acceleration(const Force &force) {
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   305
    return (force/mass);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   306
}
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   307
122
16a73ebca810 No warnings anymore, but well have to think about that applyForce
saiam
parents: 116
diff changeset
   308
void PhysicsObject::applyForce (Force force) {
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   309
    // Add applied force to the queue
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   310
    forceq.push(force);
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   311
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   312
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   313
void PhysicsObject::changeAim(float da) {
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   314
    this->aim += da;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   315
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   316
    if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   317
    if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN;
109
   318
    //Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim;
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   319
}
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   320
128
890ac82cdcc0 Documenting more, cleaning variables. This code needs some serious
saiam
parents: 124
diff changeset
   321
void PhysicsObject::setFacing(bool facingRight) {
109
   322
    //Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right;
128
890ac82cdcc0 Documenting more, cleaning variables. This code needs some serious
saiam
parents: 124
diff changeset
   323
    this->facingRight = facingRight;
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   324
}
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   325
107
505bfa531496 send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents: 106
diff changeset
   326
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir) {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   327
    this->position = position;
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   328
    this->velocity = velocity;
107
505bfa531496 send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents: 106
diff changeset
   329
    this->inAir = inAir;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   330
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   331
    
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   332
Vector PhysicsObject::getPosition () {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   333
    return this->position;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   334
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   335
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   336
bool PhysicsObject::getFacing() {
128
890ac82cdcc0 Documenting more, cleaning variables. This code needs some serious
saiam
parents: 124
diff changeset
   337
    return this->facingRight;
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   338
}
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   339
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   340
float PhysicsObject::getAim() {
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   341
    return this->aim;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   342
}
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 107
diff changeset
   343
91
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   344
std::vector<Vector>& PhysicsObject::getShape () {
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   345
    return this->shape;
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   346
}
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   347
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   348
void PhysicsObject::setShape (std::vector<Vector> shape) {
123
7efb63402b2b Documenting a little bit
saiam
parents: 122
diff changeset
   349
    this->shape = shape;
91
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   350
}
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   351
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   352
void PhysicsObject::tick () {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   353
    this->updatePosition();
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   354
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   355
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   356
void PhysicsObject::draw(CL_GraphicContext *gc) {
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   357
    CL_Quad player(
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   358
                   (position+shape[0]).x, (position+shape[0]).y,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   359
                   (position+shape[1]).x, (position+shape[1]).y,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   360
                   (position+shape[2]).x, (position+shape[2]).y,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   361
                   (position+shape[3]).x, (position+shape[3]).y
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   362
                   );
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   363
    
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   364
    gc->fill_quad(player, CL_Color::green);
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   365
    
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   366
    const uint16_t chlen = 10;
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   367
    uint16_t x = player.center().x;
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   368
    uint16_t y = player.center().y;
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   369
    if (facingRight) {
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   370
        gc->draw_line(x, y,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   371
                      x + std::cos(aim)*chlen,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   372
                      y - std::sin(aim)*chlen,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   373
                      CL_Color::black);
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   374
    } else {
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   375
        gc->draw_line(x, y,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   376
                      x - std::cos(aim)*chlen,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   377
                      y - std::sin(aim)*chlen,
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   378
                      CL_Color::black);
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   379
    }
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   380
}
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 151
diff changeset
   381