| 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 real PhysicsMaterial::staticFriction |
| 37 | This property defines the amount of friction that is applied between surfaces that are not |
| 38 | moving lateral to each-other. |
| 39 | |
| 40 | Default value: \c 0.5 |
| 41 | |
| 42 | Range: \c{[0, inf]} |
| 43 | */ |
| 44 | |
| 45 | /*! |
| 46 | \qmlproperty real PhysicsMaterial::dynamicFriction |
| 47 | This property defines the amount of friction applied between surfaces that are moving relative |
| 48 | to each-other. |
| 49 | |
| 50 | Default value: \c 0.5 |
| 51 | |
| 52 | Range: \c{[0, inf]} |
| 53 | */ |
| 54 | |
| 55 | /*! |
| 56 | \qmlproperty real PhysicsMaterial::restitution |
| 57 | This property defines the coefficient of restitution, or how bouncy the material is. |
| 58 | The coefficient of restitution of two |
| 59 | colliding objects is a fractional value representing the ratio of speeds after and before an |
| 60 | impact, taken along the line of impact. A coefficient of restitution of 1 is said to collide |
| 61 | elastically, while a coefficient of restitution < 1 is said to be inelastic. |
| 62 | |
| 63 | Default value: \c 0.5 |
| 64 | |
| 65 | Range: \c{[0, 1]} |
| 66 | */ |
| 67 | |
| 68 | QPhysicsMaterial::QPhysicsMaterial(QObject *parent) : QObject(parent) { } |
| 69 | |
| 70 | float QPhysicsMaterial::staticFriction() const |
| 71 | { |
| 72 | return m_staticFriction; |
| 73 | } |
| 74 | |
| 75 | void QPhysicsMaterial::setStaticFriction(float staticFriction) |
| 76 | { |
| 77 | staticFriction = clamp(value: staticFriction, min: 0.f, PX_MAX_F32); |
| 78 | |
| 79 | if (qFuzzyCompare(p1: m_staticFriction, p2: staticFriction)) |
| 80 | return; |
| 81 | m_staticFriction = staticFriction; |
| 82 | emit staticFrictionChanged(); |
| 83 | } |
| 84 | |
| 85 | float QPhysicsMaterial::dynamicFriction() const |
| 86 | { |
| 87 | return m_dynamicFriction; |
| 88 | } |
| 89 | |
| 90 | void QPhysicsMaterial::setDynamicFriction(float dynamicFriction) |
| 91 | { |
| 92 | dynamicFriction = clamp(value: dynamicFriction, min: 0.f, PX_MAX_F32); |
| 93 | |
| 94 | if (qFuzzyCompare(p1: m_dynamicFriction, p2: dynamicFriction)) |
| 95 | return; |
| 96 | m_dynamicFriction = dynamicFriction; |
| 97 | emit dynamicFrictionChanged(); |
| 98 | } |
| 99 | |
| 100 | float QPhysicsMaterial::restitution() const |
| 101 | { |
| 102 | return m_restitution; |
| 103 | } |
| 104 | |
| 105 | void QPhysicsMaterial::setRestitution(float restitution) |
| 106 | { |
| 107 | restitution = clamp(value: restitution, min: 0.f, max: 1.f); |
| 108 | |
| 109 | if (qFuzzyCompare(p1: m_restitution, p2: restitution)) |
| 110 | return; |
| 111 | m_restitution = restitution; |
| 112 | emit restitutionChanged(); |
| 113 | } |
| 114 | |
| 115 | QT_END_NAMESPACE |
| 116 | |