VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
collisionComponent.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
4
6#include "framework/entity.h"
7
8namespace visutwin::canvas
9{
15
17 {
18 std::erase(_instances, this);
19 }
20
22 {
23 if (!entity()) {
24 return BoundingSphere(Vector3(0.0f, 0.0f, 0.0f), _radius);
25 }
26
27 // DEVIATION: collision shape primitives are approximated from render bounds until dedicated
28 // Ammo/Bullet collision shape generation is ported.
29 if (const auto* render = entity()->findComponent<RenderComponent>(); render && !render->meshInstances().empty()) {
30 bool hasBounds = false;
31 BoundingBox merged;
32 for (auto* meshInstance : render->meshInstances()) {
33 if (!meshInstance) {
34 continue;
35 }
36 const BoundingBox worldAabb = meshInstance->aabb();
37 if (!hasBounds) {
38 merged = worldAabb;
39 hasBounds = true;
40 } else {
41 merged.add(worldAabb);
42 }
43 }
44
45 if (hasBounds) {
46 return BoundingSphere(merged.center(), std::max(merged.halfExtents().length(), 0.001f));
47 }
48 }
49
50 return BoundingSphere(entity()->position(), std::max(_halfExtents.length(), _radius));
51 }
52}
Axis-Aligned Bounding Box defined by center and half-extents.
Definition boundingBox.h:21
void add(const BoundingBox &other)
const Vector3 & center() const
Definition boundingBox.h:27
const Vector3 & halfExtents() const
Definition boundingBox.h:35
Bounding sphere defined by center and radius for intersection and containment tests.
CollisionComponent(IComponentSystem *system, Entity *entity)
Entity * entity() const
Definition component.cpp:16
Component(IComponentSystem *system, Entity *entity)
Definition component.cpp:12
IComponentSystem * system() const
Definition component.h:47
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
3D vector for positions, directions, and normals with multi-backend SIMD acceleration.
Definition vector3.h:29
float length() const
Definition vector3.h:224