| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "qcapsuleshape_p.h" |
| 5 | |
| 6 | #include <QtQuick3D/QQuick3DGeometry> |
| 7 | #include <geometry/PxCapsuleGeometry.h> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | /*! |
| 12 | \qmltype CapsuleShape |
| 13 | \inherits CollisionShape |
| 14 | \inqmlmodule QtQuick3D.Physics |
| 15 | \since 6.4 |
| 16 | \brief Defines a pill-like shape. |
| 17 | |
| 18 | This type defines a capsule shape. This is a cylinder with a hemisphere at each end. |
| 19 | The origin is at the center of the capsule. The capsule is specified by \l diameter, which |
| 20 | determines the diameter of the cylinder and the hemispheres; and \l height, which |
| 21 | determines the height of the cylinder. |
| 22 | |
| 23 | \note When using scaling transformations with this shape, the x component will be used to scale the height and |
| 24 | the y component will be used to scale the diameter. The cylinder will always be perfectly circular even if the |
| 25 | scaling transformation is non-uniform. |
| 26 | |
| 27 | \sa {Qt Quick 3D Physics Shapes and Bodies}{Shapes and Bodies overview documentation} |
| 28 | */ |
| 29 | |
| 30 | /*! |
| 31 | \qmlproperty real CapsuleShape::diameter |
| 32 | This property defines the diameter of the capsule |
| 33 | |
| 34 | Default value: \c{100} |
| 35 | */ |
| 36 | |
| 37 | /*! |
| 38 | \qmlproperty real CapsuleShape::height |
| 39 | This property defines the height of the capsule |
| 40 | |
| 41 | Default value: \c{100} |
| 42 | */ |
| 43 | |
| 44 | QCapsuleShape::QCapsuleShape() = default; |
| 45 | |
| 46 | QCapsuleShape::~QCapsuleShape() |
| 47 | { |
| 48 | delete m_physXGeometry; |
| 49 | } |
| 50 | |
| 51 | physx::PxGeometry *QCapsuleShape::getPhysXGeometry() |
| 52 | { |
| 53 | if (!m_physXGeometry || m_scaleDirty) { |
| 54 | updatePhysXGeometry(); |
| 55 | } |
| 56 | |
| 57 | return m_physXGeometry; |
| 58 | } |
| 59 | |
| 60 | float QCapsuleShape::diameter() const |
| 61 | { |
| 62 | return m_diameter; |
| 63 | } |
| 64 | |
| 65 | void QCapsuleShape::setDiameter(float newDiameter) |
| 66 | { |
| 67 | if (qFuzzyCompare(p1: m_diameter, p2: newDiameter)) |
| 68 | return; |
| 69 | m_diameter = newDiameter; |
| 70 | updatePhysXGeometry(); |
| 71 | |
| 72 | emit needsRebuild(this); |
| 73 | emit diameterChanged(); |
| 74 | } |
| 75 | |
| 76 | float QCapsuleShape::height() const |
| 77 | { |
| 78 | return m_height; |
| 79 | } |
| 80 | |
| 81 | void QCapsuleShape::setHeight(float newHeight) |
| 82 | { |
| 83 | if (qFuzzyCompare(p1: m_height, p2: newHeight)) |
| 84 | return; |
| 85 | m_height = newHeight; |
| 86 | updatePhysXGeometry(); |
| 87 | |
| 88 | emit needsRebuild(this); |
| 89 | emit heightChanged(); |
| 90 | } |
| 91 | |
| 92 | void QCapsuleShape::updatePhysXGeometry() |
| 93 | { |
| 94 | delete m_physXGeometry; |
| 95 | QVector3D s = sceneScale(); |
| 96 | qreal rs = s.y(); |
| 97 | qreal hs = s.x(); |
| 98 | m_physXGeometry = new physx::PxCapsuleGeometry(rs * m_diameter * 0.5f, hs * m_height * 0.5f); |
| 99 | m_scaleDirty = false; |
| 100 | } |
| 101 | |
| 102 | QT_END_NAMESPACE |
| 103 |
