V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
authorsaiam
Fri, 28 Nov 2008 12:26:39 +0000
changeset 114 71f7e9d3d052
parent 113 1b8ade19eedd
child 115 237ea0bb125a
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
src/proto2/GameState.cc
src/proto2/Physics.cc
--- a/src/proto2/GameState.cc	Fri Nov 28 11:50:17 2008 +0000
+++ b/src/proto2/GameState.cc	Fri Nov 28 12:26:39 2008 +0000
@@ -19,27 +19,18 @@
     if (input & INPUT_MOVE_DOWN)
         da -= CROSSHAIR_ANGLE_SPEED;
 
+    if (input & INPUT_MOVE_JUMP)
+        jump();
+
+    this->changeAim(da); // Move crosshair
+
     // Player facing
     if (fx < 0) setFacing(false);
     else if (fx > 0) setFacing(true);
 
-    this->changeAim(da); // Move crosshair
-
-        // we behave differently depending on if we're in the air or on the ground
-        if (inAir) {
-            // apply horizontal force
-            if (fx)
-                applyForce(Vector(fx, 0), INPUT_INTERVAL_MS);
+    // Apply force
+    applyForce(Vector(fx, 0), INPUT_INTERVAL_MS);
 
-        } else {
-            // walk right
-            if (fx)
-                this->position = walk(fx > 0);
-        
-            // jump?
-            if (input & INPUT_MOVE_JUMP)
-                jump();    
-        }
 }
 
 void Player::debugInfo (void) {
--- a/src/proto2/Physics.cc	Fri Nov 28 11:50:17 2008 +0000
+++ b/src/proto2/Physics.cc	Fri Nov 28 12:26:39 2008 +0000
@@ -40,13 +40,6 @@
     world.addObject(this);
 }
 
-/**
- * TODO This method doesnt quite work as it should but 
- * it makes the worm move pretty smoothly.
- *
- * Make the worm walk on the ground.
- * @return Final position.
- */
 Vector PhysicsObject::walk (bool right) {
     Vector cursor = right ? this->position + Vector(1,0) : this->position + Vector(-1,0);
     Vector reached = this->position;
@@ -77,6 +70,7 @@
                 reached = cursor;
             }
             // Start checking if the lower squares are empty
+
             for(int depth = 0, max = 3; depth < max+42; depth++) {
                 
                 if(depth >= max) {
@@ -110,8 +104,11 @@
 }
 
 void PhysicsObject::jump () {
-     velocity.y = -100;
-    inAir = true;
+    // Jump only if player is "on the ground"
+    if (!this->inAir) {
+ 	velocity.y = -100;
+	inAir = true;
+    }
 }
 
 bool PhysicsObject::possibleLocation (Vector loc) {
@@ -128,42 +125,36 @@
  */   
 void PhysicsObject::updatePosition () {
 
-    if(!this->inAir) {
-        /*if(!forceq.empty()) {
-          if(forceq.front().y == 0) {
-          this->position = moveVertically(forceq.front().x > 0);
-          forceq.pop();
-          }
-          } */
-        return;
-    }
-    // TODO HERE
-
     // Add gravity to the force queue
     forceq.push(world.gravity);
     
     // Go trough every force in the queue
-    // TODO: It might be possible to optimize by adding forces together
     Force total;
     posAfterTick = position;
     velAfterTick = velocity;
     while (!forceq.empty()) {
+        Engine::log(DEBUG, "PhysicObject.updatePosition") << "Forceq length: " << forceq.front(); 
         total += forceq.front();
         forceq.pop();
         //        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Current position: " << posAfterTick;
     }
+
+    // TODO: This is _ugly_ (but not so ugly as the last one I think)
+    // hack. I think we should handle walking from the collision
+    // detection code.
+
+    if (!this->inAir) {
+        if (total.x != 0)
+            this->position = walk(total.x > 0);
+        return; // argh
+    }
+
     integrate(total, PHYSICS_TICK_MS);
 
     Vector newPosition = posAfterTick /*+ (velAfterTick * PHYSICS_TICK_MS)/1000*/;
     this->velocity = velAfterTick;
-    //Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Nopeus: "<<this->velocity;
-    /*
-    this->velocity += world.gravity * (PHYSICS_TICK_MS / 1000.0);
 
-    Vector newPosition = position + velocity * (PHYSICS_TICK_MS / 1000.0);
-    */
-
-    //TODO Handle the object as a square or a polygon
+    // Collision detection
 
     bool collided = false;
 
@@ -172,6 +163,7 @@
     
     Vector tmpVector = position;
     Vector reached = position;
+
     int steps = (int) (newPosition-position).length() + 2;
         
     //Engine::log(DEBUG, "physics.update_position") << unitVector-newPosition;
@@ -198,88 +190,12 @@
         }
         if(collided)
             break;
-        /*
-        if(velocity.y > 0) {
-            if(world.getType(tmpVector+shape[2]) != EMPTY) {
-                reached = position + unitVector*(i-1);
-                collided = true;
-                this->velocity.y *= -0.3;
-                this->velocity.x *= 0.7;
-                if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
-                    this->inAir = false;
-                    this->velocity = Vector(0,0);
-                }
-                break;
-            }
-        } else {
-            if(world.getType(tmpVector+shape[0]) != EMPTY) {
-                reached = position + unitVector*(i-1);
-                collided = true;
-                this->velocity.y *= -0.3;
-                this->velocity.x *= 0.7;
-                if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
-                    this->inAir = false;
-                    this->velocity = Vector(0,0);
-                }
-                break;
-            }
-        }
-
-        if(velocity.x > 0) {
-            if(world.getType(tmpVector+shape[1]) != EMPTY) {
-                reached = position + unitVector*(i-1);
-                collided = true;
-                this->velocity.x *= -0.6;
-                this->velocity.y *= 0.7;
-                if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
-                    this->inAir = false;
-                    this->velocity = Vector(0,0);
-                }
-                break;
-            }
-        } else {
-            if(world.getType(tmpVector+shape[3]) != EMPTY) {
-                reached = position + unitVector*(i-1);
-                collided = true;
-                this->velocity.x *= -0.6;
-                this->velocity.y *= 0.7;
-                if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
-                    this->inAir = false;
-                    this->velocity = Vector(0,0);
-                }
-                break;
-            }
-        }*/
-        
-        // This col. det. doesn't let worms inside the ground, but on the other hand the worms get often stuck
-        /*if(world.getType(tmpVector+shape[0]) != EMPTY || (world.getType(tmpVector+shape[2]) != EMPTY)) {
-            reached = position + unitVector*(i-1);
-            collided = true;
-            this->velocity.y *= -0.3;
-            if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
-                this->inAir = false;
-                this->velocity = Vector(0,0);
-            }
-            break;
-        }
-        if(world.getType(tmpVector+shape[1]) != EMPTY || (world.getType(tmpVector+shape[3]) != EMPTY)) {
-            reached = position + unitVector*(i-1);
-            collided = true;
-            this->velocity.x *= -0.6;
-            if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
-                this->inAir = false;
-                this->velocity = Vector(0,0);
-            }
-            break;
-        }*/
-        
-            //Engine::log(DEBUG, "physics.update_position") << "didnt hit";
     }
-
+    
     // In case of some float error check the final coordinate
     if(!collided) {
         if(world.getType(newPosition+shape[0]) != EMPTY || (world.getType(newPosition+shape[1]) != EMPTY) 
-            || (world.getType(newPosition+shape[2]) != EMPTY) || (world.getType(newPosition+shape[3]) != EMPTY)) {
+           || (world.getType(newPosition+shape[2]) != EMPTY) || (world.getType(newPosition+shape[3]) != EMPTY)) {
             
             Engine::log(DEBUG, "physics.update_position") << "didnt hit";
             // There was error, and there is ground
@@ -293,7 +209,7 @@
         //TODO: it shouldn't just stop on collision
     }
     this->position = newPosition;
-
+    
 }
 
 /**
@@ -420,7 +336,6 @@
     
     // Add applied force to the queue
     forceq.push(force);
-    this->inAir = true;
 }
 
 void PhysicsObject::changeAim(float da) {