src/Physics.cc
author nireco
Thu, 04 Dec 2008 15:06:08 +0000
changeset 190 12fea0d2d0df
parent 189 41f8913f7d15
child 191 98fcc0843335
permissions -rw-r--r--
forgot to return anything on new walk
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
#include "Physics.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#include "Engine.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
#include "GameState.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     4
#include "Terrain.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
#include <algorithm>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
#include <functional>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
#include <cmath>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
#include <assert.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
    : Terrain(1337), tick_timer(PHYSICS_TICK_MS), tick_counter(0), dimensions(dimensions),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    13
      gravity(gravity) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
    slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
    tick_timer.enable();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    16
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
void PhysicsWorld::addPlayerObject (PhysicsObject *object) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    19
    players.push_back(object);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    20
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    22
void PhysicsWorld::addProjectile (PhysicsObject *projectile) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    23
    projectiles.push_back(projectile);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    25
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    26
void PhysicsWorld::tick () {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
    //    Engine::log(DEBUG, "physics.apply_force") << "*tick*";
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    28
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    29
    for (std::list<PhysicsObject*>::iterator i = players.begin(); i != players.end(); i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    30
        (*i)->tick(); 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    31
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    32
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    33
    for (std::list<PhysicsObject*>::iterator i = projectiles.begin(); i != projectiles.end(); i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    34
        (*i)->tick();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    35
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    36
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    37
    tick_counter++;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    38
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    39
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    40
uint32_t PhysicsWorld::getTick (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    41
    return tick_counter;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    42
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    43
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    44
void PhysicsWorld::draw(CL_GraphicContext *gc) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    45
    Terrain::draw(gc);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    46
   
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    47
    // Draw players
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    48
    for (std::list<PhysicsObject*>::iterator it = players.begin(); it != players.end(); it++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    49
        (*it)->draw(gc);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    50
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    51
    // Draw projectiles
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    52
    for (std::list<PhysicsObject*>::iterator it = projectiles.begin(); it != projectiles.end(); it++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    53
        (*it)->draw(gc);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    54
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    55
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    56
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    57
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    58
                              Vector position, Vector velocity)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    59
    : world(world), position(position), velocity(velocity),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    60
      mass(mass), inAir(true), aim(0), facingRight(true), reloadTimer(0) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
    // TODO: Is thir the right way to do this?
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    62
    //world.addPlayerObject(this);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    63
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    64
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    65
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    66
 * Player walks on floor.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    67
 */
189
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
    68
Vector PhysicsObject::walk_one_step (float partial, bool right) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    69
    // which way we are walking
189
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
    70
    float deltaX = right ? partial : -partial;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    71
    Vector reached = this->position;
189
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
    72
    if(reached.roundToInt() == (reached+Vector(deltaX, 0)).roundToInt()) {
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
    73
        return reached+Vector(deltaX, 0);
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
    74
    }
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    75
    // Is there upward ramp
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    76
    if(!possibleLocation(position+Vector(deltaX, 0))) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    77
        // Yes. Then we check n pixels up
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    78
        for(int i = 1; i < 3; i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79
            if(possibleLocation(position+Vector(deltaX, -i))) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    80
                // and when there is finally EMPTY, we can walk
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    81
                reached = position+Vector(deltaX, -i);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    82
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    83
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    84
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    85
    } else {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    86
        // Or downward ramp or flat
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    87
        for(int i = 0; 1; i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    88
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    89
            // And when there is finally ground we can step on
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    90
            // it. If there is no gound we still step there,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    91
            // but will fall one pixel down
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    92
            if(possibleLocation(position+Vector(deltaX, i))) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    93
                reached = position+Vector(deltaX, i);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    94
            } else {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    95
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    96
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    97
            
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    98
            // If the fall is big enough, set the worm in the air
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
            if (i >= 2) {
189
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   100
//                Vector back = walk(dt, !right);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   101
                this->inAir = true;
189
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   102
//                this->velocity.x = right ? velocity : -velocity;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   103
                // Avoid stepping two pixels down when it starts to free fall
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   104
                reached.y -= 2;
189
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   105
//                this->velocity = (reached-back)*1000/dt;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   106
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   107
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   108
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   109
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   110
    // And we return where we got
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   111
    return reached;
189
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   112
}
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   113
Vector PhysicsObject::walk (TimeMS dt, bool right) {
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   114
    float velocity = PLAYER_WALK_SPEED;
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   115
    float walkAmount = (velocity*dt)/1000;
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   116
    Vector reached = this->position;
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   117
    while(walkAmount > 0) {
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   118
        walk_one_step((1 < walkAmount ? 1 : walkAmount), right);
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   119
        walkAmount--;
41f8913f7d15 walk has now fixable velocity in Config.hh
nireco
parents: 185
diff changeset
   120
    }
190
12fea0d2d0df forgot to return anything on new walk
nireco
parents: 189
diff changeset
   121
    return reached;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   122
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   123
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   124
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   125
 * Makes the player jump in the air.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   126
 * @param direction -1: jump left, 0: jump up, 1: jump right
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   128
void PhysicsObject::jump (int direction) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   129
    // Jump only if player is "on the ground"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   130
    if (!this->inAir) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   131
 	    velocity.y = -100;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   132
        switch (direction) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   133
            case 1:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   134
                velocity.x += 20;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   135
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   136
            case -1:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   137
                velocity.x -= 20;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   138
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   139
            case 0:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   140
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   141
            default:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   142
                throw std::logic_error("Invalid jump direction");
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   143
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   144
	    inAir = true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   145
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   146
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   147
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   148
bool PhysicsObject::possibleLocation (Vector loc) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   149
    for(unsigned int i = 0; i < this->shape.size(); i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   150
        if(world.collides(loc+shape[i]))
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   151
            return false;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   152
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   153
    return true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   154
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   155
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   156
void func1() {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   157
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   158
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   159
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   160
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   161
 * Updates object speed and position. This function organises force
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   162
 * integration and collision detection.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   163
 */   
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   164
void PhysicsObject::updatePosition () {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   165
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   166
    // Reloads weapon if not reloaded
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   167
    reloadTimer -= PHYSICS_TICK_MS;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   168
    if(reloadTimer < 0)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   169
        reloadTimer = 0;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   170
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   171
    // Add gravity to the force queue
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   172
    forceq.push(world.gravity);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   173
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   174
    // Go trough every force in the queue
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   175
    Force total;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   176
    while (!forceq.empty()) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   177
        total += forceq.front();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   178
        forceq.pop();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   179
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   180
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   181
    // If the player has stopped and there's some ground under some of the 3 some of the 3t
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   182
    // set inAir false
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   183
    if (this->velocity == Vector(0,0)) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   184
        this->inAir = !world.collides(this->position+shape[1]+Vector(0, 1))
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   185
                      && !world.collides(this->position+shape[2]+Vector(0, 1))
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   186
                      && !world.collides(this->position+shape[3]+Vector(0, 1));
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   187
        // If, however, there's a force caused by a bomb, e.g., set it in air.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   188
        // Still, we have to be able to separate forces caused by walking attempts
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   189
        // and bombs etc (+0.1 because float comparison can be dangerous)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   190
        if (total.y < 0 || abs(total.x) > PLAYER_MOVE_FORCE + 0.1)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   191
            this->inAir = true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   192
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   193
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   194
    if(!possibleLocation(position)) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   195
        //if we are trapped in ground form dirtball or something
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   196
        //we might want to just return and set velocity to some value
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   197
        //return;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   198
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   199
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   200
    // If the worm is not in the air make it walk,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   201
    // otherwise integrate the new position and velocity
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   202
    if (!this->inAir) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   203
        //std::cout << "Tryin to walk" << std::endl;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   204
        // It walks only if there's some vertical force
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   205
        if (total.x != 0) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   206
            std::cout << "Succeeding to walk" << std::endl;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   207
            this->position = walk(PHYSICS_TICK_MS, total.x > 0);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   208
            this->velocity = Vector(0,0);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   209
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   210
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   211
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   212
    if(!possibleLocation(position)) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   213
        Engine::log(DEBUG, "great failure") << "great failure";
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   214
        func1();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   215
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   216
    Vector newPosition;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   217
    Vector velAfterTick;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   218
    // Calculate new position and velocity to the given references
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   219
    integrate(total, PHYSICS_TICK_MS, newPosition, velAfterTick);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   220
    this->velocity = velAfterTick;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   221
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   222
   
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   223
    // Collision detection
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   224
    bool collided = false;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   225
   
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   226
    const Vector diffVec = newPosition-position;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   227
    const Vector unitVector = diffVec / diffVec.length();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   228
    Vector reached = position;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   229
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   230
    while ((position-reached).sqrLength() < diffVec.sqrLength()) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   231
        reached += unitVector;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   232
        // Check if any of the shapes points collide
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   233
        for (uint64_t i = 0; i < shape.size(); i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   234
            if (world.collides(reached+shape[i])) {  // Collision
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   235
                if (inAir) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   236
                    //                    Engine::log(DEBUG, "Here");
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   237
                    this->bounce(world.getNormal(reached+shape[i], 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   238
                                                 reached-unitVector+shape[i]));
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   239
                    //this->velocity *= COLLISION_ELASTICITY;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   240
                }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   241
                reached = reached - unitVector; // Return to last point
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   242
                collided = true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   243
                if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   244
                    this->velocity = Vector(0,0);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   245
                }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   246
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   247
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   248
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   249
        if (collided)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   250
            break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   251
//        reached += unitVector;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   252
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   253
   
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   254
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   255
    if(!possibleLocation(reached)) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   256
        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "logic error reached should not be possible to be impossible.. diffVec: " << diffVec;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   257
        func1();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   258
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   259
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   260
    // In case of some float error check the final coordinate
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   261
    if(!collided) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   262
        if(!possibleLocation(newPosition)) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   263
            newPosition = reached;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   264
        } else {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   265
            // This means everything was ok, so no need to do anything
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   266
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   267
    } else {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   268
        newPosition = reached;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   269
        onCollision();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   270
        //this->velocity = Vector(0, 0);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   271
        //TODO: it shouldn't just stop on collision
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   272
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   273
    if(!possibleLocation(newPosition)) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   274
        Engine::log(DEBUG, "great failure") << "great failure";
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   275
        func1();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   276
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   277
    this->position = newPosition;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   278
    if(!possibleLocation(position)) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   279
        Engine::log(DEBUG, "great failure") << "great failure";
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   280
        func1();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   281
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   282
//    Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Pos: " << this->position;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   283
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   284
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   285
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   286
 * Bounces from straight wall in any direction.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   287
 * Direction given as normal of that wall
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   288
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   289
void PhysicsObject::bounce (Vector normal) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   290
    // normal.sqrLength can't be 0 when got from getNormal()
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   291
    if (normal.sqrLength() != 0) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   292
        Vector nvel = velocity;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   293
        // We project the velocity on normal and remove twice that much from velocity
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   294
        nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   295
        velocity = nvel;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   296
        // We lose some of our speed on collision
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   297
        this->velocity *= this->collision_elasticity;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   298
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   299
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   300
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   301
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   302
 * Integrates given force over time and stores new position to
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   303
 * posAfterTick and new velocity to velAfterTick.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   304
 * @param force Force vector.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   305
 * @param dt The time the force is applied (<=PHYSICS_TICK_MS)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   306
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   307
void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   308
    posAfterTick = position;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   309
    velAfterTick = velocity;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   310
    Derivative tmpd;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   311
    Derivative k1 = evaluate(force, 0, tmpd, posAfterTick, velAfterTick);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   312
    Derivative k2 = evaluate(force, 0.5f*dt, k1, posAfterTick, velAfterTick);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   313
    Derivative k3 = evaluate(force, 0.5f*dt, k2, posAfterTick, velAfterTick);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   314
    Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   315
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   316
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   317
    const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   318
    const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   319
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   320
    //    Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   321
    posAfterTick = posAfterTick + (dxdt * dt)/1000;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   322
    velAfterTick = velAfterTick + (dvdt * dt)/1000;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   323
    //Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   324
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   325
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   326
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   327
    Vector curPos = posAfterTick + (d.dx*dt)/1000;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   328
    Vector curVel = velAfterTick + (d.dv*dt)/1000;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   329
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   330
    Derivative out;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   331
    out.dx = curVel;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   332
    out.dv = acceleration(force);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   333
    //Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   334
    return out;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   335
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   336
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   337
Vector PhysicsObject::acceleration(const Force &force) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   338
    return (force/mass);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   339
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   340
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   341
void PhysicsObject::applyForce (Force force) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   342
    // Add applied force to the queue
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   343
    forceq.push(force);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   344
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   345
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   346
void PhysicsObject::changeAim(float da) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   347
    this->aim += da;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   348
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   349
    if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   350
    if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   351
    //Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   352
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   353
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   354
void PhysicsObject::setFacing(bool facingRight) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   355
    //Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   356
    this->facingRight = facingRight;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   357
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   358
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   359
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   360
    this->position = position;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   361
    this->velocity = velocity;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   362
    this->inAir = inAir;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   363
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   364
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   365
Vector PhysicsObject::getPosition () {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   366
    return this->position;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   367
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   368
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   369
bool PhysicsObject::getFacing() {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   370
    return this->facingRight;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   371
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   372
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   373
float PhysicsObject::getAim() {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   374
    return this->aim;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   375
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   376
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   377
std::vector<Vector>& PhysicsObject::getShape () {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   378
    return this->shape;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   379
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   380
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   381
void PhysicsObject::setShape (std::vector<Vector> shape) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   382
    this->shape = shape;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   383
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   384
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   385
void PhysicsObject::tick () {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   386
    this->updatePosition();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   387
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   388
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   389
bool PhysicsObject::canShoot() {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   390
    return this->reloadTimer <= 0;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   391
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   392
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   393
void PhysicsObject::draw(CL_GraphicContext *gc) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   394
    CL_Quad player(
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   395
                   (position+shape[0]).x, (position+shape[0]).y,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   396
                   (position+shape[1]).x, (position+shape[1]).y,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   397
                   (position+shape[2]).x, (position+shape[2]).y,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   398
                   (position+shape[3]).x, (position+shape[3]).y
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   399
                   );
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   400
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   401
    gc->fill_quad(player, CL_Color::green);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   402
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   403
    const uint16_t chlen = 10;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   404
    uint16_t x = player.center().x;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   405
    uint16_t y = player.center().y;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   406
    if (facingRight) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   407
        gc->draw_line(x, y,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   408
                      x + std::cos(aim)*chlen,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   409
                      y - std::sin(aim)*chlen,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   410
                      CL_Color::black);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   411
    } else {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   412
        gc->draw_line(x, y,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   413
                      x - std::cos(aim)*chlen,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   414
                      y - std::sin(aim)*chlen,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   415
                      CL_Color::black);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   416
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   417
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   418