StevEngine
StevEngine is a customizable C++ game engine.
Loading...
Searching...
No Matches
CharacterBody.hpp
1#pragma once
2#ifdef StevEngine_PHYSICS
3#include "main/Component.hpp"
4#include "physics/Layers.hpp"
5#include "utilities/Vector3.hpp"
6
7#include "Jolt/Physics/Character/CharacterVirtual.h"
8
9#define CHARACTERBODY_TYPE "CharacterBody"
10
11namespace StevEngine::Physics {
13 float mass = 70;
14 float maxStrength = 100;
18 float minTimeRemaining = 1.0e-4f;
19 float collisionTolerance = 1.0e-3f;
20 float characterPadding = 0.02f;
21 uint32_t maxNumHits = 256;
22 float hitReductionCosMaxAngle = 0.999f;
24 };
25
32 class CharacterBody : public Component {
33 public:
34 static const bool unique = true;
41 CharacterBody(CharacterSettings settings = {}, LayerID layer = LayerManager::DEFAULT);
42
48
50
55 virtual std::string GetType() const { return CHARACTERBODY_TYPE; }
56
62 virtual Utilities::Stream Export(Utilities::StreamType type) const;
63
67 virtual void Start();
68
72 virtual void Deactivate();
73
79 virtual void Update(double deltaTime);
80
85 void ExtendedUpdate(double deltaTime, JPH::CharacterVirtual::ExtendedUpdateSettings updateSettings = {});
86
90 virtual ~CharacterBody();
91
96 virtual bool RefreshShape();
97
98 protected:
99 JPH::CharacterVirtualSettings settings;
100 LayerID layer;
101
102 bool SetShape(JPH::Ref<JPH::Shape> shape, Utilities::Vector3 shapeOffset = 0);
103 JPH::Ref<JPH::Shape> GetShape() const { return shape; }
104
105 private:
106 JPH::CharacterVirtual* jphCharacter;
107 JPH::Ref<JPH::Shape> shape;
108
109 // Jolt filters
110 JPH::BodyFilter bodyFilter;
111 JPH::ShapeFilter shapeFilter;
112
113 // Function wrappers
114 public:
115 bool IsSupported() const { return jphCharacter->IsSupported(); };
116 bool IsOnGround() const { return jphCharacter->GetGroundState() == JPH::CharacterBase::EGroundState::OnGround; };
117 float GetMass () const { return jphCharacter->GetMass(); };
118 void SetMass (float mass) { jphCharacter->SetMass(mass); settings.mMass = mass; };
119 float GetMaxStrength () const { return jphCharacter->GetMaxStrength(); };
120 void SetMaxStrength (float maxStrength) { jphCharacter->SetMaxStrength(maxStrength); settings.mMaxStrength = maxStrength; };
121 float GetPenetrationRecoverySpeed () const { return jphCharacter->GetPenetrationRecoverySpeed(); };
122 void SetPenetrationRecoverySpeed (float speed) { jphCharacter->SetPenetrationRecoverySpeed(speed); settings.mPenetrationRecoverySpeed = speed; };
123 float GetCharacterPadding () const { return jphCharacter->GetCharacterPadding(); };
124 uint GetMaxNumHits () const { return jphCharacter->GetMaxNumHits(); };
125 void SetMaxNumHits (uint maxHits) { jphCharacter->SetMaxNumHits(maxHits); settings.mMaxNumHits = maxHits; };
126 float GetHitReductionCosMaxAngle () const { return jphCharacter->GetHitReductionCosMaxAngle(); };
127 void SetHitReductionCosMaxAngle (float cosMaxAngle) { jphCharacter->SetHitReductionCosMaxAngle(cosMaxAngle); settings.mHitReductionCosMaxAngle = cosMaxAngle; };
128 void SetMaxSlopeAngle (float maxSlopeAngle) { jphCharacter->SetMaxSlopeAngle(maxSlopeAngle); settings.mMaxSlopeAngle = maxSlopeAngle; };
129 bool IsSlopeTooSteep (Utilities::Vector3 normal) const { return jphCharacter->IsSlopeTooSteep(normal); };
130 };
131}
132#endif
Component()
Create new component.
Definition Component.cpp:11
uint GetMaxNumHits() const
Character padding.
Definition CharacterBody.hpp:124
void SetHitReductionCosMaxAngle(float cosMaxAngle)
Cos(angle) where angle is the maximum angle between two hits contact normals that are allowed to be m...
Definition CharacterBody.hpp:127
float GetMaxStrength() const
Set character mass (kg)
Definition CharacterBody.hpp:119
virtual std::string GetType() const
Get component type.
Definition CharacterBody.hpp:55
JPH::Ref< JPH::Shape > GetShape() const
Get Jolt Physics collision shape.
Definition CharacterBody.hpp:103
Utilities::Vector3 velocity
Character velocity.
Definition CharacterBody.hpp:49
virtual ~CharacterBody()
Clean up resources.
Definition CharacterBody.cpp:165
virtual Utilities::Stream Export(Utilities::StreamType type) const
Serialize component to a stream.
Definition CharacterBody.cpp:108
float GetCharacterPadding() const
Set how fast a penetration will be resolved, 0 = nothing is resolved, 1 = everything in one update.
Definition CharacterBody.hpp:123
LayerID layer
Physics layer.
Definition CharacterBody.hpp:100
void SetMaxSlopeAngle(float maxSlopeAngle)
Set Cos(angle) where angle is the maximum angle between two hits contact normals that are allowed to ...
Definition CharacterBody.hpp:128
void ExtendedUpdate(double deltaTime, JPH::CharacterVirtual::ExtendedUpdateSettings updateSettings={})
Extended physics transform update.
Definition CharacterBody.cpp:156
virtual void Deactivate()
Clean up when deactivated.
Definition CharacterBody.cpp:142
void SetPenetrationRecoverySpeed(float speed)
This value governs how fast a penetration will be resolved, 0 = nothing is resolved,...
Definition CharacterBody.hpp:122
bool IsSlopeTooSteep(Utilities::Vector3 normal) const
Set the maximum angle of slope that character can still walk on (radians)
Definition CharacterBody.hpp:129
void SetMass(float mass)
Get character mass (kg)
Definition CharacterBody.hpp:118
CharacterBody(CharacterSettings settings={}, LayerID layer=LayerManager::DEFAULT)
Create rigid body.
Definition CharacterBody.cpp:36
float GetHitReductionCosMaxAngle() const
Set max num hits to collect in order to avoid excess of contact points collection.
Definition CharacterBody.hpp:126
JPH::CharacterVirtualSettings settings
Jolt Physics Virtual Character Controller Settings.
Definition CharacterBody.hpp:99
virtual void Start()
Initialize component.
Definition CharacterBody.cpp:127
bool SetShape(JPH::Ref< JPH::Shape > shape, Utilities::Vector3 shapeOffset=0)
Set Jolt Physics collision shape.
Definition CharacterBody.cpp:70
float GetMass() const
Is character on the ground.
Definition CharacterBody.hpp:117
bool IsOnGround() const
Is character supported (by ground/wall)
Definition CharacterBody.hpp:116
void SetMaxStrength(float maxStrength)
Maximum force with which the character can push other bodies (N)
Definition CharacterBody.hpp:120
void SetMaxNumHits(uint maxHits)
Max num hits to collect in order to avoid excess of contact points collection.
Definition CharacterBody.hpp:125
static const bool unique
Only one per GameObject.
Definition CharacterBody.hpp:34
virtual void Update(double deltaTime)
Update transform from physics Adds gravity to the current velocity.
Definition CharacterBody.cpp:146
virtual bool RefreshShape()
Refresh combined collision shape Rebuilds shape from all attached colliders.
Definition CharacterBody.cpp:44
Stream for serialization of data.
Definition Stream.hpp:22
3D vector class
Definition Vector3.hpp:17
Definition CharacterBody.hpp:12
float minTimeRemaining
Early out condition: If this much time is left to simulate we are done.
Definition CharacterBody.hpp:18
uint32_t maxNumHits
Max num hits to collect in order to avoid excess of contact points collection.
Definition CharacterBody.hpp:21
float predictiveContactDistance
How far to scan outside of the shape for predictive contacts. A value of 0 will most likely cause the...
Definition CharacterBody.hpp:15
float maxStrength
Maximum force with which the character can push other bodies (N).
Definition CharacterBody.hpp:14
float collisionTolerance
How far we're willing to penetrate geometry.
Definition CharacterBody.hpp:19
uint32_t maxCollisionIterations
Max amount of collision loops.
Definition CharacterBody.hpp:16
float characterPadding
How far we try to stay away from the geometry, this ensures that the sweep will hit as little as poss...
Definition CharacterBody.hpp:20
float penetrationRecoverySpeed
This value governs how fast a penetration will be resolved, 0 = nothing is resolved,...
Definition CharacterBody.hpp:23
float hitReductionCosMaxAngle
Cos(angle) where angle is the maximum angle between two hits contact normals that are allowed to be m...
Definition CharacterBody.hpp:22
float mass
Character mass (kg)
Definition CharacterBody.hpp:13
uint32_t maxConstraintIterations
How often to try stepping in the constraint solving.
Definition CharacterBody.hpp:17