Bläddra i källkod

Added separate Equals() function to Vector & Quaternion classes to perform comparison with epsilon. Equality operator does not use epsilon. This optimizes network sync somewhat.

Lasse Öörni 13 år sedan
förälder
incheckning
66a860144a

+ 4 - 0
Docs/ScriptAPI.dox

@@ -310,6 +310,7 @@ Methods:<br>
 - float DotProduct(const Vector2&) const
 - float AbsDotProduct(const Vector2&) const
 - Vector2 Lerp(const Vector2&, float) const
+- bool Equals(const Vector2&) const
 - Vector2 Normalized() const
 - String ToString() const
 
@@ -328,6 +329,7 @@ Methods:<br>
 - float AbsDotProduct(const Vector3&) const
 - Vector3 CrossProduct(const Vector3&) const
 - Vector3 Lerp(const Vector3&, float) const
+- bool Equals(const Vector3&) const
 - Vector3 Normalized() const
 - String ToString() const
 
@@ -345,6 +347,7 @@ Methods:<br>
 - float DotProduct(const Vector4&) const
 - float AbsDotProduct(const Vector4&) const
 - Vector4 Lerp(const Vector4&, float) const
+- bool Equals(const Vector4&) const
 - String ToString() const
 
 Properties:<br>
@@ -366,6 +369,7 @@ Methods:<br>
 - Quaternion Inverse() const
 - float DotProduct(const Quaternion&) const
 - Quaternion Slerp(const Quaternion&, float) const
+- bool Equals(const Quaternion&) const
 - String ToString() const
 
 Properties:<br>

+ 1 - 4
Engine/Core/Variant.cpp

@@ -101,9 +101,6 @@ bool Variant::operator == (const Variant& rhs) const
     
     switch (type_)
     {
-    case VAR_NONE:
-        return true;
-        
     case VAR_INT:
         return value_.int_ == rhs.value_.int_;
         
@@ -148,7 +145,7 @@ bool Variant::operator == (const Variant& rhs) const
         
     case VAR_VARIANTMAP:
         return *(reinterpret_cast<const VariantMap*>(&value_)) == *(reinterpret_cast<const VariantMap*>(&rhs.value_));
-
+        
     default:
         return true;
     }

+ 4 - 0
Engine/Engine/MathAPI.cpp

@@ -205,6 +205,7 @@ static void RegisterVector2(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Vector2", "float DotProduct(const Vector2&in) const", asMETHOD(Vector2, DotProduct), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector2", "float AbsDotProduct(const Vector2&in) const", asMETHOD(Vector2, AbsDotProduct), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector2", "Vector2 Lerp(const Vector2&in, float) const", asMETHOD(Vector2, Lerp), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Vector2", "bool Equals(const Vector2&in) const", asMETHOD(Vector2, Equals), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector2", "Vector2 Normalized() const", asMETHOD(Vector2, Normalized), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector2", "String ToString() const", asMETHOD(Vector2, ToString), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector2", "float get_length() const", asMETHOD(Vector2, Length), asCALL_THISCALL);
@@ -255,6 +256,7 @@ static void RegisterVector3(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Vector3", "float AbsDotProduct(const Vector3&in) const", asMETHOD(Vector3, AbsDotProduct), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "Vector3 CrossProduct(const Vector3&in) const", asMETHOD(Vector3, CrossProduct), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "Vector3 Lerp(const Vector3&in, float) const", asMETHOD(Vector3, Lerp), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Vector3", "bool Equals(const Vector3&in) const", asMETHOD(Vector3, Equals), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "Vector3 Normalized() const", asMETHOD(Vector3, Normalized), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "String ToString() const", asMETHOD(Vector3, ToString), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "float get_length() const", asMETHOD(Vector3, Length), asCALL_THISCALL);
@@ -310,6 +312,7 @@ static void RegisterVector4(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Vector4", "float DotProduct(const Vector4&in) const", asMETHOD(Vector4, DotProduct), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector4", "float AbsDotProduct(const Vector4&in) const", asMETHOD(Vector4, AbsDotProduct), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector4", "Vector4 Lerp(const Vector4&in, float) const", asMETHOD(Vector4, Lerp), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Vector4", "bool Equals(const Vector4&in) const", asMETHOD(Vector4, Equals), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector4", "String ToString() const", asMETHOD(Vector4, ToString), asCALL_THISCALL);
     engine->RegisterObjectProperty("Vector4", "float x", offsetof(Vector4, x_));
     engine->RegisterObjectProperty("Vector4", "float y", offsetof(Vector4, y_));
@@ -386,6 +389,7 @@ static void RegisterQuaternion(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Quaternion", "Quaternion Inverse() const", asMETHOD(Quaternion, Inverse), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "float DotProduct(const Quaternion&in) const", asMETHOD(Quaternion, DotProduct), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Slerp(const Quaternion&in, float) const", asMETHOD(Quaternion, Slerp), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Quaternion", "bool Equals(const Quaternion&in) const", asMETHOD(Quaternion, Equals), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "String ToString() const", asMETHOD(Quaternion, ToString), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Vector3 get_eulerAngles() const", asMETHOD(Quaternion, EulerAngles), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "float get_yaw() const", asMETHOD(Quaternion, YawAngle), asCALL_THISCALL);

+ 1 - 1
Engine/Math/Polyhedron.cpp

@@ -158,7 +158,7 @@ void Polyhedron::Clip(const Plane& plane)
     {
         for (unsigned j = clippedVertices_.Size() - 1; j > i; --j)
         {
-            if (clippedVertices_[j] == clippedVertices_[i])
+            if (clippedVertices_[j].Equals(clippedVertices_[i]))
                 clippedVertices_.Erase(j);
         }
     }

+ 6 - 4
Engine/Math/Quaternion.h

@@ -115,10 +115,10 @@ public:
         return *this;
     }
     
-    /// Test for equality with another quaternion.
-    bool operator == (const Quaternion& rhs) const { return Equals(w_, rhs.w_) && Equals(x_, rhs.x_) && Equals(y_, rhs.y_) && Equals(z_, rhs.z_); }
-    /// Test for inequality with another quaternion.
-    bool operator != (const Quaternion& rhs) const { return !Equals(w_, rhs.w_) || !Equals(x_, rhs.x_) || !Equals(y_, rhs.y_) || !Equals(z_, rhs.z_); }
+    /// Test for equality with another quaternion without epsilon.
+    bool operator == (const Quaternion& rhs) const { return w_ == rhs.w_ && x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
+    /// Test for inequality with another quaternion without epsilon.
+    bool operator != (const Quaternion& rhs) const { return w_ != rhs.w_ || x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
     /// Multiply with a scalar.
     Quaternion operator * (float rhs) const { return Quaternion(w_ * rhs, x_ * rhs, y_ * rhs, z_ * rhs); }
     /// Return negation.
@@ -208,6 +208,8 @@ public:
     float LengthSquared() const { return w_ * w_ + x_ * x_ + y_ * y_ + z_ * z_; }
     /// Calculate dot product.
     float DotProduct(const Quaternion& rhs) const { return w_ * rhs.w_ + x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
+    /// Test for equality with another quaternion with epsilon.
+    bool Equals(const Quaternion& rhs) const { return ::Equals(w_, rhs.w_) && ::Equals(x_, rhs.x_) && ::Equals(y_, rhs.y_) && ::Equals(z_, rhs.z_); }
     
     /// Return Euler angles in degrees.
     Vector3 EulerAngles() const;

+ 6 - 4
Engine/Math/Vector2.h

@@ -64,10 +64,10 @@ public:
         return *this;
     }
     
-    /// Test for equality with another vector.
-    bool operator == (const Vector2& rhs) const { return Equals(x_, rhs.x_) && Equals(y_, rhs.y_); }
-    /// Test for inequality with another vector.
-    bool operator != (const Vector2& rhs) const { return !Equals(x_, rhs.x_) || !Equals(y_, rhs.y_); }
+    /// Test for equality with another vector without epsilon.
+    bool operator == (const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
+    /// Test for inequality with another vector without epsilon.
+    bool operator != (const Vector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
     /// Add a vector.
     Vector2 operator + (const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
     /// Return negation.
@@ -158,6 +158,8 @@ public:
     Vector2 Abs() const { return Vector2(fabsf(x_), fabsf(y_)); }
     /// Linear interpolation with another vector.
     Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
+    ///// Test for equality with another vectir with epsilon.
+    bool Equals(const Vector2& rhs) const { return ::Equals(x_, rhs.x_) && ::Equals(y_, rhs.y_); }
     
     /// Return normalized to unit length.
     Vector2 Normalized() const

+ 6 - 4
Engine/Math/Vector3.h

@@ -75,10 +75,10 @@ public:
         return *this;
     }
     
-    /// Test for equality with another vector.
-    bool operator == (const Vector3& rhs) const { return Equals(x_, rhs.x_) && Equals(y_, rhs.y_) && Equals(z_, rhs.z_); }
-    /// Test for inequality with another vector.
-    bool operator != (const Vector3& rhs) const { return !Equals(x_, rhs.x_) || !Equals(y_, rhs.y_) || !Equals(z_, rhs.z_); }
+    /// Test for equality with another vector without epsilon.
+    bool operator == (const Vector3& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
+    /// Test for inequality with another vector without epsilon.
+    bool operator != (const Vector3& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
     /// Add a vector.
     Vector3 operator + (const Vector3& rhs) const { return Vector3(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
     /// Return negation.
@@ -187,6 +187,8 @@ public:
     Vector3 Abs() const { return Vector3(fabsf(x_), fabsf(y_), fabsf(z_)); }
     /// Linear interpolation with another vector.
     Vector3 Lerp(const Vector3& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
+    /// Test for equality with another vector with epsilon.
+    bool Equals(const Vector3& rhs) const { return ::Equals(x_, rhs.x_) && ::Equals(y_, rhs.y_) && ::Equals(z_, rhs.z_); }
     
     /// Return normalized to unit length.
     Vector3 Normalized() const

+ 6 - 4
Engine/Math/Vector4.h

@@ -80,10 +80,10 @@ public:
         return *this;
     }
     
-    /// Test for equality with another vector.
-    bool operator == (const Vector4& rhs) const { return Equals(x_, rhs.x_) && Equals(y_, rhs.y_) && Equals(z_, rhs.z_) && Equals(w_, rhs.w_); }
-    /// Test for inequality with another vector.
-    bool operator != (const Vector4& rhs) const { return !Equals(x_, rhs.x_) || !Equals(y_, rhs.y_) || !Equals(z_, rhs.z_) || !Equals(w_, rhs.w_); }
+    /// Test for equality with another vector without epsilon.
+    bool operator == (const Vector4& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_ && w_ == rhs.w_; }
+    /// Test for inequality with another vector without epsilon.
+    bool operator != (const Vector4& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_; }
     /// Add a vector.
     Vector4 operator + (const Vector4& rhs) const { return Vector4(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_, w_ + rhs.w_); }
     /// Return negation.
@@ -168,6 +168,8 @@ public:
     Vector4 Abs() const { return Vector4(fabsf(x_), fabsf(y_), fabsf(z_), fabsf(w_)); }
     /// Linear interpolation with another vector.
     Vector4 Lerp(const Vector4& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
+    /// Test for equality with another vector with epsilon.
+    bool Equals(const Vector4& rhs) const { return ::Equals(x_, rhs.x_) && ::Equals(y_, rhs.y_) && ::Equals(z_, rhs.z_) && ::Equals(w_, rhs.w_); }
     
     /// Return float data.
     const float* Data() const { return &x_; }

+ 1 - 1
Engine/Physics/CollisionShape.cpp

@@ -510,7 +510,7 @@ void CollisionShape::OnNodeSet(Node* node)
 void CollisionShape::OnMarkedDirty(Node* node)
 {
     Vector3 newWorldScale = node_->GetWorldScale();
-    if (newWorldScale != cachedWorldScale_ && shape_)
+    if (!newWorldScale.Equals(cachedWorldScale_) && shape_)
     {
         switch (shapeType_)
         {

+ 2 - 2
Engine/Physics/RigidBody.cpp

@@ -654,12 +654,12 @@ void RigidBody::OnMarkedDirty(Node* node)
         Vector3 newPosition = node_->GetWorldPosition();
         Quaternion newRotation = node_->GetWorldRotation();
         
-        if (newPosition != lastPosition_)
+        if (!newPosition.Equals(lastPosition_))
         {
             lastPosition_ = newPosition;
             SetPosition(newPosition);
         }
-        if (newRotation != GetRotation())
+        if (!newRotation.Equals(lastRotation_))
         {
             lastRotation_ = newRotation;
             SetRotation(newRotation);