]> git.localhorst.tv Git - blank.git/blob - src/world/EntityController.hpp
simplify ray/chunk intersection test
[blank.git] / src / world / EntityController.hpp
1 #ifndef BLANK_WORLD_ENTITYCONTROLLER_HPP_
2 #define BLANK_WORLD_ENTITYCONTROLLER_HPP_
3
4 #include "EntityState.hpp"
5
6 #include <glm/glm.hpp>
7
8
9 namespace blank {
10
11 class Entity;
12
13 struct EntityController {
14
15         virtual ~EntityController();
16
17         virtual void Update(Entity &, float dt) = 0;
18
19         virtual glm::vec3 ControlForce(const Entity &, const EntityState &) const = 0;
20
21
22         /// try to add as much of add to out so it doesn't exceed max
23         /// returns true if it's maxed out
24         static bool MaxOutForce(
25                 glm::vec3 &out,
26                 const glm::vec3 &add,
27                 float max
28         ) noexcept;
29         /// give a force that makes state come to a halt over 1/n seconds
30         static inline glm::vec3 Halt(
31                 const EntityState &state,
32                 float n
33         ) noexcept {
34                 return state.velocity * -n;
35         }
36         /// give a force that makes state's velocity converge with given
37         /// target velocity over 1/n seconds
38         static inline glm::vec3 TargetVelocity(
39                 const glm::vec3 &target,
40                 const EntityState &state,
41                 float n
42         ) noexcept {
43                 return (target - state.velocity) * n;
44         }
45         /// give a force that makes state go after target with an attempted
46         /// acceleration over 1/n seconds and a speed of s
47         static inline glm::vec3 Seek(
48                 const EntityState &state,
49                 const EntityState &target,
50                 float s,
51                 float n
52         ) noexcept {
53                 return TargetVelocity(normalize(target.Diff(state)) * s, state, n);
54         }
55         /// opposite of seek
56         static inline glm::vec3 Flee(
57                 const EntityState &state,
58                 const EntityState &target,
59                 float s,
60                 float n
61         ) noexcept {
62                 return TargetVelocity(normalize(state.Diff(target)) * s, state, n);
63         }
64
65 };
66
67 }
68
69 #endif