jotain mik? hajottaa kaiken
authorekku
Thu, 20 Nov 2008 19:23:15 +0000
changeset 79 295ecb26d8ff
parent 78 bbc21da84813
child 80 c21641a487c5
jotain mik? hajottaa kaiken
src/proto2/GameState.hh
src/proto2/Physics.cc
src/proto2/Physics.hh
--- a/src/proto2/GameState.hh	Thu Nov 20 18:33:29 2008 +0000
+++ b/src/proto2/GameState.hh	Thu Nov 20 19:23:15 2008 +0000
@@ -7,14 +7,14 @@
 #include <list>
 #include <stdexcept>
 
-// in meters/kg
-const float MAP_WIDTH = 100.0;
-const float MAP_HEIGHT = 100.0;
-const float MAP_GRAVITY = 9.81;
+// in cells/kg
+const uint16_t MAP_WIDTH = 800;
+const uint16_t MAP_HEIGHT = 600;
+const float MAP_GRAVITY = 100.0;
 const float PLAYER_MASS = 10.0;
-const float PLAYER_MOVE_FORCE = 500.0;
-const float PLAYER_INITIAL_X = 50.0;
-const float PLAYER_INITIAL_Y = 40.0;
+const float PLAYER_MOVE_FORCE = 5000.0;
+const float PLAYER_INITIAL_X = 400.0;
+const float PLAYER_INITIAL_Y = 300.0;
 
 // forward-declare GameState
 class GameState;
--- a/src/proto2/Physics.cc	Thu Nov 20 18:33:29 2008 +0000
+++ b/src/proto2/Physics.cc	Thu Nov 20 19:23:15 2008 +0000
@@ -7,7 +7,7 @@
 #include <cmath>
 
 PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions)
-    : tick_timer(PHYSICS_TICK_MS), gravity(gravity), dimensions(dimensions), terrain(dimensions.x, std::vector<TerrainType>(dimensions.y, DIRT) {
+    : tick_timer(PHYSICS_TICK_MS), gravity(gravity), dimensions(dimensions), terrain(dimensions.x, std::vector<TerrainType>(dimensions.y, EMPTY)) {
 
     slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick);
     tick_timer.enable();
@@ -127,8 +127,8 @@
     int deltaX = oldPos.x - newPos.x; 
     int deltaY = oldPos.y - newPos.y; 
     double distance = sqrt(deltaX * deltaX + deltaY * deltaY);
-    double xInc = (double) deltaX / distance;
-    double yInc = (double) deltaY / distance;
+    double xInc = deltaX / distance;
+    double yInc = deltaY / distance;
     double currentX = oldPos.x;
     double currentY = oldPos.y;
 
@@ -137,16 +137,16 @@
         currentX += xInc;
         currentY += yInc;
         if(terrain[(int)currentX][(int)currentY] != EMPTY)
-            return false;
+            return true;
     }
-    return true;
+    return false;
 }
 
-void PhysicsObject::integrate(Vector force, Time dt) {
+void PhysicsObject::integrate(Vector force, TimeMS dt) {
     // TODO
 }
 
-void PhysicsObject::applyForce (Vector force, Time dt) {
+void PhysicsObject::applyForce (Vector force, TimeMS dt) {
     Vector oldVelocity = velocity;
 
     this->velocity += force * dt / 1000 / mass;  // The last factor denotes the time.
@@ -174,29 +174,37 @@
  * then randomizes circles of empty or rock
  * @param seed - seed number for random number generator
  */
-void generateTerrain(int seed) {
+void PhysicsWorld::generateTerrain(int seed) {
     // generating should use own random number generator, but didn't find easily how that is done
     srand(seed);
     
     // some constants to control random generation
     const int min_range = 10;
     const int max_range = 40;
-    const int num = 30;
+    const int num = 0;
     const int rock_rarity = 4; // 1 / rock_rarity will be rock circle
 
     // loops for amount of circles
     for(int i = 0; i < num; i++) {
         // information of new circle
-        int midx = rand()%dimensions.x;
-        int midy = rand()%dimensions.y;
+        int midx = rand()%(int)dimensions.x;
+        int midy = rand()%(int)dimensions.y;
+
+        // put first circle in the middle of the cave
+		// so that we have some area we can certainly spawn into 
+		if(i == 0) {
+			midx = dimensions.x / 2;
+			midy = dimensions.y / 2;
+		}
+
         int range = rand()%(max_range-min_range)+min_range;
         TerrainType type = EMPTY;
         if(rand()%rock_rarity == 0) {
             type = ROCK;
         }
         // loops for every pixel of circle
-        for(int x = max(0, midx-range); x < min(dimensions.x, midx+range); x++) {
-            for(int y = max(0, midy-range); y < min(dimensions.y, midy+range); y++) {
+        for(int x = std::max(0, midx-range); x < std::min((int)dimensions.x, midx+range); x++) {
+            for(int y = std::max(0, midy-range); y < std::min((int)dimensions.y, midy+range); y++) {
                 if(x*x+y*y < range*range) {
                     // and sets it to type
                     terrain[x][y] = type;
@@ -210,7 +218,7 @@
  * Returns terrainType in given tile. ROCK if tile is out of area
  * @param pos - coordinate of tile
  */
-TerrainType PhysicsWorld::getType(Vector pos) {
+TerrainType PhysicsWorld::getType(Vector pos) const {
     int x = (int)(pos.x);
     int y = (int)(pos.y);
     if(x < 0 || y < 0 || x >= dimensions.x || y >= dimensions.y) {
--- a/src/proto2/Physics.hh	Thu Nov 20 18:33:29 2008 +0000
+++ b/src/proto2/Physics.hh	Thu Nov 20 19:23:15 2008 +0000
@@ -6,9 +6,9 @@
 #include <vector>
 #include <ClanLib/core.h>
 
-typedef uint16_t Time;
+typedef uint16_t TimeMS;
 
-const Time PHYSICS_TICK_MS = 10;
+const TimeMS PHYSICS_TICK_MS = 10;
 
 enum TerrainType {EMPTY, DIRT, ROCK};
 
@@ -54,7 +54,7 @@
     
     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
     
-    virtual void applyForce (Vector force, Time dt);
+    virtual void applyForce (Vector force, TimeMS dt);
     void updatePhysics (Vector position, Vector velocity);
     
 private:
@@ -63,7 +63,7 @@
     /**
      * Use RK4 to integrate the effects of force over a time intervall.
      */
-    void integrate(Vector force, Time dt);
+    void integrate(Vector force, TimeMS dt);
     
 public:
     Vector getPosition (void);