Add basic pathfinding API

This should cover all requirements from the assignment, but a more specific use case will be added later.
This commit is contained in:
karl 2021-03-31 14:49:25 +02:00
parent 1c9b58207e
commit 90d8b0479d
5 changed files with 82 additions and 0 deletions

28
include/Gedeng/NavMesh.h Normal file
View File

@ -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

15
include/Gedeng/Node.h Normal file
View File

@ -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<Node &> get_children() const;
};
} // namespace Gedeng

View File

@ -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

15
include/Gedeng/Path.h Normal file
View File

@ -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

7
include/Gedeng/Vector3.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
namespace Gedeng {
class Vector3 {};
} // namespace Gedeng