From 90d8b0479d1f8e391839df5202f587e9b5b2f99d Mon Sep 17 00:00:00 2001 From: karl Date: Wed, 31 Mar 2021 14:49:25 +0200 Subject: [PATCH] Add basic pathfinding API This should cover all requirements from the assignment, but a more specific use case will be added later. --- include/Gedeng/NavMesh.h | 28 ++++++++++++++++++++++++++++ include/Gedeng/Node.h | 15 +++++++++++++++ include/Gedeng/NodeSystem.h | 17 +++++++++++++++++ include/Gedeng/Path.h | 15 +++++++++++++++ include/Gedeng/Vector3.h | 7 +++++++ 5 files changed, 82 insertions(+) create mode 100644 include/Gedeng/NavMesh.h create mode 100644 include/Gedeng/Node.h create mode 100644 include/Gedeng/NodeSystem.h create mode 100644 include/Gedeng/Path.h create mode 100644 include/Gedeng/Vector3.h diff --git a/include/Gedeng/NavMesh.h b/include/Gedeng/NavMesh.h new file mode 100644 index 0000000..8de3d0f --- /dev/null +++ b/include/Gedeng/NavMesh.h @@ -0,0 +1,28 @@ +#pragma once +#include "Node.h" +#include "Path.h" + +namespace Gedeng { + +class NavMesh { + // Build the NavMesh from all collider geometry in the given node and all + // its children. + void create_from_nodes(const Node &root); + + // Return true if the NavMesh has succesfully been created from nodes (by a + // former `create_from_nodes` call) and its geometry is valid. + bool is_valid() const; + + // Return a valid path from a given Vector3 to a given Vector3. + // If no valid path can be constructed, the returned path will be of size 1 + // and contain only the `from` vector. + // The returned path may not be optimal -- if that is undesired, call + // `optimize_path` on the result. + Path get_path(const Vector3 &from, const Vector3 &to) const; + + // Optimize the given path to traverse the NavMesh as efficiently as + // possible. This can be slow! + void optimize_path(Path &path) const; +}; + +} // namespace Gedeng \ No newline at end of file diff --git a/include/Gedeng/Node.h b/include/Gedeng/Node.h new file mode 100644 index 0000000..0e41a76 --- /dev/null +++ b/include/Gedeng/Node.h @@ -0,0 +1,15 @@ +#pragma once +#include "Gedeng/String.h" +#include "Gedeng/Vector.h" + +namespace Gedeng { + +class Node { + // Add the given node as a child of this node. + void add_child(const Node &node); + + // Return a vector with all child nodes of this node. + Vector get_children() const; +}; + +} // namespace Gedeng \ No newline at end of file diff --git a/include/Gedeng/NodeSystem.h b/include/Gedeng/NodeSystem.h new file mode 100644 index 0000000..7458d17 --- /dev/null +++ b/include/Gedeng/NodeSystem.h @@ -0,0 +1,17 @@ +#pragma once +#include "Gedeng/Node.h" +#include "Gedeng/String.h" + +namespace Gedeng { + +class NodeSystem { + // Load a node with all its children from a node definition file (`.gnd`) on + // disk. + static Node load_nodes_from_disk(const String &path); + + // Save a node and all its children to disk, at the given path. It is + // recommended to end the path with `.gnd`. + static void save_nodes_to_disk(const Node &node, const String &path); +}; + +} // namespace Gedeng \ No newline at end of file diff --git a/include/Gedeng/Path.h b/include/Gedeng/Path.h new file mode 100644 index 0000000..8b53c85 --- /dev/null +++ b/include/Gedeng/Path.h @@ -0,0 +1,15 @@ +#pragma once +#include "Vector3.h" + +namespace Gedeng { + +class Path { + // Return a Vector3 on the path. `t` should be between 0 (path begin) and 1 + // (path end). + Vector3 get_interpolated_position(float t) const; + + // Return the number of Vector3's which make up the path. + unsigned int get_size() const; +}; + +} // namespace Gedeng diff --git a/include/Gedeng/Vector3.h b/include/Gedeng/Vector3.h new file mode 100644 index 0000000..079c9ce --- /dev/null +++ b/include/Gedeng/Vector3.h @@ -0,0 +1,7 @@ +#pragma once + +namespace Gedeng { + +class Vector3 {}; + +} // namespace Gedeng \ No newline at end of file