1 | // Copyright (C) 2021 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qsphereshape_p.h" |
5 | #include <QtQuick3D/QQuick3DGeometry> |
6 | |
7 | #include <geometry/PxSphereGeometry.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | /*! |
12 | \qmltype SphereShape |
13 | \inqmlmodule QtQuick3D.Physics |
14 | \inherits CollisionShape |
15 | \since 6.4 |
16 | \brief Defines a spherical collision shape. |
17 | |
18 | This type defines a spherical shape. The origin is at the center of the sphere. |
19 | \note When using a scaling transformation with this shape, the x component will be used to scale the diameter of |
20 | the sphere. The resulting shape will be perfectly round, even if a non-uniform scaling transformation is used. |
21 | */ |
22 | |
23 | /*! |
24 | \qmlproperty float SphereShape::diameter |
25 | This property defines the diameter of the sphere |
26 | */ |
27 | |
28 | QSphereShape::QSphereShape() = default; |
29 | |
30 | QSphereShape::~QSphereShape() |
31 | { |
32 | delete m_physXGeometry; |
33 | } |
34 | |
35 | float QSphereShape::diameter() const |
36 | { |
37 | return m_diameter; |
38 | } |
39 | |
40 | physx::PxGeometry *QSphereShape::getPhysXGeometry() |
41 | { |
42 | if (!m_physXGeometry || m_scaleDirty) { |
43 | updatePhysXGeometry(); |
44 | } |
45 | return m_physXGeometry; |
46 | } |
47 | |
48 | void QSphereShape::setDiameter(float diameter) |
49 | { |
50 | if (qFuzzyCompare(p1: m_diameter, p2: diameter)) |
51 | return; |
52 | |
53 | m_diameter = diameter; |
54 | updatePhysXGeometry(); |
55 | |
56 | emit needsRebuild(this); |
57 | emit diameterChanged(diameter: m_diameter); |
58 | } |
59 | |
60 | void QSphereShape::updatePhysXGeometry() |
61 | { |
62 | delete m_physXGeometry; |
63 | auto s = sceneScale(); |
64 | m_physXGeometry = new physx::PxSphereGeometry(m_diameter * 0.5f * s.x()); |
65 | m_scaleDirty = false; |
66 | } |
67 | |
68 | QT_END_NAMESPACE |
69 |