aboutsummaryrefslogtreecommitdiff
path: root/src/shared/movement/movement.hh
blob: 9c6e17d45eb384a2ffaf990a991988b504372bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef SHARED_MOVEMENT_MOVEMENT_HH_
#define SHARED_MOVEMENT_MOVEMENT_HH_

#include "shared/entity/animate.hh"
#include "shared/entity/moveable.hh"
#include "shared/math/math.hh"
#include "shared/movement/struct.hh"
#include "shared/shared.hh"
#include "shared/world/chunk.hh"

#include <algorithm>
#include <array>
#include <cstdint>

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtx/norm.hpp>

namespace shared {
namespace movement {

// Returns a local pos relative to the base chunk pos, which can be negative,
// or over 16.0f.
glm::vec3 make_relative(const shared::math::coords& base_chunk_pos,
                        const glm::vec3& other_local_pos,
                        const shared::math::coords& other_chunk_pos) noexcept;

void normalise_position(glm::vec3& local_pos,
                        shared::math::coords& chunk_pos) noexcept;
bool intersect_aabbs(const aabb& a, const aabb& b) noexcept;

struct line {
    glm::vec3 origin;
    glm::vec3 dir;
};
struct ray_aabb_ret {
    glm::vec3 position;
    float time;
    glm::vec3 normal;
};
std::optional<ray_aabb_ret> intersect_ray_aabb(const line& line,
                                               const aabb& aabb) noexcept;
struct moving_aabb {
    struct aabb aabb;
    glm::vec3 velocity;
};
struct moving_aabb_ret {
    float time;
    glm::vec3 normal;
};
std::optional<moving_aabb_ret>
intersect_moving_aabbs(const moving_aabb& a, const moving_aabb& b) noexcept;

// We need more information about the block during movement code.
struct block {
    shared::world::block block;
    struct aabb aabb;
    shared::math::coords chunk_pos;
    glm::ivec3 pos;
};
using blocks = std::vector<block>;

// Returns a vec2 describing how wide and tall the array of blocks should be (to
// ensure you never fall through anything at variable tickrates).
glm::ivec2 get_move_xy(const std::uint32_t& tickrate,
                       const moveable& moveable) noexcept;

[[nodiscard]] shared::animate move(const shared::moveable& moveable,
                                   const blocks& blocks,
                                   const std::uint32_t& tickrate) noexcept;

} // namespace movement
} // namespace shared

#endif