Browse Source

Check for not overshooting the target when moving in the Navigation sample.

Lasse Öörni 12 years ago
parent
commit
50c54346eb

+ 8 - 2
Bin/Data/LuaScripts/15_Navigation.lua

@@ -318,9 +318,15 @@ function FollowPath(timeStep)
     if table.maxn(currentPath) > 0 then
     if table.maxn(currentPath) > 0 then
         local nextWaypoint = currentPath[1] -- NB: currentPath[1] is the next waypoint in order
         local nextWaypoint = currentPath[1] -- NB: currentPath[1] is the next waypoint in order
 
 
-        -- Rotate Jack toward next waypoint to reach and move
+        -- Rotate Jack toward next waypoint to reach and move. Check for not overshooting the target
+        local move = 5 * timeStep
+        local distance = (jackNode.position - nextWaypoint):Length()
+        if move > distance then
+            move = distance
+        end
+
         jackNode:LookAt(nextWaypoint, Vector3(0.0, 1.0, 0.0))
         jackNode:LookAt(nextWaypoint, Vector3(0.0, 1.0, 0.0))
-        jackNode:TranslateRelative(Vector3(0.0, 0.0, 1.0) * 5 * timeStep)
+        jackNode:TranslateRelative(Vector3(0.0, 0.0, 1.0) * move)
 
 
         -- Remove waypoint if reached it
         -- Remove waypoint if reached it
         if (jackNode.position - nextWaypoint):Length() < 0.1 then
         if (jackNode.position - nextWaypoint):Length() < 0.1 then

+ 7 - 2
Bin/Data/Scripts/15_Navigation.as

@@ -320,9 +320,14 @@ void FollowPath(float timeStep)
     {
     {
         Vector3 nextWaypoint = currentPath[0]; // NB: currentPath[0] is the next waypoint in order
         Vector3 nextWaypoint = currentPath[0]; // NB: currentPath[0] is the next waypoint in order
 
 
-        // Rotate Jack toward next waypoint to reach and move
+        // Rotate Jack toward next waypoint to reach and move. Check for not overshooting the target
+        float move = 5.0f * timeStep;
+        float distance = (jackNode.position - nextWaypoint).length;
+        if (move > distance)
+            move = distance;
+        
         jackNode.LookAt(nextWaypoint, Vector3(0.0f, 1.0f, 0.0f));
         jackNode.LookAt(nextWaypoint, Vector3(0.0f, 1.0f, 0.0f));
-        jackNode.TranslateRelative(Vector3(0.0f, 0.0f, 1.0f) * 5 * timeStep);
+        jackNode.TranslateRelative(Vector3(0.0f, 0.0f, 1.0f) * move);
 
 
         // Remove waypoint if reached it
         // Remove waypoint if reached it
         if ((jackNode.position - nextWaypoint).length < 0.1)
         if ((jackNode.position - nextWaypoint).length < 0.1)

+ 9 - 4
Source/Samples/15_Navigation/Navigation.cpp

@@ -282,7 +282,7 @@ void Navigation::SetPathPoint()
         {
         {
             // Teleport
             // Teleport
             currentPath_.Clear();
             currentPath_.Clear();
-            jackNode_->LookAt(Vector3(pathPos.x_, jackNode_->GetPosition().y_, pathPos.z_), Vector3(0.0f, 1.0f, 0.0f));
+            jackNode_->LookAt(Vector3(pathPos.x_, jackNode_->GetPosition().y_, pathPos.z_), Vector3::UP);
             jackNode_->SetPosition(pathPos);
             jackNode_->SetPosition(pathPos);
         }
         }
         else
         else
@@ -376,9 +376,14 @@ void Navigation::FollowPath(float timeStep)
     {
     {
         Vector3 nextWaypoint = currentPath_[0]; // NB: currentPath[0] is the next waypoint in order
         Vector3 nextWaypoint = currentPath_[0]; // NB: currentPath[0] is the next waypoint in order
 
 
-        // Rotate Jack toward next waypoint to reach and move
-        jackNode_->LookAt(nextWaypoint, Vector3(0.0f, 1.0f, 0.0f));
-        jackNode_->TranslateRelative(Vector3(0.0f, 0.0f, 1.0f) * 5.0f * timeStep);
+        // Rotate Jack toward next waypoint to reach and move. Check for not overshooting the target
+        float move = 5.0f * timeStep;
+        float distance = (jackNode_->GetPosition() - nextWaypoint).Length();
+        if (move > distance)
+            move = distance;
+        
+        jackNode_->LookAt(nextWaypoint, Vector3::UP);
+        jackNode_->TranslateRelative(Vector3::FORWARD * move);
 
 
         // Remove waypoint if reached it
         // Remove waypoint if reached it
         if ((jackNode_->GetPosition() - nextWaypoint).Length() < 0.1f)
         if ((jackNode_->GetPosition() - nextWaypoint).Length() < 0.1f)