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
9QT_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 float CapsuleShape::diameter
32 This property defines the diameter of the capsule
33*/
34
35/*!
36 \qmlproperty float CapsuleShape::height
37 This property defines the height of the capsule
38*/
39
40QCapsuleShape::QCapsuleShape() = default;
41
42QCapsuleShape::~QCapsuleShape()
43{
44 delete m_physXGeometry;
45}
46
47physx::PxGeometry *QCapsuleShape::getPhysXGeometry()
48{
49 if (!m_physXGeometry || m_scaleDirty) {
50 updatePhysXGeometry();
51 }
52
53 return m_physXGeometry;
54}
55
56float QCapsuleShape::diameter() const
57{
58 return m_diameter;
59}
60
61void QCapsuleShape::setDiameter(float newDiameter)
62{
63 if (qFuzzyCompare(p1: m_diameter, p2: newDiameter))
64 return;
65 m_diameter = newDiameter;
66 updatePhysXGeometry();
67
68 emit needsRebuild(this);
69 emit diameterChanged();
70}
71
72float QCapsuleShape::height() const
73{
74 return m_height;
75}
76
77void QCapsuleShape::setHeight(float newHeight)
78{
79 if (qFuzzyCompare(p1: m_height, p2: newHeight))
80 return;
81 m_height = newHeight;
82 updatePhysXGeometry();
83
84 emit needsRebuild(this);
85 emit heightChanged();
86}
87
88void QCapsuleShape::updatePhysXGeometry()
89{
90 delete m_physXGeometry;
91 QVector3D s = sceneScale();
92 qreal rs = s.y();
93 qreal hs = s.x();
94 m_physXGeometry = new physx::PxCapsuleGeometry(rs * m_diameter * 0.5f, hs * m_height * 0.5f);
95 m_scaleDirty = false;
96}
97
98QT_END_NAMESPACE
99

source code of qtquick3dphysics/src/quick3dphysics/qcapsuleshape.cpp