Antkeeper  0.0.1
flee.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 
21 
22 namespace ai {
23 namespace steering {
24 namespace behavior {
25 
27 {
28  math::fvec3 force = {0, 0, 0};
31 
32  if (sqr_distance)
33  {
34  const float inverse_distance = 1.0f / std::sqrt(sqr_distance);
35  force = difference * inverse_distance * agent.max_force;
36  force = agent.velocity - force;
37  }
38 
39  return force;
40 }
41 
42 } // namespace behavior
43 } // namespace steering
44 } // namespace ai
math::fvec3 flee(const agent &agent, const math::fvec3 &target)
Attempts to steer an agent so that it moves away from a target.
Definition: flee.cpp:26
Artificial intelligence (AI)
Definition: ai.hpp:24
constexpr int difference(T x, T y) noexcept
Returns the number of differing bits between two values, known as Hamming distance.
Definition: bit-math.hpp:280
constexpr T sqr_distance(const vector< T, N > &p0, const vector< T, N > &p1) noexcept
Calculates the square distance between two points.
Definition: vector.hpp:1514
vector< T, N > sqrt(const vector< T, N > &x)
Takes the square root of each element.
constexpr T dot(const quaternion< T > &a, const quaternion< T > &b) noexcept
Calculates the dot product of two quaternions.
Definition: quaternion.hpp:572
Autonomous agent governed by steering behaviors.
Definition: agent.hpp:33
float max_force
Maximum force.
Definition: agent.hpp:47
math::fvec3 position
Cartesian position vector.
Definition: agent.hpp:38
math::fvec3 velocity
Velocity vector.
Definition: agent.hpp:41
n-dimensional vector.
Definition: vector.hpp:44