v0.3.0 API Reference
PhysicsShape.h
1//
2// PhysicsShape.h
3// avara3d
4//
5// Created by Morgan Davis on 1/25/18.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_PHYSICS_SHAPE_PHYSICSSHAPE_H
10#define AVARA3D_PHYSICS_SHAPE_PHYSICSSHAPE_H
11
12#include <cstdint>
13#include <memory>
14#include <optional>
15#include <unordered_set>
16#include <variant>
17
18#include "a3d/physics/PhysicsBody.h"
19
20namespace a3d {
21
22class Mesh;
23class Node;
24class PhysicsWorld;
25class PhysicsShapeProxy;
26class Scene;
27
44
45public:
46 // [Public Types]
47
49 enum class Type : uint8_t {
50 // TODO: change this
51 Primitive,
52 BoundingBox,
53 ConvexHull,
54 ConcavePolyhedron
55 };
56
58 using Source = std::variant<std::monostate, std::weak_ptr<Mesh>, std::weak_ptr<Node>>;
59
60 // [Public Static Member Functions]
61
63 static std::shared_ptr<PhysicsShape> BoundingBoxShape(const std::shared_ptr<Mesh>& mesh);
64
66 static std::shared_ptr<PhysicsShape> BoundingBoxShape(const std::shared_ptr<Node>& node);
67
69 static std::shared_ptr<PhysicsShape> ConvexHullShape(const std::shared_ptr<Mesh>& mesh);
70
72 static std::shared_ptr<PhysicsShape> ConvexHullShape(const std::shared_ptr<Node>& node);
73
75 static std::shared_ptr<PhysicsShape> ConcavePolyhedronShape(const std::shared_ptr<Mesh>& mesh);
76
78 static std::shared_ptr<PhysicsShape> ConcavePolyhedronShape(const std::shared_ptr<Node>& node);
79
80 // [Public Lifecycle Functions]
81
87 PhysicsShape(Type type, const std::shared_ptr<Mesh>& mesh);
88
95 PhysicsShape(Type type, const std::shared_ptr<Node>& node);
96
97 PhysicsShape(const PhysicsShape&) = delete;
98 PhysicsShape& operator=(const PhysicsShape&) = delete;
99
100 PhysicsShape(PhysicsShape&&) = delete;
101 PhysicsShape& operator=(PhysicsShape&&) = delete;
102
103 virtual ~PhysicsShape();
104
105 // [Public Member Functions]
106
108 virtual Type type() const;
109
117 virtual void type(Type type);
118
131 float margin() const;
132
147 void margin(float margin);
148
150 Source source() const;
151
152 // [Internal Member Functions]
153
154 virtual bool supportsBodyType(PhysicsBody::Type type) const;
155 virtual bool supportsMargin() const;
156
157 void attachedToBody(PhysicsBody& body);
158 void detachedFromBody(PhysicsBody& body);
159
160 void physicsWorldReachable(PhysicsWorld& world);
161 void physicsWorldUnreachable(PhysicsWorld& world);
162
163 void source(const Source& sourceObject);
164
165 void checkCreateProxy();
166
167 const std::unordered_set<PhysicsBody*>& bodies() const;
168
169 const std::optional<float>& marginOverride() const;
170
171 PhysicsShapeProxy* proxy() const;
172
173protected:
174 // [Protected Lifecycle Functions]
175
176 PhysicsShape();
177
178 // [Protected Member Variables]
179
180 Type _type;
181 std::unique_ptr<PhysicsShapeProxy> _proxy;
182
183private:
184 // [Private Member Variables]
185
186 std::optional<float> _margin;
187 Source _source;
188 std::unordered_set<PhysicsBody*> _bodies;
189};
190
191} // namespace a3d
192
193#endif // AVARA3D_PHYSICS_SHAPE_PHYSICSSHAPE_H
Rigid-body simulation state attached to a Node.
Definition: PhysicsBody.h:39
Type
Determines how a rigid body participates in simulation.
Definition: PhysicsBody.h:45
Describes collision geometry used by one or more PhysicsBody objects.
Definition: PhysicsShape.h:43
PhysicsShape(Type type, const std::shared_ptr< Mesh > &mesh)
Creates a PhysicsShape of type derived from mesh.
virtual Type type() const
Returns the collision-geometry type.
static std::shared_ptr< PhysicsShape > ConcavePolyhedronShape(const std::shared_ptr< Node > &node)
Creates a concave-polyhedron collision shape from mesh geometry in the node hierarchy.
static std::shared_ptr< PhysicsShape > BoundingBoxShape(const std::shared_ptr< Node > &node)
Creates a bounding-box collision shape from mesh geometry in the node hierarchy.
std::variant< std::monostate, std::weak_ptr< Mesh >, std::weak_ptr< Node > > Source
Weak source object used to derive collision geometry, or no source for primitive shapes.
Definition: PhysicsShape.h:58
Source source() const
Returns the weak Mesh or Node source, or std::monostate for a source-less primitive shape.
virtual void type(Type type)
Validates the requested collision-geometry type.
void margin(float margin)
Sets a uniform collision margin for this shape.
static std::shared_ptr< PhysicsShape > ConcavePolyhedronShape(const std::shared_ptr< Mesh > &mesh)
Creates a concave-polyhedron collision shape derived from mesh.
static std::shared_ptr< PhysicsShape > BoundingBoxShape(const std::shared_ptr< Mesh > &mesh)
Creates a bounding-box collision shape derived from mesh.
Type
Collision-geometry representation requested for a PhysicsShape.
Definition: PhysicsShape.h:49
static std::shared_ptr< PhysicsShape > ConvexHullShape(const std::shared_ptr< Mesh > &mesh)
Creates a convex-hull collision shape derived from mesh.
static std::shared_ptr< PhysicsShape > ConvexHullShape(const std::shared_ptr< Node > &node)
Creates a convex-hull collision shape from mesh geometry in the node hierarchy.
PhysicsShape(Type type, const std::shared_ptr< Node > &node)
Creates a PhysicsShape of type from mesh geometry in the node hierarchy.
float margin() const
Returns the collision margin used by this shape.
Simulates rigid bodies and performs physics collision queries for a Scene.
Definition: PhysicsWorld.h:39