src/PhysicsObject.cc
author terom
Mon, 08 Dec 2008 12:02:20 +0000
changeset 282 e0e4dfc3e528
parent 279 e36f5e1a1c8d
child 285 c080c8c70333
permissions -rw-r--r--
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
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), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    12
    velocity(velocity), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    13
    mass(mass), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    14
    inAir(true), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    15
    collision_elasticity(collision_elasticity),
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    16
    aim(0), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    17
    facing(FACING_RIGHT), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    18
    alive(false),
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    19
    shouldDelete(false), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    20
    type(type), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
    21
    pivot(NULL)
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    22
{  
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    23
    if (enabled)
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    24
        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
    25
}
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
PhysicsObject::~PhysicsObject (void) {
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    28
//    Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    29
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    30
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    31
/**
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    32
 * Player walks on floor.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    33
 */
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    34
Vector PhysicsObject::walk_one_step (float partial, bool right) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    35
    // which way we are walking
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    36
    float deltaX = right ? partial : -partial;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    37
    Vector reached = this->position;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    38
    if(reached.roundToInt() == (reached+Vector(deltaX, 0)).roundToInt()) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    39
        return reached+Vector(deltaX, 0);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    40
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    41
    // Is there upward ramp
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    42
    if(!possibleLocation(position+Vector(deltaX, 0))) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    43
        // Yes. Then we check n pixels up
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    44
        for(int i = 1; i < 3; i++) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    45
            if(possibleLocation(position+Vector(deltaX, -i))) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    46
                // and when there is finally EMPTY, we can walk
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    47
                reached = position+Vector(deltaX, -i);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    48
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    49
            }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    50
        }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    51
    } else {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    52
        // Or downward ramp or flat
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    53
        for(int i = 0; 1; i++) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    54
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    55
            // And when there is finally ground we can step on
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    56
            // it. If there is no gound we still step there,
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    57
            // but will fall one pixel down
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    58
            if(possibleLocation(position+Vector(deltaX, i))) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    59
                reached = position+Vector(deltaX, i);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    60
            } else {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    61
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    62
            }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    63
            
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    64
            // If the fall is big enough, set the worm in the air
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    65
            if (i >= 2) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    66
//                Vector back = walk(dt, !right);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    67
                this->inAir = true;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    68
//                this->velocity.x = right ? velocity : -velocity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    69
                // Avoid stepping two pixels down when it starts to free fall
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    70
                reached.y -= 2;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    71
//                this->velocity = (reached-back)*1000/dt;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    72
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    73
            }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    74
        }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    75
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    76
    // And we return where we got
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    77
    return reached;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    78
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    79
void PhysicsObject::walk (TimeMS dt, bool right) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    80
    float velocity = PLAYER_WALK_SPEED;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    81
    float walkAmount = (velocity*dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    82
    Vector reached = this->position;
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 211
diff changeset
    83
    while (walkAmount > 0 && !this->inAir) {
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    84
        this->position = walk_one_step((1 < walkAmount ? 1 : walkAmount), right);
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) {
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   133
        Vector direction(pivot->getPosition()-position);
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   134
        float magnitude = pivot->getPivotForce(this);
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   135
        float length = direction.length();
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   136
        if (length > 0) {
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   137
            direction = direction / length * magnitude;
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   138
            applyForce(direction);
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   139
        }
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   140
    }
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   141
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   142
    std::pair<Force, TimeMS> force;
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   143
    std::queue<std::pair<Force, TimeMS> > newfq;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   144
    Force total;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   145
    while (!forceq.empty()) {
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   146
        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
   147
        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
   148
            force.second -= dt;
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   149
            newfq.push(force);
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   150
        }
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   151
        total += force.first;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   152
        forceq.pop();
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   153
    }
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   154
    forceq = newfq;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   155
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   156
    // 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
   157
    // set inAir false
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   158
    if (this->velocity == Vector(0,0)) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   159
        this->inAir = !world.collides(this->position+shape[1]+Vector(0, 1))
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   160
                      && !world.collides(this->position+shape[2]+Vector(0, 1))
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   161
                      && !world.collides(this->position+shape[3]+Vector(0, 1));
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   162
        // If, however, there's a force caused by a bomb, e.g., set it in air.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   163
        // Still, we have to be able to separate forces caused by walking attempts
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   164
        // 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
   165
        if (total.y < 0.01 || fabs(total.x) > PLAYER_MOVE_FORCE + 0.1)
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   166
            this->inAir = true;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   167
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   168
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   169
    if(!possibleLocation(position)) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   170
        //if we are trapped in ground form dirtball or something
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   171
        //we might want to just return and set velocity to some value
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   172
        //return;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   173
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   174
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   175
    // If the worm is not in the air make it walk,
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   176
    // otherwise integrate the new position and velocity
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   177
    if (!this->inAir) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   178
        // It walks only if there's some vertical force
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   179
        if (total.x != 0) {
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 200
diff changeset
   180
            walk(dt, total.x > 0);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   181
            this->velocity = Vector(0,0);
247
b87f68be579f When on the ground, dont integrate in vain
ekku
parents: 241
diff changeset
   182
        }   
b87f68be579f When on the ground, dont integrate in vain
ekku
parents: 241
diff changeset
   183
        // 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
   184
        // 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
   185
        return;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   186
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   187
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
   188
    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
   189
        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
   190
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   191
    Vector newPosition;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   192
    Vector velAfterTick;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   193
    // 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
   194
    integrate(total, dt, newPosition, velAfterTick);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   195
    this->velocity = velAfterTick;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   196
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   197
    // Collision detection
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   198
    bool collided = false;
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 247
diff changeset
   199
    Vector collisionPoint;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   200
   
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   201
    const Vector diffVec = newPosition - position;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   202
    const Vector unitVector = diffVec / diffVec.length();
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;
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   207
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   208
        // Check if any of the shapes points collide
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   209
        for (uint64_t i = 0; i < shape.size(); i++) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   210
            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
   211
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 247
diff changeset
   212
                collisionPoint = reached+shape[i];
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 247
diff changeset
   213
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   214
                if (inAir)
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   215
                    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
   216
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   217
                reached = reached - unitVector; // Return to last point
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   218
                collided = true;
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   219
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   220
                if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED)
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   221
                    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
   222
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   223
                break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   224
            }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   225
        }
279
e36f5e1a1c8d let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents: 275
diff changeset
   226
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   227
        if (collided)
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   228
            break;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   229
    }
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   230
   
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   231
    
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
   232
    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
   233
        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position << ", diffVec=" << diffVec;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   234
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   235
    // 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
   236
    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
   237
        if (!possibleLocation(newPosition)) {
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   238
            newPosition = reached;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   239
        } else {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   240
            // This means everything was ok, so no need to do anything
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   241
        }
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
   242
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
        this->position = newPosition;
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
   244
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   245
    } else {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   246
        newPosition = reached;
211
d5d52fb191e4 some minor fixes, shots now explode in collisionpoint
nireco
parents: 210
diff changeset
   247
        this->position = 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
   248
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
   249
        // the following may delete this object, so it must be the last thing called
272
97de051edbcf Added PhysicsObject* attribute to onCollision
saiam
parents: 268
diff changeset
   250
        onCollision(collisionPoint, NULL);
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
   251
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
   252
        return;
197
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
/**
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   257
 * Bounces from straight wall in any direction.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   258
 * Direction given as normal of that wall
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   259
 */
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   260
void PhysicsObject::bounce (Vector normal) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   261
    // normal.sqrLength can't be 0 when got from getNormal()
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   262
    if (normal.sqrLength() != 0) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   263
        Vector nvel = velocity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   264
        // We project the velocity on normal and remove twice that much from velocity
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   265
        nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   266
        velocity = nvel;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   267
        // We lose some of our speed on collision
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   268
        this->velocity *= this->collision_elasticity;
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
/**
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   273
 * Integrates given force over time and stores new position to
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   274
 * posAfterTick and new velocity to velAfterTick.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   275
 * @param force Force vector.
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   276
 * @param dt The time the force is applied (<=PHYSICS_TICK_MS)
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   277
 */
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   278
void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   279
    posAfterTick = position;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   280
    velAfterTick = velocity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   281
    Derivative tmpd;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   282
    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
   283
    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
   284
    Derivative k3 = evaluate(force, dt / 2, k2, posAfterTick, velAfterTick);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   285
    Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   286
    
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   287
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   288
    const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   289
    const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   290
    
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   291
    //    Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   292
    posAfterTick = posAfterTick + (dxdt * dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   293
    velAfterTick = velAfterTick + (dvdt * dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   294
    //Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   295
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   296
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   297
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   298
    Vector curPos = posAfterTick + (d.dx*dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   299
    Vector curVel = velAfterTick + (d.dv*dt)/1000;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   300
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   301
    Derivative out;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   302
    out.dx = curVel;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   303
    out.dv = acceleration(force);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   304
    //Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   305
    return out;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   306
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   307
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   308
Vector PhysicsObject::acceleration(const Force &force) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   309
    return (force/mass);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   310
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   311
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   312
void PhysicsObject::applyForce (Force force, TimeMS dt) {
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   313
    // Add applied force to the queue
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 272
diff changeset
   314
    forceq.push(std::make_pair(force, dt));
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   315
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   316
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   317
void PhysicsObject::changeAim(float da) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   318
    this->aim += da;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   319
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   320
    if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   321
    if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   322
    //Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   323
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   324
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   325
void PhysicsObject::setFacing (FacingDirection facing) {
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   326
    this->facing = facing;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   327
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   328
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   329
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir, FacingDirection facing, float aim) {
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   330
    this->position = position;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   331
    this->velocity = velocity;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   332
    this->inAir = inAir;
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   333
    this->facing = facing;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 197
diff changeset
   334
    this->aim = aim;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   335
}
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   336
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
   337
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
   338
    return position;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
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
    
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
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
   342
    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
   343
}
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
   344
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
   345
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
   346
    return velocity;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   347
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   348
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   349
FacingDirection PhysicsObject::getFacing (void) const {
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   350
    return facing;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   351
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
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
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
   354
    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
   355
}
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
   356
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::getDirection (void) const {
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   358
    return facing == FACING_RIGHT ? Vector(cos(aim), -sin(aim)) : Vector(-cos(aim), -sin(aim));
235
0a0c729365ee code cleanup
terom
parents: 229
diff changeset
   359
}
0a0c729365ee code cleanup
terom
parents: 229
diff changeset
   360
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
   361
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
   362
    return shape;
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   363
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   364
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   365
void PhysicsObject::setShape (std::vector<Vector> shape) {
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   366
    this->shape = shape;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   367
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   368
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   369
void PhysicsObject::setPivot (PhysicsObject *pivot) {
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   370
    this->pivot = pivot;
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   371
}
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   372
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 200
diff changeset
   373
void PhysicsObject::tick (TimeMS tick_length) {
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 200
diff changeset
   374
    this->updatePosition(tick_length);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   375
}
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
   376
    
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   377
void PhysicsObject::enable (void) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   378
    // only enable once until disabled
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   379
    if (alive)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   380
        return;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   381
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   382
    // mark as alive
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   383
    alive = true;
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   384
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   385
    // add the world objects list
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   386
    world.addPhysicsObject(this);
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   387
}
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   388
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   389
void PhysicsObject::disable (void) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   390
    // mark as disabled
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   391
    alive = false;
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   392
}
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   393
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
   394
void PhysicsObject::destroy (void) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   395
    // 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
   396
    alive = false;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   397
    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
   398
}
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
   399
    
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
   400
bool PhysicsObject::isDestroyed (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
   401
    return !alive;
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
   402
}
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
   403
    
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
   404
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
   405
    if (!alive) {
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   406
        if (shouldDelete)
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   407
            delete this;
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   408
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
   409
        return true;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   410
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
   411
    } 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
   412
        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
   413
    }
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
}
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   415
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   416
float PhysicsObject::getPivotForce (PhysicsObject *bob) { 
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   417
    (void) bob;
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   418
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 257
diff changeset
   419
    return 0.0; 
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   420
}
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
   421
268
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   422
bool PhysicsObject::collides (const PhysicsObject &obj) {
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   423
    const std::vector<Vector> oShape = obj.getShape();
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   424
    Vector p1, p2, p3;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   425
    int8_t sign, nsign;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   426
    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
   427
        p3 = *i + obj.getPosition();
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   428
        sign = 0;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   429
        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
   430
            p1 = *j + position;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   431
            if ( (j+1) == shape.end() ) p2 = *shape.begin() + position;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   432
            else p2 = *(j+1) + position;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   433
            nsign = crossProduct(p1, p2, p3);
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   434
            if ( sign == 0 ) sign = nsign;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   435
            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
   436
            else return false;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   437
        }
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   438
    }
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   439
    return true;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   440
}
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   441
    
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   442
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
   443
    (void) collisionPoint;
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   444
    (void) other;
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 279
diff changeset
   445
}
268
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   446
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   447
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
   448
    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
   449
    if (p < 0)
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   450
        return -1;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   451
    else
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   452
        return 1;
0b96b88af335 Added collision detection method to PhysicsObject. Not tested.
saiam
parents: 265
diff changeset
   453
}