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
qphysicsmaterial.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 <foundation/PxSimpleTypes.h>
8
9static float clamp(float value, float min, float max)
10{
11 return std::max(std::min(value, max), min);
12}
13
14QT_BEGIN_NAMESPACE
15
16/*!
17 \qmltype PhysicsMaterial
18 \inqmlmodule QtQuick3D.Physics
19 \since 6.4
20 \brief Defines the physics material of a body.
21
22 The PhysicsMaterial type determines how objects interact when they touch.
23
24 Friction uses the Coulomb friction model, which is based around
25 the concepts of 2 coefficients: the static friction coefficient and the dynamic friction
26 coefficient (sometimes called kinetic friction). Friction resists relative lateral motion of two
27 solid surfaces in contact. These two coefficients define a relationship between the normal force
28 exerted by each surface on the other and the amount of friction force that is applied to resist
29 lateral motion. While most real-world materials have friction coefficients between \c{0} and
30 \c{1}, values above \c{1} are not uncommon. The properties accept any real number greater or
31 equal to \c{0}.
32
33 Restitution determines how objects bounce when they collide.
34*/
35
36/*!
37 \qmlproperty real PhysicsMaterial::staticFriction
38 This property defines the amount of friction that is applied between surfaces that are not
39 moving lateral to each-other.
40
41 Default value: \c 0.5
42
43 Range: \c{[0, inf]}
44*/
45
46/*!
47 \qmlproperty real PhysicsMaterial::dynamicFriction
48 This property defines the amount of friction applied between surfaces that are moving relative
49 to each-other.
50
51 Default value: \c 0.5
52
53 Range: \c{[0, inf]}
54*/
55
56/*!
57 \qmlproperty real PhysicsMaterial::restitution
58 This property defines the coefficient of restitution, or how bouncy the material is.
59 The coefficient of restitution of two
60 colliding objects is a fractional value representing the ratio of speeds after and before an
61 impact, taken along the line of impact. A coefficient of restitution of 1 is said to collide
62 elastically, while a coefficient of restitution < 1 is said to be inelastic.
63
64 Default value: \c 0.5
65
66 Range: \c{[0, 1]}
67*/
68
69QPhysicsMaterial::QPhysicsMaterial(QObject *parent) : QObject(parent) { }
70
71float QPhysicsMaterial::staticFriction() const
72{
73 return m_staticFriction;
74}
75
76void QPhysicsMaterial::setStaticFriction(float staticFriction)
77{
78 staticFriction = clamp(staticFriction, 0.f, PX_MAX_F32);
79
80 if (qFuzzyCompare(m_staticFriction, staticFriction))
81 return;
82 m_staticFriction = staticFriction;
83 emit staticFrictionChanged();
84}
85
86float QPhysicsMaterial::dynamicFriction() const
87{
88 return m_dynamicFriction;
89}
90
91void QPhysicsMaterial::setDynamicFriction(float dynamicFriction)
92{
93 dynamicFriction = clamp(dynamicFriction, 0.f, PX_MAX_F32);
94
95 if (qFuzzyCompare(m_dynamicFriction, dynamicFriction))
96 return;
97 m_dynamicFriction = dynamicFriction;
98 emit dynamicFrictionChanged();
99}
100
101float QPhysicsMaterial::restitution() const
102{
103 return m_restitution;
104}
105
106void QPhysicsMaterial::setRestitution(float restitution)
107{
108 restitution = clamp(restitution, 0.f, 1.f);
109
110 if (qFuzzyCompare(m_restitution, restitution))
111 return;
112 m_restitution = restitution;
113 emit restitutionChanged();
114}
115
116QT_END_NAMESPACE
static float clamp(float value, float min, float max)