8#include "physxnode/qphysxcharactercontroller_p.h"
10#include <QtCore/QPointer>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
40
41
42
43
44
45
46
47
48
49
50
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
82
83
84
85
86
87
88
89
90
91
92
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
118
119
120
121
122
123
124
125
128
129
130
131
134
135
136
137
138
139
140
141
142
143
144
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
201
202
203
204
205
206
207
208
209
210
211
212
213
214
217
218
219
220
221
222
223
224
225
226
227
228
229
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
254
255
256
257
258
259
260
261
262
263
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
296QCharacterController::QCharacterController() =
default;
298const QVector3D &QCharacterController::movement()
const
303void QCharacterController::setMovement(
const QVector3D &newMovement)
305 if (!QPhysicsUtils::isFinite(newMovement)) {
306 qWarning() <<
"CharacterController: movement must be finite, ignoring" << newMovement;
310 if (m_movement == newMovement)
312 m_movement = newMovement;
313 emit movementChanged();
316const QVector3D &QCharacterController::gravity()
const
321void QCharacterController::setGravity(
const QVector3D &newGravity)
323 if (!QPhysicsUtils::isFinite(newGravity)) {
324 qWarning() <<
"CharacterController: gravity must be finite, ignoring" << newGravity;
328 if (m_gravity == newGravity)
330 m_gravity = newGravity;
334 if (newGravity.isNull())
335 m_freeFallVelocity = QVector3D(0.0f, 0.0f, 0.0f);
336 emit gravityChanged();
339void QCharacterController::addEnvironmentGravity(
const QVector3D &gravity)
341 m_environmentGravity += gravity;
344void QCharacterController::clearEnvironmentGravity()
346 m_environmentGravity = QVector3D(0.0f, 0.0f, 0.0f);
349QVector3D QCharacterController::effectiveGravity()
const
351 return m_gravity + m_environmentGravity;
355QVector3D QCharacterController::getDisplacement(
float deltaTime)
357 if (deltaTime <= 0.0f) {
358 return QVector3D(0.0f, 0.0f, 0.0f);
363 if (m_movementMode == MovementMode::Free) {
364 return m_movement * deltaTime;
368 const QVector3D moveVelocity = sceneRotation() * m_movement;
369 QVector3D displacement = moveVelocity * deltaTime;
372 const QVector3D g = effectiveGravity();
373 if (g.isNull() && m_freeFallVelocity.isNull()) {
377 const bool hasGravity = !g.isNull();
378 const QVector3D gravityDir = hasGravity ? g.normalized() : QVector3D(0.0f, 0.0f, 0.0f);
381 const bool grounded = (m_collisions & Collision::Down);
384 const bool landing = (!m_lastGrounded && grounded);
386 m_lastGrounded = grounded;
392 const QVector3D impactVelocity = m_freeFallVelocity + (m_midAirControl ? moveVelocity : m_jumpVelocity);
395 const QVector3D fallDir = hasGravity ? gravityDir : m_freeFallVelocity.normalized();
396 const float impactSpeed = QVector3D::dotProduct(impactVelocity, fallDir);
397 if (impactSpeed > 0.0f) {
398 const QPointer<QCharacterController> self(
this);
399 emit landed(impactSpeed);
402 return QVector3D(0.0f, 0.0f, 0.0f);
406 const QVector3D gravityStepDisp = 0.5f * g * deltaTime * deltaTime;
411 m_freeFallVelocity = QVector3D(0.0f, 0.0f, 0.0f);
412 m_jumpVelocity = moveVelocity;
414 const bool jump = QVector3D::dotProduct(gravityStepDisp + displacement, gravityDir) < 0.0f
422 constexpr float slideTime = 0.7f;
423 return displacement + g * slideTime * deltaTime;
430 return displacement + gravityStepDisp;
435 if (!m_midAirControl) {
436 displacement = m_jumpVelocity * deltaTime;
440 displacement += m_freeFallVelocity * deltaTime;
441 m_freeFallVelocity += g * deltaTime;
443 const bool hitCeiling = (m_collisions & Collision::Up);
444 if (!m_midAirControl && hitCeiling && hasGravity) {
446 const QVector3D upDir = -gravityDir;
447 const float upSpeed = QVector3D::dotProduct(m_jumpVelocity, upDir);
448 if (upSpeed > 0.0f) {
449 m_jumpVelocity -= upDir * upSpeed;
456bool QCharacterController::midAirControl()
const
458 return m_midAirControl;
461void QCharacterController::setMidAirControl(
bool newMidAirControl)
463 if (m_midAirControl == newMidAirControl)
465 m_midAirControl = newMidAirControl;
466 emit midAirControlChanged();
469void QCharacterController::teleport(
const QVector3D &position)
471 if (!QPhysicsUtils::isFinite(position)) {
472 qWarning() <<
"CharacterController: teleport() position must be finite, ignoring"
478 m_teleportPosition = position;
479 m_freeFallVelocity = {};
482bool QCharacterController::getTeleport(QVector3D &position)
485 position = m_teleportPosition;
492const QCharacterController::Collisions &QCharacterController::collisions()
const
497void QCharacterController::setCollisions(
const Collisions &newCollisions)
499 if (m_collisions == newCollisions)
501 m_collisions = newCollisions;
502 emit collisionsChanged();
505bool QCharacterController::enableShapeHitCallback()
const
507 return m_enableShapeHitCallback;
510QAbstractPhysXNode *QCharacterController::createPhysXBackend()
512 return new QPhysXCharacterController(
this);
515void QCharacterController::setEnableShapeHitCallback(
bool newEnableShapeHitCallback)
517 if (m_enableShapeHitCallback == newEnableShapeHitCallback)
519 m_enableShapeHitCallback = newEnableShapeHitCallback;
520 emit enableShapeHitCallbackChanged();
523float QCharacterController::maximumStepHeight()
const
525 return m_maximumStepHeight;
528void QCharacterController::setMaximumStepHeight(
float newMaximumStepHeight)
530 if (!qIsFinite(newMaximumStepHeight)) {
531 qWarning() <<
"CharacterController: maximumStepHeight must be finite, ignoring"
532 << newMaximumStepHeight;
536 if (newMaximumStepHeight < 0.f)
537 newMaximumStepHeight = -1.f;
539 if (qFuzzyCompare(m_maximumStepHeight, newMaximumStepHeight))
542 m_maximumStepHeight = newMaximumStepHeight;
543 emit maximumStepHeightChanged();
546float QCharacterController::slopeLimit()
const
551void QCharacterController::setSlopeLimit(
float newSlopeLimit)
553 if (!qIsFinite(newSlopeLimit)) {
554 qWarning() <<
"CharacterController: slopeLimit must be finite, ignoring"
559 newSlopeLimit = qBound(0.f, newSlopeLimit, 90.f);
561 if (qFuzzyCompare(m_slopeLimit, newSlopeLimit))
563 m_slopeLimit = newSlopeLimit;
564 emit slopeLimitChanged();
567bool QCharacterController::slideOnSteepSlopes()
const
569 return m_slideOnSteepSlopes;
572void QCharacterController::setSlideOnSteepSlopes(
bool newSlideOnSteepSlopes)
574 if (m_slideOnSteepSlopes == newSlideOnSteepSlopes)
576 m_slideOnSteepSlopes = newSlideOnSteepSlopes;
577 emit slideOnSteepSlopesChanged();
580QCharacterController::MovementMode QCharacterController::movementMode()
const
582 return m_movementMode;
585void QCharacterController::setMovementMode(MovementMode newMovementMode)
587 if (m_movementMode == newMovementMode)
589 m_movementMode = newMovementMode;
590 emit movementModeChanged();
593QQuaternion QCharacterController::gravityOrientation()
const
595 return m_gravityOrientation;
598bool QCharacterController::isSliding()
const
603void QCharacterController::setSliding(
bool sliding)
605 if (m_sliding == sliding)
608 emit slidingChanged();
611void QCharacterController::setGravityOrientation(
const QQuaternion &newRotation)
613 if (m_gravityOrientation != newRotation) {
614 m_gravityOrientation = newRotation;
615 emit gravityOrientationChanged();
#define QT_BEGIN_NAMESPACE