src/PhysicsObject.cc
changeset 370 39e59dd36b6e
parent 336 ef598a3dc1b7
child 408 e6cfc44266af
equal deleted inserted replaced
369:9b682981e060 370:39e59dd36b6e
    33  * Player walks on floor.
    33  * Player walks on floor.
    34  */
    34  */
    35 Vector PhysicsObject::walk_one_step (float partial, bool right) {
    35 Vector PhysicsObject::walk_one_step (float partial, bool right) {
    36     // which way we are walking
    36     // which way we are walking
    37     float deltaX = right ? partial : -partial;
    37     float deltaX = right ? partial : -partial;
       
    38 
    38     Vector reached = this->position;
    39     Vector reached = this->position;
    39     if(reached.roundToInt() == (reached+Vector(deltaX, 0)).roundToInt()) {
    40 
    40         return reached+Vector(deltaX, 0);
    41     if (reached.roundToInt() == (reached + Vector(deltaX, 0)).roundToInt()) {
    41     }
    42         return reached + Vector(deltaX, 0);
       
    43     }
       
    44 
    42     // Is there upward ramp
    45     // Is there upward ramp
    43     if(!possibleLocation(position+Vector(deltaX, 0))) {
    46     if(!possibleLocation(position+Vector(deltaX, 0))) {
    44         // Yes. Then we check n pixels up
    47         // Yes. Then we check n pixels up
    45         for(int i = 1; i < 3; i++) {
    48         for(int i = 1; i < 3; i++) {
    46             if(possibleLocation(position+Vector(deltaX, -i))) {
    49             if(possibleLocation(position+Vector(deltaX, -i))) {
   209             if (world.collides(reached+shape[i])) {  // Collision
   212             if (world.collides(reached+shape[i])) {  // Collision
   210 
   213 
   211                 collisionPoint = reached+shape[i];
   214                 collisionPoint = reached+shape[i];
   212 
   215 
   213                 if (inAir)
   216                 if (inAir)
   214                     this->bounce(world.getNormal(reached+shape[i], reached-unitVector+shape[i]));
   217                     this->bounce(world.getNormal(reached + shape[i], reached - unitVector + shape[i]));
   215 
   218 
   216                 reached = reached - unitVector; // Return to last point
   219                 reached = reached - unitVector; // Return to last point
   217                 collided = true;
   220                 collided = true;
   218 
   221                 
       
   222                 // snap velocity to zero once it's below a threshold
   219                 if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED)
   223                 if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED)
   220                     this->velocity = Vector(0,0);
   224                     this->velocity = Vector(0, 0);
   221 
   225 
   222                 break;
   226                 break;
   223             }
   227             }
   224         }
   228         }
   225 
   229