Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
authorsaiam
Sun, 30 Nov 2008 23:00:26 +0000
changeset 153 73402d5b778e
parent 152 89e2d078817c
child 154 78d144a48354
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
src/proto2/GameState.cc
src/proto2/GameState.hh
src/proto2/Graphics.cc
src/proto2/Physics.cc
src/proto2/Physics.hh
src/proto2/Terrain.cc
--- a/src/proto2/GameState.cc	Sun Nov 30 19:20:11 2008 +0000
+++ b/src/proto2/GameState.cc	Sun Nov 30 23:00:26 2008 +0000
@@ -38,7 +38,7 @@
         // But this now just segfaults
 //        world.addObject(new Shot(state, position, true));
 
-        world.removeGround(position, 10);
+        world.removeGround(position, 15);
     }
 }
 
--- a/src/proto2/GameState.hh	Sun Nov 30 19:20:11 2008 +0000
+++ b/src/proto2/GameState.hh	Sun Nov 30 23:00:26 2008 +0000
@@ -22,10 +22,10 @@
         PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
             
         std::vector<Vector> shape(4);
-        shape[0] = Vector(0,-18);
-        shape[1] = Vector(6,-9);
-        shape[2] = Vector(0,0); 
-        shape[3] = Vector(-6,-9);
+        shape[0] = Vector(0,-9);
+        shape[1] = Vector(6,0);
+        shape[2] = Vector(0,9); 
+        shape[3] = Vector(-6,0);
         // Initialize the shape of the player (salmiakki shape)
         setShape(shape);		
     }
--- a/src/proto2/Graphics.cc	Sun Nov 30 19:20:11 2008 +0000
+++ b/src/proto2/Graphics.cc	Sun Nov 30 23:00:26 2008 +0000
@@ -97,28 +97,8 @@
     // draw players
     for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
         Player *p = *it;
-        
-        Vector loc = p->getPosition();
-        bool right = p->getFacing();
-        float aim = p->getAim();
-        std::vector<Vector> &shape = p->getShape();
-        // draw quad
-        gc->fill_quad(
-                      CL_Quad(
-                              (loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY,
-                              (loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY,
-                              (loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY,
-                              (loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY
-                              ), CL_Color::green
-                      );
-
-        // Draw crosshair
-        if (right) {
-            gc->draw_line(loc.x, loc.y, loc.x + std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black);
-        } else {
-            gc->draw_line(loc.x, loc.y, loc.x - std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black);
-        }
-    }
+        p->draw(gc);
+    }        
 
     // flip window buffer, LIEK NAO
     win.flip(0);
--- 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
--- a/src/proto2/Physics.hh	Sun Nov 30 19:20:11 2008 +0000
+++ b/src/proto2/Physics.hh	Sun Nov 30 23:00:26 2008 +0000
@@ -22,16 +22,6 @@
 typedef uint16_t TimeMS;
 typedef Vector Force;
 
-// TODO: Random definitions. Should these be somewhere else?
-//enum TerrainType {EMPTY, DIRT, ROCK};
-
-// Yeah?!?!?! Atleast this could be documented. Contains vectors
-// presenting all the 8 directions in a square grid?
-//const Vector DIRECTIONS[] = { Vector(0,-1), Vector(1,-1), Vector(1,0), 
-//                            Vector(1,1), Vector(0,1), Vector(-1,1), 
-//                            Vector(-1,0), Vector(-1,-1) };
-
-
 /**
  * PhysicsWorld class. PhysicsWorld contains PhysicsObjects that are
  * simulated in the PhysicsWorld.
@@ -302,6 +292,13 @@
      * Update object in physics simulation.
      */
     void tick();
+
+    /**
+     * Draw object
+     *
+     * @param gc CL_GraphicContext
+     */
+    void draw(CL_GraphicContext *gc);
 };
 
 // TODO: This could probably be moved somewhere else or removed
--- a/src/proto2/Terrain.cc	Sun Nov 30 19:20:11 2008 +0000
+++ b/src/proto2/Terrain.cc	Sun Nov 30 23:00:26 2008 +0000
@@ -136,20 +136,24 @@
         }
     }
 
+   
     // Special cases
-    Vector tmp = direction(direction(prevPoint-point) + direction(normal));
-    if (normal.length() == 0 || getType(tmp.x, tmp.y) != EMPTY) 
+    /*    Vector tmp = direction(direction(prevPoint-point) + direction(normal));
+    Engine::log(DEBUG, "Terrain.getNormal") << "tmp: " << tmp;
+    if (normal.length() == 0 || (tmp.x != 0 && tmp.y != 0 && getType(tmp.x, tmp.y) != EMPTY)) 
         normal = prevPoint - point; // Direct hit
-
-    if (getType(tmp.x,tmp.y) != EMPTY) 
+    */
     Engine::log(DEBUG, "Terrain.getNormal") << "Normal: " << normal;
     return normal;
+    return Vector(0,-1);
 }
 
 Vector direction(const Vector &v) {
     Vector tmp(v);
-    tmp /= tmp.length();
-    return Vector(round(tmp.x), round(tmp.y));
+    if (tmp.length() > 0) tmp /= tmp.length();
+    tmp.x = round(tmp.x);
+    tmp.y = round(tmp.y);
+    return tmp;
 }
 
 // TODO: This could better :)