Skylicht Engine
Loading...
Searching...
No Matches
CCharacterController.h
1/*
2!@
3MIT License
4
5Copyright (c) 2024 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "PhysicsEngine/ICollisionObject.h"
28
29#ifdef USE_BULLET_PHYSIC_ENGINE
30#include <btBulletDynamicsCommon.h>
31#include <BulletDynamics/Character/btKinematicCharacterController.h>
32#include <BulletCollision/CollisionDispatch/btGhostObject.h>
33#endif
34
35namespace Skylicht
36{
37 namespace Physics
38 {
68 class CCharacterController : public ICollisionObject
69 {
70 friend class CPhysicsEngine;
71 protected:
72#ifdef USE_BULLET_PHYSIC_ENGINE
73 btPairCachingGhostObject* m_ghostObject;
74 btKinematicCharacterController* m_controller;
75 btConvexShape* m_shape;
76#endif
77 public:
78 CCharacterController();
79
80 virtual ~CCharacterController();
81
82 virtual void initComponent();
83
84 virtual void startComponent();
85
86 virtual void updateComponent();
87
88 virtual void setCollisionGroupAndFilter(int group, int filter);
89
95 bool initCharacter(float stepHeight);
96
101
106 core::vector3df getPosition();
107
112 void setPosition(const core::vector3df& pos);
113
118 core::vector3df getRotationEuler();
119
124 core::quaternion getRotation();
125
130 void setRotation(const core::vector3df& eulerDeg);
131
136 void setRotation(const core::quaternion& q);
137
142
147 void setWalkDirection(const core::vector3df& walk);
148
153 bool canJump();
154
159 bool onGround();
160
165 void setMaxSlope(float slopeRadians);
166
171 float getMaxSlope() const;
172
178
184
188 void reset();
189
194 void setStepHeight(float h);
195
200 float getStepHeight() const;
201
206 void setFallSpeed(float fallSpeed);
207
212 float getFallSpeed() const;
213
218 void setJumpSpeed(float jumpSpeed);
219
224 float getJumpSpeed() const;
225
230 void setMaxJumpHeight(float maxJumpHeight);
231
236 void jump(const core::vector3df& v);
237
242 void setGravity(const core::vector3df& gravity);
243
248 core::vector3df getGravity() const;
249
254 inline void applyImpulse(const core::vector3df& v)
255 {
256 jump(v);
257 }
258
263 void setAngularVelocity(const core::vector3df& velocity);
264
269 core::vector3df getAngularVelocity() const;
270
275 void setLinearVelocity(const core::vector3df& velocity);
276
281 core::vector3df getLinearVelocity() const;
282
287 void setLinearDamping(float d);
288
293 float getLinearDamping() const;
294
299 void setAngularDamping(float d);
300
305 float getAngularDamping() const;
306
311 void setUp(const core::vector3df& up);
312
317 core::vector3df getUp();
318
319 protected:
320
321#ifdef USE_BULLET_PHYSIC_ENGINE
322 btKinematicCharacterController* getCharacterController()
323 {
324 return m_controller;
325 }
326
327 btPairCachingGhostObject* getGhostObject()
328 {
329 return m_ghostObject;
330 }
331#endif
332 };
333 }
334}
void setAngularDamping(float d)
Sets the angular damping coefficient.
void jump(const core::vector3df &v)
Triggers a jump with an optional initial velocity.
float getFallSpeed() const
Gets the maximum falling speed.
core::quaternion getRotation()
Gets the world rotation as a quaternion.
bool onGround()
Checks if the character is currently on the ground.
void setMaxPenetrationDepth(float d)
Sets the maximum depth the character can penetrate other objects before collision response.
bool canJump()
Checks if the character is in a state where it can jump.
core::vector3df getPosition()
Gets the current world position of the character.
float getStepHeight() const
Gets the current step height.
float getLinearDamping() const
Gets the linear damping coefficient.
void setStepHeight(float h)
Sets the step height for the character.
core::vector3df getUp()
Gets the 'up' direction.
core::vector3df getAngularVelocity() const
Gets the angular velocity.
void setPosition(const core::vector3df &pos)
Sets the world position of the character.
void applyImpulse(const core::vector3df &v)
Applies an instantaneous impulse (wraps jump).
Definition CCharacterController.h:254
void setLinearVelocity(const core::vector3df &velocity)
Sets the linear velocity of the character.
float getMaxPenetrationDepth() const
Gets the maximum penetration depth.
void setGravity(const core::vector3df &gravity)
Sets the custom gravity for this character.
bool initCharacter(float stepHeight)
Initializes the character controller in the physics world.
core::vector3df getGravity() const
Gets the custom gravity of the character.
void setMaxSlope(float slopeRadians)
Sets the maximum slope the character can walk up.
core::vector3df getRotationEuler()
Gets the world rotation as Euler angles.
virtual void setCollisionGroupAndFilter(int group, int filter)
Sets the collision group and filter mask.
void setMaxJumpHeight(float maxJumpHeight)
Sets the maximum height the character can jump.
void releaseCharacter()
Releases the character controller from the physics world.
void setWalkDirection(const core::vector3df &walk)
Sets the direction and speed the character should walk.
void syncTransform()
Synchronizes the GameObject's transform with the character's physics transform.
void setJumpSpeed(float jumpSpeed)
Sets the initial speed when jumping.
float getJumpSpeed() const
Gets the initial jump speed.
void setAngularVelocity(const core::vector3df &velocity)
Sets the angular velocity of the character.
void setRotation(const core::quaternion &q)
Sets the world rotation using a quaternion.
float getAngularDamping() const
Gets the angular damping coefficient.
void setFallSpeed(float fallSpeed)
Sets the maximum falling speed.
core::vector3df getLinearVelocity() const
Gets the linear velocity.
void setUp(const core::vector3df &up)
Sets the 'up' direction for the character.
float getMaxSlope() const
Gets the maximum slope angle.
void reset()
Resets the character controller's state and position in the dynamics world.
void setRotation(const core::vector3df &eulerDeg)
Sets the world rotation using Euler angles.
void setLinearDamping(float d)
Sets the linear damping coefficient.
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29