vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
collider.h
1#pragma once
2
3#include <cstdint>
4#include <vector>
5
6#include <glm/glm.hpp>
7
8#include "core/reflect.h"
9#include "resource/asset/mesh_asset.h"
10
11namespace Vkm::Engine {
12
18enum class ColliderShape : uint8_t {
19 Box = 0,
20 Capsule = 1,
21 Mesh = 2,
22 Terrain = 3,
23 Count
24};
25
33inline bool isTriangles(ColliderShape shape) {
34 return shape == ColliderShape::Mesh || shape == ColliderShape::Terrain;
35}
36
45 ColliderShape shape = ColliderShape::Box;
46 glm::vec3 center = {0.0f, 0.0f, 0.0f};
47 glm::vec3 halfExtents = {0.5f, 0.5f, 0.5f};
48 float radius = 0.5f;
49 float halfHeight = 0.5f;
50 MeshHandle mesh = {};
52 glm::vec3 meshScale = {1.0f, 1.0f, 1.0f};
53};
54
60struct MeshNode {
61 glm::vec3 min = {0.0f, 0.0f, 0.0f};
62 glm::vec3 max = {0.0f, 0.0f, 0.0f};
63
64 uint32_t firstTriangle = 0;
65 uint32_t triangleCount = 0;
66 uint32_t rightChild = 0;
67};
68
73 uint64_t uid = 0;
74 uint64_t version = 0;
75 glm::vec3 scale = {0.0f, 0.0f, 0.0f};
76};
77
84struct Collider {
86 std::vector<ColliderPart> parts = { ColliderPart{} };
87
96 std::vector<glm::vec3> meshPoints;
97
98 std::vector<MeshNode> meshNodes;
100
101 bool isTrigger = false;
103 bool enabled = true;
104};
105
106} // namespace Vkm::Engine
107
108VKM_ENUM_NAMES(::Vkm::Engine::ColliderShape, "Box", "Capsule", "Mesh", "Terrain")
109
110// Every shape's fields are reflected whatever the tag, so switching a part's shape and back keeps
111// what it was authored with.
112VKM_REFLECT_BEGIN(::Vkm::Engine::ColliderPart)
113 VKM_F(shape)
114 VKM_F(center)
115 VKM_F(halfExtents)
116 VKM_F(radius)
117 VKM_F(halfHeight)
118 VKM_F(mesh)
119 VKM_F(meshScale)
120VKM_REFLECT_END()
121
122VKM_REFLECT_BEGIN(::Vkm::Engine::Collider)
123 VKM_F(isTrigger)
124 VKM_F(enabled)
125 VKM_F(parts)
126VKM_REFLECT_END()
One primitive of a collider, in the entity's local frame.
Definition collider.h:44
ColliderShape shape
Which fields below are live.
Definition collider.h:45
float halfHeight
Capsule: half the segment, caps excluded.
Definition collider.h:49
glm::vec3 halfExtents
Box: half-sizes.
Definition collider.h:47
float radius
Capsule: sweep radius.
Definition collider.h:48
MeshHandle mesh
Definition collider.h:50
glm::vec3 center
Local offset from the entity origin.
Definition collider.h:46
glm::vec3 meshScale
Mesh: applied to every vertex, as Transform scale is not.
Definition collider.h:52
Collision geometry attached to an entity, evaluated in its Transform frame.
Definition collider.h:84
std::vector< MeshNode > meshNodes
Bounding hierarchy over meshPoints, built with them.
Definition collider.h:98
std::vector< ColliderPart > parts
One or more parts; a single unit box by default.
Definition collider.h:86
MeshColliderSource meshBuiltFrom
What the two above were built from.
Definition collider.h:99
std::vector< glm::vec3 > meshPoints
The mesh or terrain part's triangle corners, three a triangle, in the part's frame; a mesh's scaled b...
Definition collider.h:96
bool isTrigger
Definition collider.h:101
bool enabled
False: inert - no broadphase entry, no contacts, no debug draw.
Definition collider.h:103
What a Collider's triangles were built from, so a change to any of it rebuilds them.
Definition collider.h:72
uint64_t uid
The mesh or terrain asset, by Resource::uid; zero for none.
Definition collider.h:73
glm::vec3 scale
The part's meshScale when built.
Definition collider.h:75
uint64_t version
Its Resource::version when built.
Definition collider.h:74
One node of a triangle mesh's bounding hierarchy.
Definition collider.h:60
uint32_t firstTriangle
Leaf: first triangle; interior: unused.
Definition collider.h:64
uint32_t rightChild
Interior: the far child; left is this + 1.
Definition collider.h:66
uint32_t triangleCount
0 marks an interior node
Definition collider.h:65
Component representing a renderable mesh (geometry + material) in the world.
Definition mesh.h:34
A terrain in the world: its asset, centred on the entity.
Definition terrain.h:15