189 Q_ASSERT(!controller);
191 auto *characterController =
static_cast<QCharacterController *>(frontendNode);
193 auto shapes = characterController->getCollisionShapesList();
194 if (shapes.length() != 1) {
195 qWarning() <<
"CharacterController: invalid collision shapes list.";
198 auto *capsule = qobject_cast<QCapsuleShape *>(shapes.first());
200 qWarning() <<
"CharacterController: collision shape is not a capsule.";
203 auto *mgr = world->controllerManager();
205 qWarning() <<
"QtQuick3DPhysics internal error: missing controller manager.";
211 const QVector3D scale = characterController->sceneScale();
212 const qreal heightScale = scale.y();
213 const qreal radiusScale = scale.x();
214 physx::PxCapsuleControllerDesc desc;
216 desc.reportCallback = reportCallback;
218 desc.radius = 0.5f * radiusScale * capsule->diameter();
219 desc.height = heightScale * capsule->height();
221 resolveStepOffset(characterController->maximumStepHeight() * heightScale,
222 desc.height, desc.radius);
226 auto g = characterController->effectiveGravity();
228 QVector3D upDirection = -g.normalized();
229 desc.upDirection = QPhysicsUtils::toPhysXType(upDirection);
232 characterController->setGravityOrientation(
233 QQuaternion::rotationTo(QVector3D(0, 1, 0), upDirection));
236 desc.slopeLimit = toPhysXSlopeLimit(characterController->slopeLimit());
237 desc.nonWalkableMode = toPhysXNonWalkableMode(characterController->slideOnSteepSlopes());
239 desc.material = material;
240 const QVector3D pos = characterController->scenePosition();
241 desc.position = { pos.x(), pos.y(), pos.z() };
244 controller =
static_cast<
physx::PxCapsuleController *>(mgr->createController(desc));
247 qWarning() <<
"QtQuick3DPhysics internal error: could not create controller.";
251 controller->setUserData(
static_cast<
void *>(frontendNode));
253 auto *actor = controller->getActor();
255 actor->userData = characterController;
257 qWarning() <<
"QtQuick3DPhysics internal error: CharacterController created without actor.";
269 QHash<QQuick3DNode *, QMatrix4x4> & )
274 auto *characterController =
static_cast<QCharacterController *>(frontendNode);
276 if (controller ==
nullptr) {
279 characterController->clearEnvironmentGravity();
284 const auto &shapes = characterController->getCollisionShapesList();
285 auto capsule = shapes.length() == 1 ? qobject_cast<QCapsuleShape *>(shapes.front()) :
nullptr;
287 if (shapes.length() != 1) {
288 qWarning() <<
"CharacterController: invalid collision shapes list.";
289 }
else if (!capsule) {
290 qWarning() <<
"CharacterController: collision shape is not a capsule.";
292 const QVector3D sceneScale = characterController->sceneScale();
293 const qreal heightScale = sceneScale.y();
294 const qreal radiusScale = sceneScale.x();
297 const float heightNew = heightScale * capsule->height();
298 const float radiusNew = 0.5f * radiusScale * capsule->diameter();
300 if (!qIsFinite(heightNew) || heightNew <= 0.f || !qIsFinite(radiusNew)
301 || radiusNew <= 0.f) {
302 qWarning() <<
"CharacterController: scaled capsule height/radius must be finite "
303 "and positive, ignoring.";
305 if (!qFuzzyCompare(controller->getHeight(), heightNew))
306 controller->resize(heightNew);
307 if (!qFuzzyCompare(controller->getRadius(), radiusNew))
308 controller->setRadius(radiusNew);
312 const float stepOffset =
313 resolveStepOffset(characterController->maximumStepHeight() * heightScale, heightNew,
314 radiusNew, controller->getStepOffset());
315 if (!qFuzzyCompare(controller->getStepOffset(), stepOffset))
316 controller->setStepOffset(stepOffset);
319 auto pxUpDirection = controller->getUpDirection();
320 QVector3D oldUpDirection = QPhysicsUtils::toQtType(pxUpDirection);
322 auto g = characterController->effectiveGravity();
324 QVector3D newUpDirection = -g.normalized();
327 if (!qFuzzyCompare(oldUpDirection, newUpDirection)) {
328 controller->setUpDirection(QPhysicsUtils::toPhysXType(newUpDirection));
330 QQuaternion delta = QQuaternion::rotationTo(oldUpDirection, newUpDirection);
331 characterController->setGravityOrientation((delta * characterController->gravityOrientation()).normalized());
339 const float slopeLimit = toPhysXSlopeLimit(characterController->slopeLimit());
340 if (!qFuzzyCompare(controller->getSlopeLimit(), slopeLimit)) {
341 controller->setSlopeLimit(slopeLimit);
344 const auto nonWalkableMode =
345 toPhysXNonWalkableMode(characterController->slideOnSteepSlopes());
346 if (controller->getNonWalkableMode() != nonWalkableMode) {
347 controller->setNonWalkableMode(nonWalkableMode);
352 QVector3D position = QPhysicsUtils::toQtType(physx::toVec3(controller->getPosition()));
353 const QQuick3DNode *parentNode =
static_cast<QQuick3DNode *>(characterController->parentItem());
356 characterController->setPosition(position);
358 characterController->setPosition(parentNode->mapPositionFromScene(position));
365 QVector3D teleportPos;
366 bool teleport = characterController->getTeleport(teleportPos);
368 controller->setPosition({ teleportPos.x(), teleportPos.y(), teleportPos.z() });
369 }
else if (deltaTime > 0) {
370 const auto displacement =
371 QPhysicsUtils::toPhysXType(characterController->getDisplacement(deltaTime));
379 const physx::PxControllerFilters filters(
nullptr, filter, filter);
382 auto collisions = controller->move(displacement, displacement.magnitude() * 0.01f,
389 characterController->setCollisions(QCharacterController::Collisions(uint(collisions)));
393 characterController->setSliding(reportCallback
->sliding);
401 characterController->clearEnvironmentGravity();
406 if (updateMaterial() && material) {
407 if (
auto *shape = shapeOf(*controller))
408 shape->setMaterials(&material, 1);