Antkeeper  0.0.1
wander.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
22 #include <engine/math/random.hpp>
24 
25 namespace ai {
26 namespace steering {
27 namespace behavior {
28 
29 math::fvec3 wander_2d(const agent& agent, float noise, float distance, float radius, float& angle)
30 {
31  // Shift wander angle
32  angle += math::random(-noise, noise);
33 
34  // Calculate center of wander circle
35  const math::fvec3 center = agent.position + agent.forward * distance;
36 
37  // Decompose orientation into swing and twist rotations
38  auto [swing, twist] = math::swing_twist(agent.orientation, agent.up);
39 
40  // Calculate offset to point on wander circle
41  const math::fvec3 offset = (math::angle_axis(angle, agent.up) * agent.forward * radius) * twist;
42 
43  // Seek toward point on wander circle
44  return seek(agent, center + offset);
45 }
46 
47 math::fvec3 wander_3d(const agent& agent, float noise, float distance, float radius, float& theta, float& phi)
48 {
49  // Shift wander angles
50  theta += math::random(-noise, noise);
51  phi += math::random(-noise, noise);
52 
53  // Calculate center of wander sphere
54  const math::fvec3 center = agent.position + agent.forward * distance;
55 
56  // Convert spherical coordinates to Cartesian point on wander sphere
57  const float r_cos_theta = radius * std::cos(theta);
58  const math::fvec3 offset =
59  {
60  r_cos_theta * std::cos(phi),
61  r_cos_theta * std::sin(phi),
62  radius * std::sin(theta)
63  };
64 
65  // Seek toward point on wander sphere
66  return seek(agent, center + offset);
67 }
68 
69 } // namespace behavior
70 } // namespace steering
71 } // namespace ai
math::fvec3 wander_2d(const agent &agent, float noise, float distance, float radius, float &angle)
Steers an agent in a continuously shifting random direction on the yaw plane.
Definition: wander.cpp:29
math::fvec3 wander_3d(const agent &agent, float noise, float distance, float radius, float &theta, float &phi)
Steers an agent in a continuously shifting random direction.
Definition: wander.cpp:47
math::fvec3 seek(const agent &agent, const math::fvec3 &target)
Attempts to steer an agent so that it moves toward a target.
Definition: seek.cpp:26
Artificial intelligence (AI)
Definition: ai.hpp:24
constexpr T phi
Golden ratio constant.
Definition: numbers.hpp:109
T angle(const vector< T, N > &from, const vector< T, N > &to)
Calculates the angle between two direction vectors.
Definition: vector.hpp:1030
quaternion< T > angle_axis(T angle, const vec3< T > &axis)
Creates a rotation from an angle and axis.
Definition: quaternion.hpp:685
T random(T start, T end)
Generates a pseudo-random floating point number on [start, end) using std::rand().
Definition: random.hpp:38
T distance(const vector< T, N > &p0, const vector< T, N > &p1)
Calculates the distance between two points.
Definition: vector.hpp:1106
std::array< quaternion< T >, 2 > swing_twist(const quaternion< T > &q, const vec3< T > &twist_axis, T tolerance=T{1e-6})
Decomposes a quaternion into swing and twist rotation components.
Definition: quaternion.hpp:768
T offset(T longitude)
Calculates the UTC offset at a given longitude.
Definition: utc.hpp:38
Autonomous agent governed by steering behaviors.
Definition: agent.hpp:33
math::fquat orientation
Orientation quaternion.
Definition: agent.hpp:56
math::fvec3 up
Orthonormal basis up direction vector.
Definition: agent.hpp:62
math::fvec3 position
Cartesian position vector.
Definition: agent.hpp:38
math::fvec3 forward
Orthonormal basis forward direction vector.
Definition: agent.hpp:59
n-dimensional vector.
Definition: vector.hpp:44