Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcharactercontroller_p.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef CHARACTERCONTROLLER_H
6#define CHARACTERCONTROLLER_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQuick3DPhysics/qtquick3dphysicsglobal.h>
20#include <QtQuick3DPhysics/private/qabstractphysicsbody_p.h>
21#include <QtQml/QQmlEngine>
22#include <QVector3D>
23
25
27
28class Q_QUICK3DPHYSICS_EXPORT QCharacterController : public QAbstractPhysicsBody
29{
30 Q_OBJECT
31 Q_PROPERTY(QVector3D movement READ movement WRITE setMovement NOTIFY movementChanged)
32 Q_PROPERTY(QVector3D gravity READ gravity WRITE setGravity NOTIFY gravityChanged)
33 Q_PROPERTY(bool midAirControl READ midAirControl WRITE setMidAirControl NOTIFY
34 midAirControlChanged)
35 Q_PROPERTY(Collisions collisions READ collisions NOTIFY collisionsChanged)
36 Q_PROPERTY(bool enableShapeHitCallback READ enableShapeHitCallback WRITE
37 setEnableShapeHitCallback NOTIFY enableShapeHitCallbackChanged)
38 Q_PROPERTY(float maximumStepHeight READ maximumStepHeight WRITE setMaximumStepHeight NOTIFY
39 maximumStepHeightChanged REVISION(6, 13))
40 Q_PROPERTY(float slopeLimit READ slopeLimit WRITE setSlopeLimit NOTIFY
41 slopeLimitChanged REVISION(6, 13))
42 Q_PROPERTY(bool sliding READ isSliding NOTIFY slidingChanged REVISION(6, 13))
43 Q_PROPERTY(bool slideOnSteepSlopes READ slideOnSteepSlopes WRITE setSlideOnSteepSlopes NOTIFY
44 slideOnSteepSlopesChanged REVISION(6, 13))
45 Q_PROPERTY(MovementMode movementMode READ movementMode WRITE setMovementMode NOTIFY
46 movementModeChanged REVISION(6, 13))
47 Q_PROPERTY(QQuaternion gravityOrientation READ gravityOrientation NOTIFY gravityOrientationChanged
48 REVISION(6, 13))
50 QML_NAMED_ELEMENT(CharacterController)
51public:
52 QCharacterController();
53
54 enum class MovementMode {
55 ForwardBased,
56 Free
57 };
59 Q_ENUM(MovementMode)
60
61 enum class Collision {
62 None = 0,
63 Side = 1 << 0,
64 Up = 1 << 1,
65 Down = 1 << 2,
66 };
67 Q_DECLARE_FLAGS(Collisions, Collision)
68 Q_FLAG(Collisions)
69
70 const QVector3D &movement() const;
71 void setMovement(const QVector3D &newMovement);
72 const QVector3D &gravity() const;
73 void setGravity(const QVector3D &newGravity);
74 QVector3D getDisplacement(float deltaTime);
75 bool getTeleport(QVector3D &position);
76
77 bool midAirControl() const;
78 void setMidAirControl(bool newMidAirControl);
79
80 Q_INVOKABLE void teleport(const QVector3D &position);
82 const Collisions &collisions() const;
83 void setCollisions(const Collisions &newCollisions);
84 QAbstractPhysXNode *createPhysXBackend() final;
85
86 Q_REVISION(6, 6) bool enableShapeHitCallback() const;
87 Q_REVISION(6, 6) void setEnableShapeHitCallback(bool newEnableShapeHitCallback);
89 // What GravityFields contribute this frame, on top of
90 // the gravity property. Cleared at the end of every sync().
91 void addEnvironmentGravity(const QVector3D &gravity);
92 void clearEnvironmentGravity();
93 QVector3D effectiveGravity() const;
94 Q_REVISION(6, 13) float maximumStepHeight() const;
95 Q_REVISION(6, 13) void setMaximumStepHeight(float newMaximumStepHeight);
96
97 Q_REVISION(6, 13) float slopeLimit() const;
98 Q_REVISION(6, 13) void setSlopeLimit(float newSlopeLimit);
99
100 Q_REVISION(6, 13) bool slideOnSteepSlopes() const;
101 Q_REVISION(6, 13) void setSlideOnSteepSlopes(bool newSlideOnSteepSlopes);
103 Q_REVISION(6, 13) MovementMode movementMode() const;
104 Q_REVISION(6, 13) void setMovementMode(MovementMode newMovementMode);
106 Q_REVISION(6, 13) QQuaternion gravityOrientation() const;
107
108 Q_REVISION(6, 13) bool isSliding() const;
109
110signals:
111 void movementChanged();
112 void gravityChanged();
113
114 void midAirControlChanged();
116 void collisionsChanged();
117 void enableShapeHitCallbackChanged();
118 void shapeHit(QAbstractPhysicsNode *body, const QVector3D &position, const QVector3D &impulse,
119 const QVector3D &normal);
121 Q_REVISION(6, 13) void slidingChanged();
123 Q_REVISION(6, 13) void maximumStepHeightChanged();
124 Q_REVISION(6, 13) void slopeLimitChanged();
125
126 Q_REVISION(6, 13) void slideOnSteepSlopesChanged();
127
128 Q_REVISION(6, 13) void landed(float speed);
130 Q_REVISION(6, 13) void movementModeChanged();
131
132 Q_REVISION(6, 13) void gravityOrientationChanged();
133
134private:
135 friend class QPhysXCharacterController;
136
137 Q_REVISION(6, 13) void setGravityOrientation(const QQuaternion &newRotation);
138 Q_REVISION(6, 13) void setSliding(bool sliding);
139
140 QVector3D m_movement;
141 QVector3D m_gravity;
142 QVector3D m_environmentGravity; // what the fields contributed this frame
143 bool m_midAirControl = true;
144
145 QVector3D m_freeFallVelocity; // actual speed at start of next tick, if free fall
146
147 QVector3D m_teleportPosition;
148 bool m_teleport = false;
149 Collisions m_collisions;
150
151 bool m_enableShapeHitCallback = false;
152 bool m_sliding = false;
153 float m_maximumStepHeight = -1.f;
154 float m_slopeLimit = 45.f;
155
156 bool m_lastGrounded = false;
157 QVector3D m_jumpVelocity; // launch velocity: what was applied on the last grounded frame
158 bool m_slideOnSteepSlopes = false;
159 MovementMode m_movementMode = MovementMode::ForwardBased;
160 QQuaternion m_gravityOrientation;
161};
162
163Q_DECLARE_OPERATORS_FOR_FLAGS(QCharacterController::Collisions)
164
166
167#endif // CHARACTERCONTROLLER_H
void onShapeHit(const physx::PxControllerShapeHit &hit) override
void onControllerHit(const physx::PxControllersHit &) override
ControllerCallback(QPhysicsWorld *worldIn)
void onObstacleHit(const physx::PxControllerObstacleHit &) override
physx::PxQueryHitType::Enum preFilter(const physx::PxFilterData &, const physx::PxShape *shape, const physx::PxRigidActor *, physx::PxHitFlags &) override
physx::PxQueryHitType::Enum postFilter(const physx::PxFilterData &, const physx::PxQueryHit &, const physx::PxShape *, const physx::PxRigidActor *) override
bool filter(const physx::PxController &a, const physx::PxController &b) override
virtual void cleanup(QPhysXWorld *)
void setFiltersDirty(bool dirty)
virtual void detachFrontend()
QPhysXCharacterController(QCharacterController *frontEnd)
QPhysicsMaterial * qtMaterial() const override
DebugDrawBodyType getDebugDrawBodyType() override
void init(QPhysicsWorld *world, QPhysXWorld *physX) override
void cleanup(QPhysXWorld *physX) override
void updateFilters(QPhysXWorld *physX) override
void sync(float deltaTime, QHash< QQuick3DNode *, QMatrix4x4 > &transformCache) override
#define PHYSX_RELEASE(x)
DebugDrawBodyType
static float resolveStepOffset(float maximumStepHeight, float height, float radius, float current=-1.f)
static float toPhysXSlopeLimit(float degrees)
static physx::PxShape * shapeOf(const physx::PxController &controller)
#define QT_BEGIN_NAMESPACE
#define QT_END_NAMESPACE