Cogs.Core
CollisionSystem.cpp
1#include "CollisionSystem.h"
2
3#include "Resources/Model.h"
4#include "Resources/Mesh.h"
5
6#include "Components/Core/MeshComponent.h"
7
8#include "PhysicsManager.h"
9
11{
12 for (auto & c : pool) {
13 auto & data = getData(&c);
14
15 if (c.hasChanged() || !data.collisionShape) {
16 data.collisionShape = std::make_unique<btStaticPlaneShape>(btVector3(0, 0, 1), 0.0f);
17 }
18
19 c.collisionShape = data.collisionShape.get();
20 }
21}
22
24{
25 for (auto & c : pool) {
26 auto & data = getData(&c);
27
28 if (c.hasChanged() || !data.collisionShape) {
29 data.collisionShape = std::make_unique<btSphereShape>(c.radius);
30 }
31
32 c.collisionShape = data.collisionShape.get();
33 }
34}
35
37{
38 for (auto & c : pool) {
39 auto & data = getData(&c);
40
41 if (c.hasChanged() || !data.collisionShape) {
42 data.collisionShape = std::make_unique<btBoxShape>(toBullet(c.halfSize));
43 }
44
45 c.collisionShape = data.collisionShape.get();
46 }
47}
48
50{
51 for (auto & c : pool) {
52 auto & data = getData(&c);
53
54 if (c.hasChanged() || !data.collisionShape) {
55 data.collisionShape = std::make_unique<btCapsuleShapeZ>(c.radius, c.height);
56 }
57
58 c.collisionShape = data.collisionShape.get();
59 }
60}
61
63{
64 for (auto & c : pool) {
65 auto & data = getData(&c);
66
67 if (c.hasChanged() || !data.collisionShape) {
68 data.collisionShape = std::make_unique<btCylinderShapeZ>(btVector3(c.radius, c.radius, c.height));
69 }
70
71 c.collisionShape = data.collisionShape.get();
72 }
73}
74
76{
77 for (auto & c : pool) {
78 auto & data = getData(&c);
79
80 if (c.hasChanged() || !data.collisionShape) {
81 data.collisionShape = std::make_unique<btEmptyShape>();
82 }
83
84 c.collisionShape = data.collisionShape.get();
85 }
86}
87
88namespace Cogs::Core
89{
90 ModelPart * findFirst(Model & model)
91 {
92 for (auto & part : model.parts) {
93 if (part.meshIndex != static_cast<uint32_t>(-1)) return &part;
94 }
95
96 return nullptr;
97 }
98}
99
101{
102 for (auto & c : pool) {
103 auto & data = getData(&c);
104
105 if (c.hasChanged() || !data.collisionShape) {
106 Mesh * mesh = nullptr;
107 glm::mat4 transform(1.0f);
108
109 if (c.mesh) {
110 mesh = c.mesh.resolve();
111 } else if (c.model) {
112 auto model = c.model.resolve();
113
114 if (model->isLoaded() && model->parts.size()) {
115 auto part = findFirst(*model);
116
117 if (part) {
118 mesh = model->meshes[part->meshIndex].resolve();
119 transform = model->getPartTransform(*part);
120 }
121 }
122 } else {
123 auto mc = c.getComponent<MeshComponent>();
124
125 if (mc && mc->meshHandle) {
126 mesh = mc->meshHandle.resolve();
127 }
128 }
129
130 if (mesh) {
131 data.mesh = std::make_unique<btTriangleMesh>();
132
133 auto [vertexData, count, stride] = mesh->getPositionStream();
134
135 auto indexes = mesh->getIndexes();
136 if (indexes.size()) {
137 const size_t faces = indexes.size() / 3;
138
139 for (size_t i = 0; i < faces; ++i) {
140 auto v1 = *reinterpret_cast<const glm::vec3 *>(vertexData + indexes[i * 3 + 0] * stride);
141 auto v2 = *reinterpret_cast<const glm::vec3 *>(vertexData + indexes[i * 3 + 1] * stride);
142 auto v3 = *reinterpret_cast<const glm::vec3 *>(vertexData + indexes[i * 3 + 2] * stride);
143
144 v1 = glm::vec3(transform * glm::vec4(v1, 1));
145 v2 = glm::vec3(transform * glm::vec4(v2, 1));
146 v3 = glm::vec3(transform * glm::vec4(v3, 1));
147
148 data.mesh->addTriangle(
149 toBullet(v1),
150 toBullet(v2),
151 toBullet(v3)
152 );
153 }
154
155 data.collisionShape = std::make_unique<btBvhTriangleMeshShape>(data.mesh.get(), false);
156 }
157 }
158 }
159
160 c.collisionShape = data.collisionShape.get();
161 }
162}
void update()
Updates the system state to that of the current frame.
ComponentPool< ComponentType > pool
Pool of components managed by the system.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Contains a handle to a Mesh resource to use when rendering using the MeshRenderComponent.
Definition: MeshComponent.h:15
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Meshes contain streams of vertex data in addition to index data and options defining geometry used fo...
Definition: Mesh.h:265
StreamReference getPositionStream()
Get the data of the stream containing positions.
Definition: Mesh.cpp:136
Model resources define a template for a set of connected entities, with resources such as meshes,...
Definition: Model.h:56