Avara3D 0.2.0
C++ 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 <unordered_set>
15#include <variant>
16
17#include "a3d/physics/PhysicsBody.h"
18
19namespace a3d {
20
21 class Mesh;
22 class Node;
23 class PhysicsWorld;
24 class PhysicsShapeProxy;
25 class Scene;
26
38
39 public:
40 // [Public Types]
41
43 enum class Type : uint8_t {
44 // TODO: change this
45 Primitive,
46 BoundingBox,
47 ConvexHull,
48 ConcavePolyhedron
49 };
50
52 using Source = std::variant<std::monostate, std::weak_ptr<Mesh>, std::weak_ptr<Node>>;
53
54 // [Public Static Member Functions]
55
57 static std::shared_ptr<PhysicsShape> BoundingBoxShape(const std::shared_ptr<Mesh>& mesh);
58
60 static std::shared_ptr<PhysicsShape> BoundingBoxShape(const std::shared_ptr<Node>& node);
61
63 static std::shared_ptr<PhysicsShape> ConvexHullShape(const std::shared_ptr<Mesh>& mesh);
64
66 static std::shared_ptr<PhysicsShape> ConvexHullShape(const std::shared_ptr<Node>& node);
67
69 static std::shared_ptr<PhysicsShape> ConcavePolyhedronShape(const std::shared_ptr<Mesh>& mesh);
70
72 static std::shared_ptr<PhysicsShape> ConcavePolyhedronShape(const std::shared_ptr<Node>& node);
73
74 // [Public Lifecycle Functions]
75
81 PhysicsShape(Type type, const std::shared_ptr<Mesh>& mesh);
82
89 PhysicsShape(Type type, const std::shared_ptr<Node>& node);
90
91 PhysicsShape(const PhysicsShape&) = delete;
92 PhysicsShape& operator=(const PhysicsShape&) = delete;
93
94 PhysicsShape(PhysicsShape&&) = delete;
95 PhysicsShape& operator=(PhysicsShape&&) = delete;
96
97 virtual ~PhysicsShape();
98
99 // [Public Member Functions]
100
102 virtual Type type() const;
103
111 virtual void type(Type type);
112
114 Source source() const;
115
116 // [Internal Member Functions]
117
118 virtual bool supportsBodyType(PhysicsBody::Type type) const;
119
120 void attachedToBody(PhysicsBody& body);
121 void detachedFromBody(PhysicsBody& body);
122
123 void physicsWorldReachable(PhysicsWorld& world);
124 void physicsWorldUnreachable(PhysicsWorld& world);
125
126 void source(const Source& sourceObject);
127
128 void checkCreateProxy();
129
130 const std::unordered_set<PhysicsBody*>& bodies() const;
131
132 PhysicsShapeProxy* proxy() const;
133
134 protected:
135 // [Protected Lifecycle]
136
137 PhysicsShape();
138
139 // [Protected Member Variables]
140
141 Type _type;
142 std::unique_ptr<PhysicsShapeProxy> _proxy;
143
144 private:
145 // [Private Member Variables]
146
147 Source _source;
148 std::unordered_set<PhysicsBody*> _bodies;
149 };
150
151}
152
153#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:37
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:52
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.
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:43
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.
Simulates rigid bodies and performs physics collision queries for a Scene.
Definition: PhysicsWorld.h:39