aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3dphysics/qphysicsmaterial.cpp
blob: 1ec6e3e3d7ccc1cf88af9fb60e89fc087da790bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qphysicsmaterial_p.h"

#include <foundation/PxSimpleTypes.h>

static float clamp(float value, float min, float max)
{
    return std::max(std::min(value, max), min);
}

QT_BEGIN_NAMESPACE

/*!
    \qmltype PhysicsMaterial
    \inqmlmodule QtQuick3D.Physics
    \since 6.4
    \brief Defines the physics material of a body.

    The PhysicsMaterial type determines how objects interact when they touch.

    Friction uses the Coulomb friction model, which is based around
    the concepts of 2 coefficients: the static friction coefficient and the dynamic friction
    coefficient (sometimes called kinetic friction). Friction resists relative lateral motion of two
    solid surfaces in contact. These two coefficients define a relationship between the normal force
    exerted by each surface on the other and the amount of friction force that is applied to resist
    lateral motion. While most real-world materials have friction coefficients between \c{0} and
    \c{1}, values above \c{1} are not uncommon. The properties accept any real number greater or
    equal to \c{0}.

    Restitution determines how objects bounce when they collide.
*/

/*!
    \qmlproperty real PhysicsMaterial::staticFriction
    This property defines the amount of friction that is applied between surfaces that are not
    moving lateral to each-other.

    Default value: \c 0.5

    Range: \c{[0, inf]}
*/

/*!
    \qmlproperty real PhysicsMaterial::dynamicFriction
    This property defines the amount of friction applied between surfaces that are moving relative
    to each-other.

    Default value: \c 0.5

    Range: \c{[0, inf]}
*/

/*!
    \qmlproperty real PhysicsMaterial::restitution
    This property defines the coefficient of restitution, or how bouncy the material is.
    The coefficient of restitution of two
    colliding objects is a fractional value representing the ratio of speeds after and before an
    impact, taken along the line of impact. A coefficient of restitution of 1 is said to collide
    elastically, while a coefficient of restitution < 1 is said to be inelastic.

    Default value: \c 0.5

    Range: \c{[0, 1]}
*/

QPhysicsMaterial::QPhysicsMaterial(QObject *parent) : QObject(parent) { }

float QPhysicsMaterial::staticFriction() const
{
    return m_staticFriction;
}

void QPhysicsMaterial::setStaticFriction(float staticFriction)
{
    staticFriction = clamp(staticFriction, 0.f, PX_MAX_F32);

    if (qFuzzyCompare(m_staticFriction, staticFriction))
        return;
    m_staticFriction = staticFriction;
    emit staticFrictionChanged();
}

float QPhysicsMaterial::dynamicFriction() const
{
    return m_dynamicFriction;
}

void QPhysicsMaterial::setDynamicFriction(float dynamicFriction)
{
    dynamicFriction = clamp(dynamicFriction, 0.f, PX_MAX_F32);

    if (qFuzzyCompare(m_dynamicFriction, dynamicFriction))
        return;
    m_dynamicFriction = dynamicFriction;
    emit dynamicFrictionChanged();
}

float QPhysicsMaterial::restitution() const
{
    return m_restitution;
}

void QPhysicsMaterial::setRestitution(float restitution)
{
    restitution = clamp(restitution, 0.f, 1.f);

    if (qFuzzyCompare(m_restitution, restitution))
        return;
    m_restitution = restitution;
    emit restitutionChanged();
}

QT_END_NAMESPACE