Преглед на файлове

Fix player height (not working with scale)

Kenneth van Ewijk преди 9 години
родител
ревизия
1c59a3bc6f
променени са 8 файла, в които са добавени 44 реда и са изтрити 13 реда
  1. 1 1
      CrystalPoint.cpp
  2. 3 9
      HeightMap.cpp
  3. 20 0
      Vertex.cpp
  4. 5 0
      Vertex.h
  5. 3 3
      World.cpp
  6. 11 0
      worlds/small.json
  7. BIN
      worlds/small.png
  8. 1 0
      worlds/worlds.json

+ 1 - 1
CrystalPoint.cpp

@@ -91,7 +91,7 @@ void CrystalPoint::update()
 	if (!worldhandler->isPlayerPositionValid())
 		player->position = oldPosition;
 
-	player->position.y = worldhandler->getHeight(player->position.x, player->position.z);
+	player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
 
 	worldhandler->update(deltaTime);
 

+ 3 - 9
HeightMap.cpp

@@ -73,9 +73,6 @@ void HeightMap::Draw()
 	glEnable(GL_TEXTURE_2D);
 	glBindTexture(GL_TEXTURE_2D, imageIndex);
 
-	//glDisable(GL_TEXTURE_2D);
-
-
 	glEnableClientState(GL_VERTEX_ARRAY);
 	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 	//glEnableClientState(GL_COLOR_ARRAY);
@@ -105,15 +102,12 @@ float HeightMap::GetHeight(float x, float y)
 	Vertex& b = vertices[index+1];
 	Vertex& c = vertices[index+3];
 
-	float lowervalue = ((b.y - c.y)*(a.x - c.x) + (c.x - b.x)*(a.y - c.y));
-
-	float labda1 = ((b.y - c.y)*(x - c.x) + (c.x - b.x)*(y - c.y)) / lowervalue;
-
+	float lowervalue = ((b.z - c.z)*(a.x - c.x) + (c.x - b.x)*(a.z - c.z));
+	float labda1 = ((b.z - c.z)*(x - c.x) + (c.x - b.x)*(y - c.z)) / lowervalue;
 	float labda2 = ((c.y - a.y)*(x - c.x) + (a.x - c.x)*(y - c.y)) / lowervalue;
-
 	float labda3 = 1 - labda1 - labda2;
 
-	Vec3f z = Vec3f(a.x, a.y, a.z) * labda1 + Vec3f(b.x, b.y, b.z) * labda2 + Vec3f(c.x, c.y, c.z) * labda3;
+	Vertex z = a * labda1 + b * labda2 + c * labda3;
 
 	return z.y;
 }

+ 20 - 0
Vertex.cpp

@@ -17,3 +17,23 @@ Vertex::Vertex(float x, float y, float z, float nx, float ny, float nz, float tx
 Vertex::~Vertex()
 {
 }
+
+Vertex Vertex::operator/(float &other)
+{
+	return Vertex(x / other, y / other, z / other, normalX, normalY, normalZ, texX, texY);
+}
+
+Vertex Vertex::operator*(Vertex & other)
+{
+	return Vertex(x*other.x, y*other.y, z*other.z, normalX, normalY, normalZ, texX, texY);
+}
+
+Vertex Vertex::operator*(float & other)
+{
+	return Vertex(x*other, y*other, z*other, normalX, normalY, normalZ, texX, texY);
+}
+
+Vertex Vertex::operator+(Vertex & other)
+{
+	return Vertex(x+other.x, y+other.y, z+other.z, normalX, normalY, normalZ, texX, texY);
+}

+ 5 - 0
Vertex.h

@@ -15,5 +15,10 @@ public:
 
 	float texX;
 	float texY;
+
+	Vertex operator/(float &other);
+	Vertex operator*(Vertex &other);
+	Vertex operator*(float &other);
+	Vertex operator+(Vertex &other);
 };
 

+ 3 - 3
World.cpp

@@ -34,9 +34,9 @@ World::World(const std::string &fileName)
 	if(!v["world"]["texture"].isNull())
 		heightmap->SetTexture(v["world"]["texture"].asString());
 
-	player->position.x = v["player"]["startposition"][0].asFloat();
-	player->position.y = v["player"]["startposition"][1].asFloat();
-	player->position.z = v["player"]["startposition"][2].asFloat();
+	player->position.x = v["player"]["startposition"][0].asFloat()*scale;
+	player->position.y = v["player"]["startposition"][1].asFloat()*scale;
+	player->position.z = v["player"]["startposition"][2].asFloat()*scale;
 
 
 	for (auto object : v["objects"])

+ 11 - 0
worlds/small.json

@@ -0,0 +1,11 @@
+{
+   "world": {
+    "heightmap": "worlds/small.png",
+    "scale": 1
+   },
+  "player": {
+    "startposition": [ 20, 5, 20 ]
+  },
+  "objects": [],
+  "enemies": []
+}

BIN
worlds/small.png


+ 1 - 0
worlds/worlds.json

@@ -1,5 +1,6 @@
 {
 	"worlds": [
+			"worlds/small.json",
 			"worlds/ice.json",
 			"worlds/fire.json"
 	]