and once again fixed getNormal, also added vector version of getType
authornireco
Mon, 01 Dec 2008 12:14:39 +0000
changeset 157 8afdd8c449f9
parent 156 d3e6625cc695
child 158 0215ace86018
and once again fixed getNormal, also added vector version of getType
src/proto2/Physics.cc
src/proto2/Terrain.cc
src/proto2/Terrain.hh
--- a/src/proto2/Physics.cc	Mon Dec 01 11:50:29 2008 +0000
+++ b/src/proto2/Physics.cc	Mon Dec 01 12:14:39 2008 +0000
@@ -252,7 +252,9 @@
 /**
  * Gets the index of the given coordinate direction
  * referring to the DIRECTIONS table in Physics.hh
- */
+ *
+ * moved to Terrain.cc
+ *//*
 int getDirectionIndex (Vector dir) {
     if(dir.x == 0 && dir.y == -1) {
         return 0;
@@ -273,7 +275,7 @@
     }
     Engine::log(DEBUG, "physics.getDirectionIndex ") << "invalid direction: " << dir;
     return 0;
-}
+}*/
 
 /**
  * Computes hitten wall's normal. Calculated from 3*3 grid
--- a/src/proto2/Terrain.cc	Mon Dec 01 11:50:29 2008 +0000
+++ b/src/proto2/Terrain.cc	Mon Dec 01 12:14:39 2008 +0000
@@ -56,6 +56,9 @@
     }
     return terrain[x][y];
 }
+TerrainType Terrain::getType(Vector point) const {
+    return getType((int32_t)point.x, (int32_t)point.y);
+}
 
 bool Terrain::collides(const Vector &point) const {
     Vector coor = getPixelLocation(point);
@@ -126,15 +129,62 @@
     }
 }
 
+/**
+ * Gets the index of the given coordinate direction
+ * referring to the DIRECTIONS table in Physics.hh
+ */
+int getDirectionIndex (Vector dir) {
+    if(dir.x == 0 && dir.y == -1) {
+        return 0;
+    } else if(dir.x == 1 && dir.y == -1) {
+        return 1;
+    } else if(dir.x == 1 && dir.y == 0) {
+        return 2;
+    } else if(dir.x == 1 && dir.y == 1) {
+        return 3;
+    } else if(dir.x == 0 && dir.y == 1) {
+        return 4;
+    } else if(dir.x == -1 && dir.y == 1) {
+        return 5;
+    } else if(dir.x == -1 && dir.y == 0) {
+        return 6;
+    } else if(dir.x == -1 && dir.y == -1) {
+        return 7;
+    }
+    Engine::log(DEBUG, "physics.getDirectionIndex ") << "invalid direction: " << dir;
+    return 0;
+}
+
+/**
+ * point should be ground and prevPoint air, but it's easy to assure that
+ * @param point - pixel on ground to which was collided
+ * @param prevPoint - pixel where we are when we collide
+ */
 Vector Terrain::getNormal(Vector point, Vector prevPoint) const {
     Vector p = getPixelLocation(point);
 
     Vector normal(0,0);
-    for (int i = 0; i < 8; i++) {
-        if (getType(p.x+DIRECTIONS[i].x, p.y+DIRECTIONS[i].y) == EMPTY) {
-            normal += DIRECTIONS[i];
+
+    int dirIdx = getDirectionIndex(prevPoint.roundToInt() - point.roundToInt());
+
+    normal += DIRECTIONS[dirIdx];
+    for(int i = 1; i <= 2; i++) {
+        if(getType(point + DIRECTIONS[(dirIdx+i+8)%8]) == EMPTY) {
+            normal += DIRECTIONS[(dirIdx+i+8)%8];
         }
     }
+    for(int i = 1; i <= 2; i++) {
+        if(getType(point + DIRECTIONS[(dirIdx-i+8)%8]) == EMPTY) {
+            normal += DIRECTIONS[(dirIdx-i+8)%8];
+        }
+    }
+    
+
+//    for (int i = 0; i < 8; i++) {
+//        if (getType(p.x+DIRECTIONS[i].x, p.y+DIRECTIONS[i].y) == EMPTY) {
+//            normal += DIRECTIONS[i];
+//        }
+//    }
 
    
     // Special cases
--- a/src/proto2/Terrain.hh	Mon Dec 01 11:50:29 2008 +0000
+++ b/src/proto2/Terrain.hh	Mon Dec 01 12:14:39 2008 +0000
@@ -70,6 +70,8 @@
      * @return Terrain type
      */
     TerrainType getType(int32_t x, int32_t y) const;
+    //point is just rounded and redirected to that above
+    TerrainType getType(Vector point) const;
 
     /**
      * Constructor.