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