Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
CapsuleCollider.hpp
Go to the documentation of this file.
1/**************************************************************************
2 * EngineSquared v0.1.1
3 *
4 * EngineSquared is a software package, part of the Engine² organization.
5 *
6 * This file is part of the EngineSquared project that is under MIT License.
7 * Copyright © 2025-present by @EngineSquared, All rights reserved.
8 *
9 * EngineSquared is a free software: you can redistribute it and/or modify
10 * it under the terms of the MIT License. See the project's LICENSE file for
11 * the full license text and details.
12 *
13 * @file CapsuleCollider.hpp
14 * @brief Capsule collision shape component
15 *
16 * User-specified capsule collider for physics simulation.
17 * Capsules are ideal for characters and cylindrical objects.
18 *
19 * @author @EngineSquared
20 * @version 0.1.1
21 * @date 2025-12-05
22 **************************************************************************/
23
24#pragma once
25
26#include <glm/vec3.hpp>
27
28namespace Physics::Component {
29
65 float halfHeight = 0.5f;
66
68 float radius = 0.25f;
69
71 glm::vec3 offset{0.0f, 0.0f, 0.0f};
72
76 CapsuleCollider() = default;
77
83 CapsuleCollider(float hHeight, float r) : halfHeight(hHeight), radius(r) {}
84
91 CapsuleCollider(float hHeight, float r, const glm::vec3 &localOffset)
92 : halfHeight(hHeight), radius(r), offset(localOffset)
93 {
94 }
95
100 [[nodiscard]] float GetTotalHeight() const { return (halfHeight + radius) * 2.0f; }
101
106 [[nodiscard]] float GetCylinderHeight() const { return halfHeight * 2.0f; }
107
112 [[nodiscard]] float GetDiameter() const { return radius * 2.0f; }
113
120 void SetHeight(float capsuleHeight)
121 {
122 halfHeight = (capsuleHeight * 0.5f) - radius;
123 if (halfHeight < 0.0f)
124 halfHeight = 0.0f;
125 }
126
130 [[nodiscard]] bool IsValid() const { return halfHeight >= 0.0f && radius > 0.0f; }
131
136 [[nodiscard]] bool IsSphere() const { return halfHeight == 0.0f; }
137
148 static CapsuleCollider CreateFromHeight(float capsuleHeight, float r)
149 {
150 float hHeight = (capsuleHeight * 0.5f) - r;
151 return CapsuleCollider(hHeight > 0.0f ? hHeight : 0.0f, r);
152 }
153
161 static CapsuleCollider AtOffset(float hHeight, float r, const glm::vec3 &localOffset)
162 {
163 return CapsuleCollider(hHeight, r, localOffset);
164 }
165};
166
167} // namespace Physics::Component
Definition BoxCollider.hpp:27
bool IsSphere() const
Check if this capsule degenerates to a sphere.
Definition CapsuleCollider.hpp:136
float GetCylinderHeight() const
Get height of just the cylindrical part.
Definition CapsuleCollider.hpp:106
float GetTotalHeight() const
Get total height of the capsule.
Definition CapsuleCollider.hpp:100
CapsuleCollider()=default
Default constructor.
static CapsuleCollider AtOffset(float hHeight, float r, const glm::vec3 &localOffset)
Create a capsule at an offset (useful for compound shapes).
Definition CapsuleCollider.hpp:161
glm::vec3 offset
Local offset from entity transform (center of capsule).
Definition CapsuleCollider.hpp:71
static CapsuleCollider CreateFromHeight(float capsuleHeight, float r)
Create a capsule from total height and radius.
Definition CapsuleCollider.hpp:148
bool IsValid() const
Check if collider is valid.
Definition CapsuleCollider.hpp:130
float GetDiameter() const
Get diameter of the capsule.
Definition CapsuleCollider.hpp:112
float radius
Radius of the capsule (both cylinder and hemisphere caps).
Definition CapsuleCollider.hpp:68
void SetHeight(float capsuleHeight)
Set height (distributes between cylinder and caps).
Definition CapsuleCollider.hpp:120
float halfHeight
Half height of the cylindrical part (distance from center to cap start).
Definition CapsuleCollider.hpp:65
CapsuleCollider(float hHeight, float r)
Construct with half-height and radius.
Definition CapsuleCollider.hpp:83
CapsuleCollider(float hHeight, float r, const glm::vec3 &localOffset)
Construct with half-height, radius and offset.
Definition CapsuleCollider.hpp:91