Jelajahi Sumber

Implemented getHeight

Kenneth van Ewijk 9 tahun lalu
induk
melakukan
4c613b606a
6 mengubah file dengan 20 tambahan dan 5 penghapusan
  1. 0 1
      Entity.cpp
  2. 12 2
      HeightMap.cpp
  3. 1 1
      HeightMap.h
  4. 5 0
      Vector.cpp
  5. 1 0
      Vector.h
  6. 1 1
      World.cpp

+ 0 - 1
Entity.cpp

@@ -21,7 +21,6 @@ Entity::~Entity()
 
 void Entity::draw()
 {
-	rotation.y += 1;
 	if (model)
 	{
 		glPushMatrix();

+ 12 - 2
HeightMap.cpp

@@ -90,7 +90,7 @@ void HeightMap::Draw()
 	glDisableClientState(GL_NORMAL_ARRAY);
 }
 
-void HeightMap::GetHeigth(float x, float y)
+float HeightMap::GetHeigth(float x, float y)
 {
 	x /= scale;
 	y /= scale;
@@ -102,8 +102,18 @@ void HeightMap::GetHeigth(float x, float y)
 	Vertex& a = vertices[index];
 	Vertex& b = vertices[index+1];
 	Vertex& c = vertices[index+3];
-	//http://stackoverflow.com/questions/36090269/finding-height-of-point-on-height-map-triangles
 
+	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 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;
+	
+	return z.y;
 }
 
 void HeightMap::SetTexture(const std::string &file)

+ 1 - 1
HeightMap.h

@@ -18,7 +18,7 @@ public:
 	~HeightMap();
 
 	void Draw();
-	void GetHeigth(float x, float z);
+	float GetHeigth(float x, float y);
 	void SetTexture(const std::string &file);
 
 	std::vector<Vertex> vertices;

+ 5 - 0
Vector.cpp

@@ -80,6 +80,11 @@ bool Vec3f::operator!=(const Vec3f & other)
 	return x != other.x & y != other.y & z != other.z;
 }
 
+Vec3f Vec3f::operator*(const float & other)
+{
+	return Vec3f(x*other, y*other, z*other);
+}
+
 Vec3f Vec3f::cross(const Vec3f & other)
 {
 	return Vec3f(

+ 1 - 0
Vector.h

@@ -23,6 +23,7 @@ public:
 	Vec3f operator / (float value);
 	bool operator ==(const Vec3f &other);
 	bool operator !=(const Vec3f &other);
+	Vec3f operator *(const float &other);
 
 	Vec3f cross(const Vec3f &other);
 

+ 1 - 1
World.cpp

@@ -133,7 +133,7 @@ void World::update(float elapsedTime)
 				{
 					Vec3f difference = e->position - enemy->position; //zou misschien omgedraait moeten worden
 					difference.Normalize();
-					//difference = difference * (e->model->radius + 0.01f);
+					difference = difference * (e->model->radius + 0.01f);
 					enemy->position = e->position + difference;
 					break;
 				}