|
|
@@ -4,19 +4,19 @@
|
|
|
// - Performing path queries to the navigation mesh
|
|
|
// - Rebuilding the navigation mesh partially when adding or removing objects
|
|
|
// - Visualizing custom debug geometry
|
|
|
+// - Raycasting drawable components
|
|
|
+// - Making a node follow the Detour path
|
|
|
|
|
|
#include "Scripts/Utilities/Sample.as"
|
|
|
|
|
|
Scene@ scene_;
|
|
|
Node@ cameraNode;
|
|
|
-Vector3 startPos;
|
|
|
Vector3 endPos;
|
|
|
Array<Vector3> currentPath;
|
|
|
float yaw = 0.0f;
|
|
|
float pitch = 0.0f;
|
|
|
bool drawDebug = false;
|
|
|
-bool startPosDefined = false;
|
|
|
-bool endPosDefined = false;
|
|
|
+Node@ jackNode;
|
|
|
|
|
|
void Start()
|
|
|
{
|
|
|
@@ -92,7 +92,15 @@ void CreateScene()
|
|
|
if (size >= 3.0f)
|
|
|
boxObject.occluder = true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ // Create Jack node that will follow the path
|
|
|
+ jackNode = scene_.CreateChild("Jack");
|
|
|
+ jackNode.position = Vector3(-5.0f, 0.0f, 20.0f);
|
|
|
+ AnimatedModel@ modelObject = jackNode.CreateComponent("AnimatedModel");
|
|
|
+ modelObject.model = cache.GetResource("Model", "Models/Jack.mdl");
|
|
|
+ modelObject.material = cache.GetResource("Material", "Materials/Jack.xml");
|
|
|
+ modelObject.castShadows = true;
|
|
|
+
|
|
|
// Create a NavigationMesh component to the scene root
|
|
|
NavigationMesh@ navMesh = scene_.CreateComponent("NavigationMesh");
|
|
|
// Create a Navigable component to the scene root. This tags all of the geometry in the scene as being part of the
|
|
|
@@ -130,7 +138,7 @@ void CreateUI()
|
|
|
Text@ instructionText = ui.root.CreateChild("Text");
|
|
|
instructionText.text =
|
|
|
"Use WASD keys to move, RMB to rotate view\n"
|
|
|
- "Shift+LMB to set path start, LMB to set path end\n"
|
|
|
+ "LMB to set destination, SHIFT+LMB to teleport\n"
|
|
|
"MMB to add or remove obstacles\n"
|
|
|
"Space to toggle debug geometry";
|
|
|
instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
|
|
|
@@ -168,12 +176,12 @@ void MoveCamera(float timeStep)
|
|
|
// Do not move if the UI has a focused element (the console)
|
|
|
if (ui.focusElement !is null)
|
|
|
return;
|
|
|
-
|
|
|
+
|
|
|
// Movement speed as world units per second
|
|
|
const float MOVE_SPEED = 20.0f;
|
|
|
// Mouse sensitivity as degrees per pixel
|
|
|
const float MOUSE_SENSITIVITY = 0.1f;
|
|
|
-
|
|
|
+
|
|
|
// Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
|
|
|
// Only move the camera when the cursor is hidden
|
|
|
if (!ui.cursor.visible)
|
|
|
@@ -197,7 +205,7 @@ void MoveCamera(float timeStep)
|
|
|
if (input.keyDown['D'])
|
|
|
cameraNode.TranslateRelative(Vector3(1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
|
|
|
|
|
|
- // Set route start/endpoint with left mouse button, recalculate route if applicable
|
|
|
+ // Set destination or teleport with left mouse button
|
|
|
if (input.mouseButtonPress[MOUSEB_LEFT])
|
|
|
SetPathPoint();
|
|
|
// Add or remove objects with middle mouse button, then rebuild navigation mesh partially
|
|
|
@@ -213,22 +221,25 @@ void SetPathPoint()
|
|
|
{
|
|
|
Vector3 hitPos;
|
|
|
Drawable@ hitDrawable;
|
|
|
-
|
|
|
+ NavigationMesh@ navMesh = scene_.GetComponent("NavigationMesh");
|
|
|
+
|
|
|
if (Raycast(250.0f, hitPos, hitDrawable))
|
|
|
{
|
|
|
- bool setStart = input.qualifierDown[QUAL_SHIFT];
|
|
|
- if (setStart)
|
|
|
+ Vector3 pathPos = navMesh.FindNearestPoint(hitPos, Vector3(1.0f, 1.0f, 1.0f));
|
|
|
+
|
|
|
+ if (input.qualifierDown[QUAL_SHIFT])
|
|
|
{
|
|
|
- startPos = hitPos;
|
|
|
- startPosDefined = true;
|
|
|
+ // Teleport
|
|
|
+ currentPath.Clear();
|
|
|
+ jackNode.LookAt(Vector3(pathPos.x, jackNode.position.y, pathPos.z), Vector3(0.0f, 1.0f, 0.0f));
|
|
|
+ jackNode.position = pathPos;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- endPos = hitPos;
|
|
|
- endPosDefined = true;
|
|
|
+ // Calculate path from Jack's current position to the end point
|
|
|
+ endPos = pathPos;
|
|
|
+ currentPath = navMesh.FindPath(jackNode.position, endPos);
|
|
|
}
|
|
|
-
|
|
|
- RecalculatePath();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -237,13 +248,13 @@ void AddOrRemoveObject()
|
|
|
// Raycast and check if we hit a mushroom node. If yes, remove it, if no, create a new one
|
|
|
Vector3 hitPos;
|
|
|
Drawable@ hitDrawable;
|
|
|
-
|
|
|
+
|
|
|
if (Raycast(250.0f, hitPos, hitDrawable))
|
|
|
{
|
|
|
// The part of the navigation mesh we must update, which is the world bounding box of the associated
|
|
|
// drawable component
|
|
|
BoundingBox updateBox;
|
|
|
-
|
|
|
+
|
|
|
Node@ hitNode = hitDrawable.node;
|
|
|
if (hitNode.name == "Mushroom")
|
|
|
{
|
|
|
@@ -256,11 +267,12 @@ void AddOrRemoveObject()
|
|
|
StaticModel@ newObject = newNode.GetComponent("StaticModel");
|
|
|
updateBox = newObject.worldBoundingBox;
|
|
|
}
|
|
|
-
|
|
|
- // Rebuild part of the navigation mesh, then recalculate path if applicable
|
|
|
+
|
|
|
+ // Rebuild part of the navigation mesh, then rebuild the path if applicable
|
|
|
NavigationMesh@ navMesh = scene_.GetComponent("NavigationMesh");
|
|
|
navMesh.Build(updateBox);
|
|
|
- RecalculatePath();
|
|
|
+ if (currentPath.length > 0)
|
|
|
+ currentPath = navMesh.FindPath(jackNode.position, endPos);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -274,17 +286,8 @@ Node@ CreateMushroom(const Vector3& pos)
|
|
|
mushroomObject.model = cache.GetResource("Model", "Models/Mushroom.mdl");
|
|
|
mushroomObject.material = cache.GetResource("Material", "Materials/Mushroom.xml");
|
|
|
mushroomObject.castShadows = true;
|
|
|
-
|
|
|
- return mushroomNode;
|
|
|
-}
|
|
|
|
|
|
-void RecalculatePath()
|
|
|
-{
|
|
|
- if (!startPosDefined || !endPosDefined)
|
|
|
- return;
|
|
|
-
|
|
|
- NavigationMesh@ navMesh = scene_.GetComponent("NavigationMesh");
|
|
|
- currentPath = navMesh.FindPath(startPos, endPos);
|
|
|
+ return mushroomNode;
|
|
|
}
|
|
|
|
|
|
bool Raycast(float maxDistance, Vector3& hitPos, Drawable@& hitDrawable)
|
|
|
@@ -311,6 +314,27 @@ bool Raycast(float maxDistance, Vector3& hitPos, Drawable@& hitDrawable)
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+void FollowPath(float timeStep)
|
|
|
+{
|
|
|
+ if (currentPath.length > 0)
|
|
|
+ {
|
|
|
+ Vector3 nextWaypoint = currentPath[0]; // NB: currentPath[0] is the next waypoint in order
|
|
|
+
|
|
|
+ // 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.TranslateRelative(Vector3(0.0f, 0.0f, 1.0f) * move);
|
|
|
+
|
|
|
+ // Remove waypoint if reached it
|
|
|
+ if ((jackNode.position - nextWaypoint).length < 0.1)
|
|
|
+ currentPath.Erase(0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
void HandleUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
|
// Take the frame time step, which is stored as a float
|
|
|
@@ -318,6 +342,9 @@ void HandleUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
|
|
|
// Move the camera, scale movement with time step
|
|
|
MoveCamera(timeStep);
|
|
|
+
|
|
|
+ // Make Jack follow the Detour path
|
|
|
+ FollowPath(timeStep);
|
|
|
}
|
|
|
|
|
|
void HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
@@ -329,18 +356,22 @@ void HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
navMesh.DrawDebugGeometry(true);
|
|
|
}
|
|
|
|
|
|
- // Visualize the start and end points and the last calculated path
|
|
|
- // Note the convenience accessor to the DebugRenderer component
|
|
|
- DebugRenderer@ debug = scene_.debugRenderer;
|
|
|
- if (startPosDefined)
|
|
|
- debug.AddBoundingBox(BoundingBox(startPos - Vector3(0.1f, 0.1f, 0.1f), startPos + Vector3(0.1f, 0.1f, 0.1f)), Color(1.0f, 1.0f, 1.0f));
|
|
|
- if (endPosDefined)
|
|
|
- debug.AddBoundingBox(BoundingBox(endPos - Vector3(0.1f, 0.1f, 0.1f), endPos + Vector3(0.1f, 0.1f, 0.1f)), Color(1.0f, 1.0f, 1.0f));
|
|
|
- if (currentPath.length > 1)
|
|
|
+ if (currentPath.length > 0)
|
|
|
{
|
|
|
+ // Visualize the current calculated path
|
|
|
+ // Note the convenience accessor to the DebugRenderer component
|
|
|
+ DebugRenderer@ debug = scene_.debugRenderer;
|
|
|
+ debug.AddBoundingBox(BoundingBox(endPos - Vector3(0.1f, 0.1f, 0.1f), endPos + Vector3(0.1f, 0.1f, 0.1f)),
|
|
|
+ Color(1.0f, 1.0f, 1.0f));
|
|
|
+
|
|
|
// Draw the path with a small upward bias so that it does not clip into the surfaces
|
|
|
Vector3 bias(0.0f, 0.05f, 0.0f);
|
|
|
- for (uint i = 0; i < currentPath.length - 1; ++i)
|
|
|
- debug.AddLine(currentPath[i] + bias, currentPath[i + 1] + bias, Color(1.0f, 1.0f, 1.0f));
|
|
|
+ debug.AddLine(jackNode.position + bias, currentPath[0] + bias, Color(1.0f, 1.0f, 1.0f));
|
|
|
+
|
|
|
+ if (currentPath.length > 1)
|
|
|
+ {
|
|
|
+ for (uint i = 0; i < currentPath.length - 1; ++i)
|
|
|
+ debug.AddLine(currentPath[i] + bias, currentPath[i + 1] + bias, Color(1.0f, 1.0f, 1.0f));
|
|
|
+ }
|
|
|
}
|
|
|
}
|