浏览代码

enemys always looking at you when you are the target

Remco 9 年之前
父节点
当前提交
d739141b25
共有 4 个文件被更改,包括 23 次插入13 次删除
  1. 3 7
      Enemy.cpp
  2. 17 0
      Vector.cpp
  3. 2 0
      Vector.h
  4. 1 6
      worlds/world1.json

+ 3 - 7
Enemy.cpp

@@ -67,13 +67,9 @@ void Enemy::update(float delta)
 		dz *= speed*delta;
 
 		position.x += dx;
-		position.z += dz;		
-	}	
+		position.z += dz;	
 
-	/*rotation.y += 10.0f;
-	if (rotation.y >= 360.0f)
-		rotation.y = 0.0f;*/
-	//std::cout << atan2f(target.z - position.z, target.x - position.x) * 180 / M_PI << std::endl;
-	//rotation.y = atan2f(target.z - position.z, target.x - position.x) * 180 / M_PI ;
+		rotation.y = atan2f(target.x - position.x, target.z - position.z) * 180 / M_PI;
+	}	
 
 }

+ 17 - 0
Vector.cpp

@@ -14,6 +14,18 @@ float Vec3f::Length()
 	return (float)sqrt(x*x+y*y+z*z);
 }
 
+void Vec3f::Normalize()
+{	
+	float length = this->Length();
+
+	if (length != 0)
+	{
+		x = x / length;
+		y = y / length;
+		z = z / length;
+	}
+}
+
 float Vec3f::Distance(const Vec3f &other)
 {
 	return (float)sqrt(pow(other.x - x, 2)+pow(other.y - y, 2)+pow(other.z - z, 2));
@@ -42,6 +54,11 @@ Vec3f Vec3f::operator+(const Vec3f & other)
 	return Vec3f(x + other.x, y + other.y, z + other.z);
 }
 
+Vec3f Vec3f::operator-(const Vec3f & other)
+{
+	return Vec3f(x - other.x, y - other.y, z - other.z);
+}
+
 Vec3f Vec3f::operator/(float value)
 {
 	return Vec3f(x / value, y / value, z / value);

+ 2 - 0
Vector.h

@@ -15,9 +15,11 @@ public:
 	Vec3f(const Vec3f &other);
 	Vec3f(float x, float y, float z);
 	float Length();
+	void Normalize();
 	float Distance(const Vec3f &);
 	float& operator [](int);
 	Vec3f operator + (const Vec3f &other);
+	Vec3f operator -(const Vec3f &other);
 	Vec3f operator / (float value);
 	bool operator ==(const Vec3f &other);
 	bool operator !=(const Vec3f &other);

+ 1 - 6
worlds/world1.json

@@ -24,11 +24,6 @@
       "id": 0,
       "pos": [ 1, 2, -10 ],
       "scale": 0.01
-    },
-    {
-      "id": 0,
-      "pos": [ 4, 2, -15 ],
-      "scale": 0.01
-    }
+    }   
   ]
 }