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