Sfoglia il codice sorgente

Fixing issues raised by Mike3D (incorrect offset in sample, exposing auto solve)

Alex Murray 8 anni fa
parent
commit
68f8d23411

+ 2 - 2
Source/Samples/45_InverseKinematics/InverseKinematics.cpp

@@ -280,7 +280,7 @@ void InverseKinematics::HandleSceneDrawableUpdateFinished(StringHash eventType,
         // to the ray intersection
         phyWorld->RaycastSingle(result, Ray(leftFootPosition + result.normal_, -result.normal_), 2);
         // The foot node has an offset relative to the root node
-        float footOffset = jackNode_->GetWorldPosition().y_ + leftFoot_->GetWorldPosition().y_;
+        float footOffset = leftFoot_->GetWorldPosition().y_ - jackNode_->GetWorldPosition().y_;
         leftEffector_->SetTargetPosition(result.position_ + result.normal_ * footOffset);
         // Rotate foot according to normal
         leftFoot_->Rotate(Quaternion(Vector3(0, 1, 0), result.normal_), TS_WORLD);
@@ -291,7 +291,7 @@ void InverseKinematics::HandleSceneDrawableUpdateFinished(StringHash eventType,
     if (result.body_)
     {
         phyWorld->RaycastSingle(result, Ray(rightFootPosition + result.normal_, -result.normal_), 2);
-        float footOffset = jackNode_->GetWorldPosition().y_ + rightFoot_->GetWorldPosition().y_;
+        float footOffset = rightFoot_->GetWorldPosition().y_ - jackNode_->GetWorldPosition().y_;
         rightEffector_->SetTargetPosition(result.position_ + result.normal_ * footOffset);
         rightFoot_->Rotate(Quaternion(Vector3(0, 1, 0), result.normal_), TS_WORLD);
     }

+ 2 - 5
Source/Samples/45_InverseKinematics/InverseKinematics.h

@@ -26,18 +26,15 @@
 
 namespace Urho3D
 {
-
 class AnimationController;
 class Node;
 class IKEffector;
 class IKSolver;
 class Scene;
-class RibbonTrail;
-
 }
 
-/// Ribbon trail demo.
-/// This sample demonstrates how to use both trail types of RibbonTrail component.
+/// Inverse Kinematics demo.
+/// This sample demonstrates how to adjust the position of animated feet so they match the ground's angle using IK
 class InverseKinematics : public Sample
 {
     URHO3D_OBJECT(InverseKinematics, Sample);

+ 2 - 2
Source/ThirdParty/ik/ik/include/ik/ordered_vector.h

@@ -31,7 +31,7 @@ struct ordered_vector_t
  * @return Returns the newly created vector object.
  */
 IK_PUBLIC_API struct ordered_vector_t*
-ordered_vector_create(uint32_t element_size);
+ordered_vector_create(const uint32_t element_size);
 
 /*!
  * @brief Initialises an existing vector object.
@@ -43,7 +43,7 @@ ordered_vector_create(uint32_t element_size);
  */
 IK_PUBLIC_API void
 ordered_vector_construct(struct ordered_vector_t* vector,
-                           uint32_t element_size);
+                           const uint32_t element_size);
 
 /*!
  * @brief Destroys an existing vector object and frees all memory allocated by

+ 4 - 4
Source/Urho3D/IK/IKEffector.cpp

@@ -61,7 +61,7 @@ void IKEffector::RegisterObject(Context* context)
     context->RegisterFactory<IKEffector>(IK_CATEGORY);
 
     URHO3D_ACCESSOR_ATTRIBUTE("Target Node", GetTargetName, SetTargetName, String, String::EMPTY, AM_DEFAULT);
-    URHO3D_ACCESSOR_ATTRIBUTE("Chain Length", GetChainLength, SetChainLength, unsigned, true, AM_DEFAULT);
+    URHO3D_ACCESSOR_ATTRIBUTE("Chain Length", GetChainLength, SetChainLength, unsigned, 0, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Target Position", GetTargetPosition, SetTargetPosition, Vector3, Vector3::ZERO, AM_DEFAULT);
     URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Target Rotation", GetTargetRotationEuler, SetTargetRotationEuler, Vector3, Vector3::ZERO, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Weight", GetWeight, SetWeight, float, 1.0, AM_DEFAULT);
@@ -290,7 +290,7 @@ void IKEffector::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
     a = node_;
     Node* b = a->GetParent();
     debug->AddSphere(
-        Sphere(a->GetWorldPosition(), averageLength * 0.1),
+        Sphere(a->GetWorldPosition(), averageLength * 0.1f),
         Color::YELLOW,
         depthTest
     );
@@ -303,7 +303,7 @@ void IKEffector::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
             depthTest
         );
         debug->AddSphere(
-            Sphere(b->GetWorldPosition(), averageLength * 0.1),
+            Sphere(b->GetWorldPosition(), averageLength * 0.1f),
             Color::YELLOW,
             depthTest
         );
@@ -313,7 +313,7 @@ void IKEffector::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
 
     Vector3 direction = targetRotation_ * Vector3::FORWARD;
     direction = direction * averageLength + targetPosition_;
-    debug->AddSphere(Sphere(targetPosition_, averageLength * 0.2), Color(255, 128, 0), depthTest);
+    debug->AddSphere(Sphere(targetPosition_, averageLength * 0.2f), Color(255, 128, 0), depthTest);
     debug->AddLine(targetPosition_, direction, Color(255, 128, 0), depthTest);
 }
 

+ 8 - 7
Source/Urho3D/IK/IKSolver.cpp

@@ -109,6 +109,7 @@ void IKSolver::RegisterObject(Context* context)
     URHO3D_ACCESSOR_ATTRIBUTE("Target Rotation", TargetRotationEnabled, EnableTargetRotation, bool, false, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Continuous Solving", ContinuousSolvingEnabled, EnableContinuousSolving, bool, false, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Update Pose", UpdatePoseEnabled, EnableUpdatePose, bool, false, AM_DEFAULT);
+    URHO3D_ACCESSOR_ATTRIBUTE("Auto Solve", AutoSolveEnabled, EnableAutoSolve, bool, true, AM_DEFAULT);
 }
 
 // ----------------------------------------------------------------------------
@@ -162,7 +163,7 @@ void IKSolver::SetTolerance(float tolerance)
 // ----------------------------------------------------------------------------
 bool IKSolver::BoneRotationsEnabled() const
 {
-    return solver_->flags & SOLVER_CALCULATE_FINAL_ROTATIONS;
+    return (solver_->flags & SOLVER_CALCULATE_FINAL_ROTATIONS) != 0;
 }
 
 // ----------------------------------------------------------------------------
@@ -176,7 +177,7 @@ void IKSolver::EnableBoneRotations(bool enable)
 // ----------------------------------------------------------------------------
 bool IKSolver::TargetRotationEnabled() const
 {
-    return solver_->flags & SOLVER_CALCULATE_TARGET_ROTATIONS;
+    return (solver_->flags & SOLVER_CALCULATE_TARGET_ROTATIONS) != 0;
 }
 
 // ----------------------------------------------------------------------------
@@ -190,7 +191,7 @@ void IKSolver::EnableTargetRotation(bool enable)
 // ----------------------------------------------------------------------------
 bool IKSolver::ContinuousSolvingEnabled() const
 {
-    return solver_->flags & SOLVER_SKIP_RESET;
+    return (solver_->flags & SOLVER_SKIP_RESET) != 0;
 }
 
 // ----------------------------------------------------------------------------
@@ -553,12 +554,12 @@ void IKSolver::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
         a = *pnode;
         b = a->parent;
         debug->AddSphere(
-            Sphere(Vec3IK2Urho(&a->position), averageLength * 0.1),
+            Sphere(Vec3IK2Urho(&a->position), averageLength * 0.1f),
             Color(0, 0, 255),
             depthTest
         );
         debug->AddSphere(
-            Sphere(Vec3IK2Urho(&a->solved_position), averageLength * 0.1),
+            Sphere(Vec3IK2Urho(&a->solved_position), averageLength * 0.1f),
             Color(255, 128, 0),
             depthTest
         );
@@ -571,7 +572,7 @@ void IKSolver::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
                 depthTest
             );
             debug->AddSphere(
-                Sphere(Vec3IK2Urho(&b->position), averageLength * 0.1),
+                Sphere(Vec3IK2Urho(&b->position), averageLength * 0.1f),
                 Color(0, 0, 255),
                 depthTest
             );
@@ -582,7 +583,7 @@ void IKSolver::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
                 depthTest
             );
             debug->AddSphere(
-                Sphere(Vec3IK2Urho(&b->solved_position), averageLength * 0.1),
+                Sphere(Vec3IK2Urho(&b->solved_position), averageLength * 0.1f),
                 Color(255, 128, 0),
                 depthTest
             );

+ 2 - 2
bin/Data/LuaScripts/45_InverseKinematics.lua

@@ -217,7 +217,7 @@ function HandleSceneDrawableUpdateFinished(eventType, eventData)
         local oppositeNormal = result.normal * -1
         result = physicsWorld:RaycastSingle(Ray(leftFootPosition + result.normal, oppositeNormal), 2)
         -- The foot node has an offset relative to the root node
-        footOffset = jackNode_.worldPosition.y + leftFoot_.worldPosition.y
+        footOffset = leftFoot_.worldPosition.y - jackNode_.worldPosition.y
         leftEffector_.targetPosition = result.position + result.normal * footOffset
         -- Rotate foot according to normal
         leftFoot_:Rotate(Quaternion(Vector3(0, 1, 0), result.normal), TS_WORLD)
@@ -228,7 +228,7 @@ function HandleSceneDrawableUpdateFinished(eventType, eventData)
     if result.body then
         local oppositeNormal = result.normal * -1
         result = physicsWorld:RaycastSingle(Ray(rightFootPosition + result.normal, oppositeNormal), 2)
-        footOffset = jackNode_.worldPosition.y + rightFoot_.worldPosition.y
+        footOffset = rightFoot_.worldPosition.y - jackNode_.worldPosition.y
         rightEffector_.targetPosition = result.position + result.normal * footOffset
         rightFoot_:Rotate(Quaternion(Vector3(0, 1, 0), result.normal), TS_WORLD)
     end

+ 2 - 2
bin/Data/Scripts/45_InverseKinematics.as

@@ -228,7 +228,7 @@ void HandleSceneDrawableUpdateFinished(StringHash eventType, VariantMap& eventDa
         // to the ray intersection
         result = scene_.physicsWorld.RaycastSingle(Ray(leftFootPosition + result.normal, -result.normal), 2);
         // The foot node has an offset relative to the root node
-        float footOffset = jackNode_.worldPosition.y + leftFoot_.worldPosition.y;
+        float footOffset = leftFoot_.worldPosition.y - jackNode_.worldPosition.y;
         leftEffector_.targetPosition = result.position + result.normal * footOffset;
         // Rotate foot according to normal
         leftFoot_.Rotate(Quaternion(Vector3(0, 1, 0), result.normal), TS_WORLD);
@@ -239,7 +239,7 @@ void HandleSceneDrawableUpdateFinished(StringHash eventType, VariantMap& eventDa
     if (result.body !is null)
     {
         result = scene_.physicsWorld.RaycastSingle(Ray(rightFootPosition + result.normal, -result.normal), 2);
-        float footOffset = jackNode_.worldPosition.y + rightFoot_.worldPosition.y;
+        float footOffset = rightFoot_.worldPosition.y - jackNode_.worldPosition.y;
         rightEffector_.targetPosition = result.position + result.normal * footOffset;
         rightFoot_.Rotate(Quaternion(Vector3(0, 1, 0), result.normal), TS_WORLD);
     }