src/PhysicsObject.cc
author saiam
Tue, 09 Dec 2008 00:45:18 +0000
changeset 336 ef598a3dc1b7
parent 322 f94a5c192097
child 370 39e59dd36b6e
permissions -rw-r--r--
Rope aplies forcce to both players
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     1
#include "PhysicsObject.hh"
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     2
#include "Engine.hh"
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     3
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     4
#include <cmath>
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
     5
#include <utility>
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
     6
#include <queue>
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     7
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
     8
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity, ObjectType type, 
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
     9
            float collision_elasticity, bool enabled) :
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    10
    world(world), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    11
    position(position), 
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
    12
    previousPosition(position),
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    13
    velocity(velocity), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    14
    mass(mass), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    15
    inAir(true), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    16
    collision_elasticity(collision_elasticity),
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    17
    aim(0), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    18
    facing(FACING_RIGHT), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    19
    alive(false),
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    20
    shouldDelete(false), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    21
    type(type), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    22
    pivot(NULL)
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    23
{  
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    24
    if (enabled)
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    25
        enable();  
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
    26
}
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
    27
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
    28
PhysicsObject::~PhysicsObject (void) {
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    29
//    Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    30
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    31
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    32
/**
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    33
 * Player walks on floor.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    34
 */
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    35
Vector PhysicsObject::walk_one_step (float partial, bool right) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    36
    // which way we are walking
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    37
    float deltaX = right ? partial : -partial;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    38
    Vector reached = this->position;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    39
    if(reached.roundToInt() == (reached+Vector(deltaX, 0)).roundToInt()) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    40
        return reached+Vector(deltaX, 0);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    41
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    42
    // Is there upward ramp
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    43
    if(!possibleLocation(position+Vector(deltaX, 0))) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    44
        // Yes. Then we check n pixels up
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    45
        for(int i = 1; i < 3; i++) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    46
            if(possibleLocation(position+Vector(deltaX, -i))) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    47
                // and when there is finally EMPTY, we can walk
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    48
                reached = position+Vector(deltaX, -i);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    49
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    50
            }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    51
        }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    52
    } else {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    53
        // Or downward ramp or flat
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    54
        for(int i = 0; 1; i++) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    55
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    56
            // And when there is finally ground we can step on
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    57
            // it. If there is no gound we still step there,
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    58
            // but will fall one pixel down
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    59
            if(possibleLocation(position+Vector(deltaX, i))) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    60
                reached = position+Vector(deltaX, i);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    61
            } else {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    62
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    63
            }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    64
            
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    65
            // If the fall is big enough, set the worm in the air
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    66
            if (i >= 2) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    67
//                Vector back = walk(dt, !right);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    68
                this->inAir = true;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    69
//                this->velocity.x = right ? velocity : -velocity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    70
                // Avoid stepping two pixels down when it starts to free fall
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    71
                reached.y -= 2;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    72
//                this->velocity = (reached-back)*1000/dt;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    73
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    74
            }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    75
        }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    76
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    77
    // And we return where we got
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    78
    return reached;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    79
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    80
void PhysicsObject::walk (TimeMS dt, bool right) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    81
    float velocity = PLAYER_WALK_SPEED;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    82
    float walkAmount = (velocity*dt)/1000;
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
    83
    while (walkAmount > 0){// && !this->inAir) {
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
    84
        setPosition (walk_one_step((1 < walkAmount ? 1 : walkAmount), right));
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    85
        walkAmount--;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    86
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    87
    // TODO: Should the remaining walkAmount be handled somehow?
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    88
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    89
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    90
/**
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    91
 * Makes the player jump in the air.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    92
 * @param direction -1: jump left, 0: jump up, 1: jump right
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    93
 */
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    94
void PhysicsObject::jump (int direction) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    95
    // Jump only if player is "on the ground"
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    96
    if (!this->inAir) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    97
 	    velocity.y = -100;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    98
        switch (direction) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    99
            case 1:
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   100
                velocity.x += 20;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   101
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   102
            case -1:
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   103
                velocity.x -= 20;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   104
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   105
            case 0:
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   106
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   107
            default:
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   108
                throw std::logic_error("Invalid jump direction");
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   109
        }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   110
	    inAir = true;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   111
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   112
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   113
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   114
bool PhysicsObject::possibleLocation (Vector loc) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   115
    for(unsigned int i = 0; i < this->shape.size(); i++) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   116
        if(world.collides(loc+shape[i]))
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   117
            return false;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   118
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   119
    return true;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   120
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   121
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   122
/**
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   123
 * Updates object speed and position. This function organises force
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   124
 * integration and collision detection.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   125
 */   
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 200
diff changeset
   126
void PhysicsObject::updatePosition (TimeMS dt) {
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   127
    // Add gravity to the force queue
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   128
    applyForce(world.gravity);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   129
    
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   130
    // If the object (practically player) has a pivot point add
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   131
    // a force towards that
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   132
    if (pivot != NULL) {
322
f94a5c192097 Rope fixing
ekku
parents: 299
diff changeset
   133
        applyForce(getPivotForce());
336
ef598a3dc1b7 Rope aplies forcce to both players
saiam
parents: 322
diff changeset
   134
        if (pivot->type == PLAYER) {
ef598a3dc1b7 Rope aplies forcce to both players
saiam
parents: 322
diff changeset
   135
            pivot->applyForce(getPivotForce()*(-1));
ef598a3dc1b7 Rope aplies forcce to both players
saiam
parents: 322
diff changeset
   136
        }
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   137
    }
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   138
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   139
    std::pair<Force, TimeMS> force;
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   140
    std::queue<std::pair<Force, TimeMS> > newfq;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   141
    Force total;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   142
    while (!forceq.empty()) {
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   143
        force = forceq.front();
275
fa44b905bc2e Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents: 273
diff changeset
   144
        if (force.second > dt) {
fa44b905bc2e Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents: 273
diff changeset
   145
            force.second -= dt;
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   146
            newfq.push(force);
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   147
        }
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   148
        total += force.first;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   149
        forceq.pop();
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   150
    }
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   151
    forceq = newfq;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   152
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   153
    // If the player has stopped and there's some ground under some of the 3 some of the 3t
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   154
    // set inAir false
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   155
    if (this->velocity == Vector(0,0)) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   156
        this->inAir = !world.collides(this->position+shape[1]+Vector(0, 1))
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   157
                      && !world.collides(this->position+shape[2]+Vector(0, 1))
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   158
                      && !world.collides(this->position+shape[3]+Vector(0, 1));
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   159
        // If, however, there's a force caused by a bomb, e.g., set it in air.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   160
        // Still, we have to be able to separate forces caused by walking attempts
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   161
        // and bombs etc (+0.1 because float comparison can be dangerous)
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   162
        if (total.y < 0.01 || fabs(total.x) > PLAYER_MOVE_FORCE + 0.1)
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   163
            this->inAir = true;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   164
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   165
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   166
    if(!possibleLocation(position)) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   167
        //if we are trapped in ground form dirtball or something
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   168
        //we might want to just return and set velocity to some value
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   169
        //return;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   170
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   171
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   172
    // If the worm is not in the air make it walk,
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   173
    // otherwise integrate the new position and velocity
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   174
    if (!this->inAir) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   175
        // It walks only if there's some vertical force
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   176
        if (total.x != 0) {
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 200
diff changeset
   177
            walk(dt, total.x > 0);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   178
            this->velocity = Vector(0,0);
247
b87f68be579f When on the ground, dont integrate in vain
ekku
parents: 241
diff changeset
   179
        }   
b87f68be579f When on the ground, dont integrate in vain
ekku
parents: 241
diff changeset
   180
        // Now the possible walking has been done so we can return from this function.
b87f68be579f When on the ground, dont integrate in vain
ekku
parents: 241
diff changeset
   181
        // In walk inAir could have been set true, but that will be handled in the next tick.
b87f68be579f When on the ground, dont integrate in vain
ekku
parents: 241
diff changeset
   182
        return;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   183
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   184
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   185
    if (!possibleLocation(position))
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   186
        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position;
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   187
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   188
    Vector newPosition;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   189
    Vector velAfterTick;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   190
    // Calculate new position and velocity to the given references
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 200
diff changeset
   191
    integrate(total, dt, newPosition, velAfterTick);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   192
    this->velocity = velAfterTick;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   193
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   194
    // Collision detection
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   195
    bool collided = false;
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 247
diff changeset
   196
    Vector collisionPoint;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   197
   
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   198
    const Vector diffVec = newPosition - position;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   199
    const Vector unitVector = diffVec / diffVec.length();
297
809a0f84d581 bouncy bouncy
nireco
parents: 288
diff changeset
   200
    if(unitVector == Vector(0, 0)) {
809a0f84d581 bouncy bouncy
nireco
parents: 288
diff changeset
   201
        return;
809a0f84d581 bouncy bouncy
nireco
parents: 288
diff changeset
   202
    }
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   203
    Vector reached = position;
247
b87f68be579f When on the ground, dont integrate in vain
ekku
parents: 241
diff changeset
   204
    
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   205
    while ((position - reached).sqrLength() < diffVec.sqrLength()) {
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   206
        reached += unitVector;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   207
        // Check if any of the shapes points collide
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   208
        for (uint64_t i = 0; i < shape.size(); i++) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   209
            if (world.collides(reached+shape[i])) {  // Collision
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 247
diff changeset
   210
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 247
diff changeset
   211
                collisionPoint = reached+shape[i];
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 247
diff changeset
   212
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   213
                if (inAir)
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   214
                    this->bounce(world.getNormal(reached+shape[i], reached-unitVector+shape[i]));
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   215
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   216
                reached = reached - unitVector; // Return to last point
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   217
                collided = true;
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   218
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   219
                if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED)
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   220
                    this->velocity = Vector(0,0);
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   221
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   222
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   223
            }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   224
        }
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   225
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   226
        if (collided)
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   227
            break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   228
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   229
   
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   230
    
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   231
    if (!possibleLocation(reached))
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   232
        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position << ", diffVec=" << diffVec;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   233
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   234
    // In case of some float error check the final coordinate
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   235
    if (!collided) {
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   236
        if (!possibleLocation(newPosition)) {
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   237
            newPosition = reached;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   238
        } else {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   239
            // This means everything was ok, so no need to do anything
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   240
        }
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   241
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   242
        setPosition (newPosition);
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   243
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   244
    } else {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   245
        newPosition = reached;
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   246
        setPosition (newPosition);
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   247
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   248
        // the following may delete this object, so it must be the last thing called
288
47a5d7896aec some collisions on PhysicsWorld::tick
nireco
parents: 287
diff changeset
   249
        onCollision(collisionPoint);
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   250
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   251
        return;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   252
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   253
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   254
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   255
/**
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   256
 * Bounces from straight wall in any direction.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   257
 * Direction given as normal of that wall
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   258
 */
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   259
void PhysicsObject::bounce (Vector normal) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   260
    // normal.sqrLength can't be 0 when got from getNormal()
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   261
    if (normal.sqrLength() != 0) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   262
        Vector nvel = velocity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   263
        // We project the velocity on normal and remove twice that much from velocity
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   264
        nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   265
        velocity = nvel;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   266
        // We lose some of our speed on collision
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   267
        this->velocity *= this->collision_elasticity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   268
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   269
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   270
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   271
/**
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   272
 * Integrates given force over time and stores new position to
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   273
 * posAfterTick and new velocity to velAfterTick.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   274
 * @param force Force vector.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   275
 * @param dt The time the force is applied (<=PHYSICS_TICK_MS)
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   276
 */
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   277
void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   278
    posAfterTick = position;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   279
    velAfterTick = velocity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   280
    Derivative tmpd;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   281
    Derivative k1 = evaluate(force, 0, tmpd, posAfterTick, velAfterTick);
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   282
    Derivative k2 = evaluate(force, dt / 2, k1, posAfterTick, velAfterTick);
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   283
    Derivative k3 = evaluate(force, dt / 2, k2, posAfterTick, velAfterTick);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   284
    Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   285
    
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   286
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   287
    const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   288
    const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   289
    
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   290
    //    Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   291
    posAfterTick = posAfterTick + (dxdt * dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   292
    velAfterTick = velAfterTick + (dvdt * dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   293
    //Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   294
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   295
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   296
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   297
    Vector curPos = posAfterTick + (d.dx*dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   298
    Vector curVel = velAfterTick + (d.dv*dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   299
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   300
    Derivative out;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   301
    out.dx = curVel;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   302
    out.dv = acceleration(force);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   303
    //Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   304
    return out;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   305
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   306
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   307
Vector PhysicsObject::acceleration(const Force &force) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   308
    return (force/mass);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   309
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   310
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   311
void PhysicsObject::applyForce (Force force, TimeMS dt) {
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   312
    // Add applied force to the queue
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   313
    forceq.push(std::make_pair(force, dt));
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   314
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   315
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   316
void PhysicsObject::changeAim(float da) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   317
    this->aim += da;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   318
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   319
    if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   320
    if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   321
    //Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   322
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   323
287
f59c8dee7f91 getType added for physics object
ekku
parents: 285
diff changeset
   324
ObjectType PhysicsObject::getType (void) const {
f59c8dee7f91 getType added for physics object
ekku
parents: 285
diff changeset
   325
    return this->type;
f59c8dee7f91 getType added for physics object
ekku
parents: 285
diff changeset
   326
}
f59c8dee7f91 getType added for physics object
ekku
parents: 285
diff changeset
   327
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   328
void PhysicsObject::setFacing (FacingDirection facing) {
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   329
    this->facing = facing;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   330
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   331
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   332
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir, FacingDirection facing, float aim) {
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   333
    setPosition (position);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   334
    this->velocity = velocity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   335
    this->inAir = inAir;
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   336
    this->facing = facing;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 197
diff changeset
   337
    this->aim = aim;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   338
}
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   339
257
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   340
Vector PhysicsObject::getPosition (void) const {
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   341
    return position;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   342
}
257
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   343
    
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   344
Vector PhysicsObject::getPreviousPosition (void) const {
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   345
    return previousPosition;
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   346
}
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   347
    
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   348
void PhysicsObject::setPosition (Vector pos) {
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   349
    this->previousPosition = this->position;
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   350
    this->position = pos;
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   351
}
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   352
    
257
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   353
PixelCoordinate PhysicsObject::getCoordinate (void) const {
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   354
    return world.getPixelCoordinate(position);
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   355
}
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   356
257
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   357
Vector PhysicsObject::getVelocity (void) const {
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   358
    return velocity;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   359
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   360
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   361
FacingDirection PhysicsObject::getFacing (void) const {
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   362
    return facing;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   363
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   364
257
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   365
float PhysicsObject::getAim (void) const {
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   366
    return aim;
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   367
}
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   368
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   369
Vector PhysicsObject::getDirection (void) const {
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   370
    return facing == FACING_RIGHT ? Vector(cos(aim), -sin(aim)) : Vector(-cos(aim), -sin(aim));
235
0a0c729365ee code cleanup
terom
parents: 229
diff changeset
   371
}
0a0c729365ee code cleanup
terom
parents: 229
diff changeset
   372
257
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   373
const std::vector<Vector>& PhysicsObject::getShape () const {
549783d71e51 add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents: 252
diff changeset
   374
    return shape;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   375
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   376
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   377
void PhysicsObject::setShape (std::vector<Vector> shape) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   378
    this->shape = shape;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   379
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   380
322
f94a5c192097 Rope fixing
ekku
parents: 299
diff changeset
   381
PhysicsObject *PhysicsObject::getPivot (void) {
f94a5c192097 Rope fixing
ekku
parents: 299
diff changeset
   382
    return this->pivot;
f94a5c192097 Rope fixing
ekku
parents: 299
diff changeset
   383
}
f94a5c192097 Rope fixing
ekku
parents: 299
diff changeset
   384
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   385
void PhysicsObject::setPivot (PhysicsObject *pivot) {
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   386
    this->pivot = pivot;
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   387
}
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   388
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 200
diff changeset
   389
void PhysicsObject::tick (TimeMS tick_length) {
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 200
diff changeset
   390
    this->updatePosition(tick_length);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   391
}
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   392
    
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   393
void PhysicsObject::enable (void) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   394
    // only enable once until disabled
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   395
    if (alive)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   396
        return;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   397
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   398
    // mark as alive
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   399
    alive = true;
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   400
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   401
    // add the world objects list
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   402
    world.addPhysicsObject(this);
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   403
}
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   404
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   405
void PhysicsObject::disable (void) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   406
    // mark as disabled
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   407
    alive = false;
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   408
}
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   409
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   410
void PhysicsObject::destroy (void) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   411
    // mark as disabled and for deletion
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   412
    alive = false;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   413
    shouldDelete = true;
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   414
}
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   415
    
299
e4dacf550ba1 implement players dying
terom
parents: 297
diff changeset
   416
bool PhysicsObject::isAlive (void) {
e4dacf550ba1 implement players dying
terom
parents: 297
diff changeset
   417
    return alive;
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   418
}
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   419
    
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   420
bool PhysicsObject::removeIfDestroyed (void) {
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   421
    if (!alive) {
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   422
        if (shouldDelete)
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   423
            delete this;
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   424
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   425
        return true;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   426
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   427
    } else {
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   428
        return false;
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   429
    }
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
   430
}
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   431
322
f94a5c192097 Rope fixing
ekku
parents: 299
diff changeset
   432
Vector PhysicsObject::getPivotForce (void) { 
f94a5c192097 Rope fixing
ekku
parents: 299
diff changeset
   433
    return Vector(0,0); 
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   434
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   435
268
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   436
bool PhysicsObject::collides (const PhysicsObject &obj) {
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   437
    const std::vector<Vector> oShape = obj.getShape();
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   438
    Vector p1, p2, p3;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   439
    int8_t sign, nsign;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   440
    for (std::vector<Vector>::const_iterator i = oShape.begin(); i != oShape.end(); i++) { // For every point in other shape
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   441
        p3 = *i + obj.getPosition();
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   442
        sign = 0;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   443
        for (std::vector<Vector>::const_iterator j = shape.begin(); j != shape.end(); j++) {
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   444
            p1 = *j + position;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   445
            if ( (j+1) == shape.end() ) p2 = *shape.begin() + position;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   446
            else p2 = *(j+1) + position;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   447
            nsign = crossProduct(p1, p2, p3);
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   448
            if ( sign == 0 ) sign = nsign;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   449
            else if ( ((sign < 0) && (nsign < 0)) || ((sign > 0) && (nsign > 0)) ) continue;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   450
            else return false;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   451
        }
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   452
    }
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   453
    return true;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   454
}
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   455
    
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   456
void  PhysicsObject::onCollision (Vector collisionPoint, PhysicsObject *other) {
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   457
    (void) collisionPoint;
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   458
    (void) other;
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   459
}
268
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   460
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   461
int8_t crossProduct (const Vector &p1, const Vector &p2, const Vector &p3) {
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   462
    float p = (p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x);
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   463
    if (p < 0)
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   464
        return -1;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   465
    else
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   466
        return 1;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   467
}