StevEngine
StevEngine is a customizable C++ game engine.
Loading...
Searching...
No Matches
Layers.hpp
1#pragma once
2#ifdef StevEngine_PHYSICS
3#include <unordered_map>
4
5#include "Jolt.h"
6#include <Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h>
7
8
9namespace StevEngine::Physics {
10 typedef JPH::ObjectLayer LayerID;
11
12 namespace BroadPhaseLayers
13 {
15 static constexpr JPH::BroadPhaseLayer NON_MOVING(0);
17 static constexpr JPH::BroadPhaseLayer MOVING(1);
18 };
19
26 class Layer {
27 friend class LayerManager;
28
29 public:
30 const bool isStatic;
31 const LayerID id;
32 const JPH::BroadPhaseLayer BroadPhaseLayer;
33
38 Layer(const Layer& copy);
39
40 private:
46 Layer(LayerID id, bool isStatic = false);
47 };
48
54 class LayerManager {
55 public:
56 enum DefaultLayers {
57 STATIC = (LayerID)0,
58 DEFAULT = (LayerID)1,
59 };
60
61 LayerManager();
62
68 LayerID CreateLayer(bool isStatic = true);
69
75 const Layer& GetLayer(LayerID id);
76
77 private:
78 std::unordered_map<LayerID, Layer> layers;
79
80 LayerID currentId;
81 };
82
83 // Jolt management
84
91 class BPLayerInterfaceImpl final : public JPH::BroadPhaseLayerInterface {
92 public:
97
102 virtual unsigned int GetNumBroadPhaseLayers() const override;
103
109 virtual JPH::BroadPhaseLayer GetBroadPhaseLayer(JPH::ObjectLayer inLayer) const override;
110 };
111
117 class ObjectVsBroadPhaseLayerFilterImpl : public JPH::ObjectVsBroadPhaseLayerFilter
118 {
119 public:
126 virtual bool ShouldCollide(JPH::ObjectLayer inLayer1, JPH::BroadPhaseLayer inLayer2) const override;
127 };
128
134 class ObjectLayerPairFilterImpl : public JPH::ObjectLayerPairFilter
135 {
136 public:
143 virtual bool ShouldCollide(JPH::ObjectLayer inObject1, JPH::ObjectLayer inObject2) const override;
144 };
145}
146
147template<> struct std::hash<StevEngine::Physics::Layer> {
153 std::size_t operator()(const StevEngine::Physics::Layer& k) const {
154 return std::hash<StevEngine::Physics::LayerID>()(k.id);
155 }
156};
157#endif
virtual unsigned int GetNumBroadPhaseLayers() const override
Get number of broad phase layers.
Definition Layers.cpp:33
virtual JPH::BroadPhaseLayer GetBroadPhaseLayer(JPH::ObjectLayer inLayer) const override
Get broad phase layer for object layer.
Definition Layers.cpp:36
BPLayerInterfaceImpl()
Create broad phase interface.
Definition Layers.cpp:32
const Layer & GetLayer(LayerID id)
Get layer by ID.
Definition Layers.cpp:27
LayerID CreateLayer(bool isStatic=true)
Create a new physics layer.
Definition Layers.cpp:22
Physics collision layer.
Definition Layers.hpp:26
Layer(const Layer &copy)
Copy physics layer.
Definition Layers.cpp:12
const bool isStatic
Whether objects are static.
Definition Layers.hpp:30
const JPH::BroadPhaseLayer BroadPhaseLayer
Broad phase settings.
Definition Layers.hpp:32
const LayerID id
Unique layer ID.
Definition Layers.hpp:31
Filter for object layer collisions.
Definition Layers.hpp:135
virtual bool ShouldCollide(JPH::ObjectLayer inObject1, JPH::ObjectLayer inObject2) const override
Check if layers should collide.
Definition Layers.cpp:47
Filter for broad phase layer collisions.
Definition Layers.hpp:118
virtual bool ShouldCollide(JPH::ObjectLayer inLayer1, JPH::BroadPhaseLayer inLayer2) const override
Check if layers should collide.
Definition Layers.cpp:39
std::size_t operator()(const StevEngine::Physics::Layer &k) const
Hash function for Physics layer.
Definition Layers.hpp:153