src/proto2/Physics.cc
changeset 153 73402d5b778e
parent 151 9fc900fbfa79
child 154 78d144a48354
--- a/src/proto2/Physics.cc	Sun Nov 30 19:20:11 2008 +0000
+++ b/src/proto2/Physics.cc	Sun Nov 30 23:00:26 2008 +0000
@@ -12,9 +12,7 @@
 PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions)
     : tick_timer(PHYSICS_TICK_MS), tick_counter(0), dimensions(dimensions),
       gravity(gravity) {
-    Engine::log(DEBUG, "Yeah");
     terrain = Terrain(1337);
-    Engine::log(DEBUG, "Yeah2");
     slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick);
     tick_timer.enable();
 }
@@ -46,7 +44,8 @@
 }
 
 Vector PhysicsObject::walk (bool right) {
-    Vector cursor = right ? this->position + Vector(1,0) : this->position + Vector(-1,0);
+    Vector cursor = right ? this->position + Vector(1,0) : 
+        this->position + Vector(-1,0);
     Vector reached = this->position;
     
     //for(int steps = 0; steps < 3; steps++) {
@@ -169,7 +168,8 @@
         // Check if any of the shapes points collide
         for (uint64_t i = 0; i < shape.size(); i ++) {
             if (world.getType(reached+shape[i]) != EMPTY) {  // Collision
-                this->bounce(world.getNormal(reached+shape[i], reached-unitVector+shape[i]));
+                this->bounce(world.getNormal(reached+shape[i], 
+                                             reached-unitVector+shape[i]));
                 reached = reached - unitVector; // Return to last point
                 collided = true;
                 this->velocity *= COLLISION_ELASTICITY;
@@ -177,6 +177,7 @@
                     this->inAir = false;
                     this->velocity = Vector(0,0);
                 }
+                
                 break;
             }
         }
@@ -282,14 +283,15 @@
  * Bounces from straight wall in any direction.
  * Direction given as normal of that wall
  */
-// TODO Bounce doesnt work kun oikealle tai vasemmalle alaviistoon mennään suoralla (tangentaalinen arvo väärän suuntainen)
 void PhysicsObject::bounce (Vector normal) {
     if (normal.length() != 0) {
-        Vector tangent(normal.y, -normal.x);
-        Vector tprojection = tangent*(velocity * tangent) / (tangent.length()*tangent.length());
-        Vector nprojection = normal*(velocity * normal) / (normal.length()*normal.length());
-        velocity = tprojection - nprojection;
-    }
+        //        Engine::log(DEBUG, "PhysicsObject.bounce") << "Velocity: " << velocity;
+        Vector nvel = velocity;
+        //        Engine::log(DEBUG, "PhysicsObject.bounce") << "New Velocity: " << nvel;
+        nvel = nvel - (2*((nvel*normal)/(normal*normal))*normal);
+        //        Engine::log(DEBUG, "PhysicsObject.bounce") << "Projection: " << nvel;
+        velocity = nvel;
+       }
 }
 
 /**
@@ -378,6 +380,32 @@
     this->updatePosition();
 }
 
+void PhysicsObject::draw(CL_GraphicContext *gc) {
+    CL_Quad player(
+                   (position+shape[0]).x, (position+shape[0]).y,
+                   (position+shape[1]).x, (position+shape[1]).y,
+                   (position+shape[2]).x, (position+shape[2]).y,
+                   (position+shape[3]).x, (position+shape[3]).y
+                   );
+    
+    gc->fill_quad(player, CL_Color::green);
+    
+    const uint16_t chlen = 10;
+    uint16_t x = player.center().x;
+    uint16_t y = player.center().y;
+    if (facingRight) {
+        gc->draw_line(x, y,
+                      x + std::cos(aim)*chlen,
+                      y - std::sin(aim)*chlen,
+                      CL_Color::black);
+    } else {
+        gc->draw_line(x, y,
+                      x - std::cos(aim)*chlen,
+                      y - std::sin(aim)*chlen,
+                      CL_Color::black);
+    }
+}
+
 /**
  * Returns terrainType in given tile. ROCK if tile is out of area
  * @param pos - coordinate of tile