Browse Source

Merge remote-tracking branch 'remotes/alexparlett/feature/spline'

Lasse Öörni 12 years ago
parent
commit
71e9559bee

+ 28 - 0
Source/Engine/LuaScript/pkgs/Scene/Spline.pkg

@@ -0,0 +1,28 @@
+$#include "Spline.h"
+
+enum InterpolationMode
+{
+    BEZIER_CURVE
+};
+
+class Spline : public Component
+{
+    void SetInterpolationMode(InterpolationMode interpolationMode);
+    void SetSpeed(float speed);
+    void SetPosition(float factor);
+    
+    InterpolationMode GetInterpolationMode() const;
+    float GetSpeed() const;
+    Vector3 GetPosition() const;
+    
+    void Push(const Vector3& controlPoint);
+    void Pop();
+    Vector3 GetPoint(float factor) const;    
+    
+    void Move(float timeStep);
+    void Reset();
+    bool IsFinished() const;
+    
+    tolua_property__get_set InterpolationMode interpolationMode;
+    tolua_property__get_set float speed;
+};

+ 1 - 0
Source/Engine/LuaScript/pkgs/SceneLuaAPI.pkg

@@ -2,6 +2,7 @@ $pfile "Scene/Serializable.pkg"
 $pfile "Scene/Component.pkg"
 $pfile "Scene/Node.pkg"
 $pfile "Scene/Scene.pkg"
+$pfile "Scene/Spline.pkg"
 
 $using namespace Urho3D;
 $#pragma warning(disable:4800)

+ 2 - 0
Source/Engine/Scene/Scene.cpp

@@ -32,6 +32,7 @@
 #include "Scene.h"
 #include "SceneEvents.h"
 #include "SmoothedTransform.h"
+#include "Spline.h"
 #include "WorkQueue.h"
 #include "XMLFile.h"
 
@@ -955,6 +956,7 @@ void RegisterSceneLibrary(Context* context)
     Node::RegisterObject(context);
     Scene::RegisterObject(context);
     SmoothedTransform::RegisterObject(context);
+    Spline::RegisterObject(context);
 }
 
 }

+ 209 - 0
Source/Engine/Scene/Spline.cpp

@@ -0,0 +1,209 @@
+//
+// Copyright (c) 2008-2013 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "Precompiled.h"
+#include "Context.h"
+#include "Log.h"
+#include "Node.h"
+#include "Scene.h"
+#include "Spline.h"
+
+namespace Urho3D
+{
+
+extern const char* SCENE_CATEGORY;
+
+Spline::Spline(Context* context) :
+    Component(context),
+    interpolationMode_(BEZIER_CURVE),
+    speed_(1.f),
+    elapsedTime_(0.f),
+    length_(0.f),
+    traveled_(0.f),
+    dirty_(false)
+{
+}
+
+void Spline::RegisterObject(Context* context)
+{
+    context->RegisterFactory<Spline>(SCENE_CATEGORY);
+
+    ACCESSOR_ATTRIBUTE(Spline, VAR_VARIANTVECTOR, "Control Points", GetControlPointsAttr, SetControlPointsAttr, VariantVector, Variant::emptyVariantVector, AM_FILE);
+    ATTRIBUTE(Spline, VAR_FLOAT, "Speed", speed_, 1.f, AM_FILE);
+    ATTRIBUTE(Spline, VAR_INT, "Interpolation Mode", interpolationMode_, BEZIER_CURVE, AM_FILE);
+    ATTRIBUTE(Spline, VAR_FLOAT, "Traveled", traveled_, 0.f, AM_FILE | AM_NOEDIT);
+    ATTRIBUTE(Spline, VAR_FLOAT, "Elapsed Time", elapsedTime_, 0.f, AM_FILE | AM_NOEDIT);
+}
+
+void Spline::SetControlPoints(const PODVector<Vector3>& controlPoints)
+{
+    controlPoints_ = controlPoints;
+
+    // We can calculate the length here because all the control points have changed so it shouldn't be too expensive.
+    CalculateLength();
+}
+
+void Spline::SetPosition(float factor)
+{
+    float t = factor;
+
+    if (t < 0.f)
+        t = 0.0f;
+    else if (t > 1.0f)
+        t = 1.0f;
+
+    traveled_ = t;
+}
+
+Vector3 Spline::GetPosition() const
+{
+    return GetPoint(traveled_);
+}
+
+Vector3 Spline::GetPoint(float factor) const
+{
+    float t = factor;
+
+    if (t < 0.f)
+        t = 0.0f;
+    else if (t > 1.0f)
+        t = 1.0f;
+
+    switch (interpolationMode_)
+    {
+    case BEZIER_CURVE:
+        return BezierMove(controlPoints_, t);
+
+    default:
+        return Vector3::ZERO;
+    }
+}
+
+void Spline::Push(const Vector3& controlPoint)
+{
+    controlPoints_.Push(controlPoint);
+
+    // Calculate the length at the next move so we don't recalculate the entire length multiple times if more than one control point is changed.
+    dirty_ = true;
+}
+
+void Spline::Pop()
+{
+    controlPoints_.Pop();
+
+    // Calculate the length at the next move so we don't recalculate the entire length multiple times if more than one control point is changed.
+    dirty_ = true;
+}
+
+void Spline::Move(float timeStep)
+{
+    if (dirty_)
+        CalculateLength();
+
+    if (traveled_ >= 1.0f || length_ <= 0.0f)
+        return;
+
+    elapsedTime_ += timeStep;
+
+    // Calculate where we should be on the spline based on length, speed and time. If that is less than the set traveled_ don't move till caught up.
+    float distanceCovered = elapsedTime_ * speed_;
+    traveled_ = distanceCovered / length_;
+
+    switch (interpolationMode_)
+    {
+    case BEZIER_CURVE:
+        if (controlPoints_.Size() < 2)
+        {
+            LOGERRORF("Spline on Node[%d,%s] in Beizer Curve mode attempted with less than two control points.", GetNode()->GetID(), GetNode()->GetName().CString());
+            return;
+        }
+        GetNode()->SetPosition(BezierMove(controlPoints_,traveled_));
+        break;
+    }
+}
+
+void Spline::Reset()
+{
+    traveled_ = 0.f;
+    elapsedTime_ = 0.f;
+}
+
+Urho3D::VariantVector Spline::GetControlPointsAttr() const
+{
+    VariantVector ret;
+    for (unsigned i = 0; i < controlPoints_.Size(); i++)
+        ret.Push(controlPoints_[i]);
+    return ret;
+}
+
+void Spline::SetControlPointsAttr(VariantVector value)
+{
+    for (unsigned i = 0; i < value.Size(); i++)
+        controlPoints_.Push(value[i].GetVector3());
+
+    CalculateLength();
+}
+
+void Spline::CalculateLength()
+{
+    if (dirty_)
+        dirty_ = false;
+
+    length_ = 0.f;
+
+    if (controlPoints_.Size() <= 0)
+    {
+        return;
+    }
+
+    switch (interpolationMode_)
+    {
+    case BEZIER_CURVE:
+        Vector3 a = controlPoints_[0];
+        for (float f = 0.000f; f <= 1.000f; f += 0.001f)
+        {
+            Vector3 b = BezierMove(controlPoints_, f);
+            length_ += Abs((a - b).Length());
+            a = b;
+        }
+        break;
+    }
+}
+
+Vector3 Spline::BezierMove(const PODVector<Vector3>& controlPoints, float t) const
+{
+    if (controlPoints.Size() == 2)
+    {
+        return controlPoints[0].Lerp(controlPoints[1], t);
+    }
+    else
+    {
+        PODVector<Vector3> newControlPoints;
+        for (unsigned i = 1; i < controlPoints.Size(); i++)
+        {
+            newControlPoints.Push(controlPoints[i - 1].Lerp(controlPoints[i], t));
+        }
+        return BezierMove(newControlPoints, t);
+    }
+}
+
+}

+ 104 - 0
Source/Engine/Scene/Spline.h

@@ -0,0 +1,104 @@
+//
+// Copyright (c) 2008-2013 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "Component.h"
+#include "Vector3.h"
+
+namespace Urho3D
+{
+/// Interpolation Mode for a Spline.
+enum InterpolationMode
+{
+    BEZIER_CURVE
+};
+
+/// Spline for creating smooth movement based on Speed along a set of Control Points modified by the Interpolation Mode.
+class URHO3D_API Spline : public Component
+{
+    OBJECT(Spline)
+
+public:
+    /// Construct an Empty Spline.
+    Spline(Context* context);
+    /// Register object factory.
+    static void RegisterObject(Context* context);
+
+    /// Set the Control Points from an already defined set.
+    void SetControlPoints(const PODVector<Vector3>& controlPoints);
+    /// Set the Interpolation Mode.
+    void SetInterpolationMode(InterpolationMode interpolationMode) { interpolationMode_ = interpolationMode; }
+    /// Set the movement Speed.
+    void SetSpeed(float speed) { speed_ = speed; }
+    /// Set the parent node's position on the Spline.
+    void SetPosition(float factor);
+
+    /// Get the Control Points.
+    const PODVector<Vector3>& GetControlPoints() const { return controlPoints_; }
+    /// Get the Interpolation Mode.
+    InterpolationMode GetInterpolationMode() const { return interpolationMode_; }
+    /// Get the movement Speed.
+    float GetSpeed() const { return speed_; }
+    /// Get the parent node's last position on the spline.
+    Vector3 GetPosition() const;
+
+    /// Add a Control Point to the end.
+    void Push(const Vector3& controlPoint);
+    /// Remove a Control Point from the end.
+    void Pop();
+    /// Get a point on the spline from 0.f to 1.f where 0 is the start and 1 is the end.
+    Vector3 GetPoint(float factor) const;
+
+    /// Move the parent node to the next position along the Spline based off the Speed value.
+    void Move(float timeStep);
+    /// Reset movement along the path.
+    void Reset();
+    /// Returns whether the movement along the Spline complete.
+    bool IsFinished() const { return traveled_ >= 1.0f; }
+
+    VariantVector GetControlPointsAttr() const;
+    void SetControlPointsAttr(VariantVector value);
+
+private:
+    /// Calculate the length of the Spline. Used for movement calculations.
+    void CalculateLength();
+    /// Move the parent node along the Spline in Bezier Mode.
+    Vector3 BezierMove(const PODVector<Vector3>& controlPoints, float t) const;
+
+    /// The Control Points of the Spline.
+    PODVector<Vector3> controlPoints_;
+    /// The Interpolation Mode of the Spline.
+    InterpolationMode interpolationMode_;
+    /// The Speed of movement along the Spline.
+    float speed_;
+
+    /// Amount of time that has elapsed while moving.
+    float elapsedTime_;
+    /// The fraction of the Spline covered.
+    float traveled_;
+    /// The length of the Spline.
+    float length_;
+    /// Whether the length needs to be recalculated. Will only be true after a push or pop.
+    bool dirty_;
+};
+}

+ 24 - 0
Source/Engine/Script/SceneAPI.cpp

@@ -26,6 +26,7 @@
 #include "Scene.h"
 #include "SmoothedTransform.h"
 #include "Sort.h"
+#include "Spline.h"
 
 namespace Urho3D
 {
@@ -224,12 +225,35 @@ static void RegisterScene(asIScriptEngine* engine)
     engine->RegisterGlobalFunction("Array<String>@ GetObjectsByCategory(const String&in)", asFUNCTION(GetObjectsByCategory), asCALL_CDECL);
 }
 
+static void RegisterSpline(asIScriptEngine* engine)
+{
+    engine->RegisterEnum("InterpolationMode");
+    engine->RegisterEnumValue("InterpolationMode", "BEZIER_CURVE", BEZIER_CURVE);
+
+    RegisterComponent<Spline>(engine, "Spline", true, false);
+    engine->RegisterObjectMethod("Spline", "void set_controlPoints(Array<Vector3>@+)", asMETHOD(Spline, SetControlPoints), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "Array<Vector3>@ get_controlPoints() const", asMETHOD(Spline, GetControlPoints), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "void set_interpolationMode(InterpolationMode)", asMETHOD(Spline, SetInterpolationMode), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "InterpolationMode get_interpolationMode() const", asMETHOD(Spline, GetInterpolationMode), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "void set_speed(float)", asMETHOD(Spline, SetSpeed), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "float get_speed() const", asMETHOD(Spline, GetSpeed), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "void set_position(float)", asMETHOD(Spline, SetPosition), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "Vector3 get_position() const", asMETHOD(Spline, GetPosition), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "void Push(const Vector3&in)", asMETHOD(Spline, Push), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "void Pop()", asMETHOD(Spline, Pop), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "Vector3 GetPoint(float) const", asMETHOD(Spline, GetPoint), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "void Move(float)", asMETHOD(Spline, Move), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "void Reset()", asMETHOD(Spline, Reset), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Spline", "bool get_finished() const", asMETHOD(Spline, IsFinished), asCALL_THISCALL);
+}
+
 void RegisterSceneAPI(asIScriptEngine* engine)
 {
     RegisterSerializable(engine);
     RegisterNode(engine);
     RegisterSmoothedTransform(engine);
     RegisterScene(engine);
+    RegisterSpline(engine);
 }
 
 }