1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | |
31 | #ifndef PXS_MATERIALCOMBINER_H |
32 | #define PXS_MATERIALCOMBINER_H |
33 | |
34 | #include "PxsMaterialCore.h" |
35 | |
36 | namespace physx |
37 | { |
38 | |
39 | class PxsMaterialCombiner |
40 | { |
41 | public: |
42 | |
43 | class PxsCombinedMaterial |
44 | { |
45 | public: |
46 | PxReal staFriction; |
47 | PxReal dynFriction; |
48 | PxU32 flags; //PxMaterialFlag::eDISABLE_FRICTION, PxMaterialFlag::eDISABLE_STRONG_FRICTION. |
49 | }; |
50 | |
51 | static PxReal combineRestitution(const PxsMaterialData& material0, const PxsMaterialData& material1); |
52 | |
53 | PxsMaterialCombiner(PxReal staticFrictionScaling, PxReal dynamicFrictionScaling); |
54 | |
55 | PxsCombinedMaterial combineIsotropicFriction(const PxsMaterialData& material0, const PxsMaterialData& material1); |
56 | |
57 | //ML:: move this function to header file to avoid LHS in Xbox |
58 | PX_FORCE_INLINE void combineIsotropicFriction(const PxsMaterialData& mat0, const PxsMaterialData& mat1, PxReal& dynamicFriction, PxReal& staticFriction, PxU32& flags) |
59 | { |
60 | |
61 | const PxU32 combineFlags= (mat0.flags | mat1.flags); //& (PxMaterialFlag::eDISABLE_STRONG_FRICTION|PxMaterialFlag::eDISABLE_FRICTION); //eventually set DisStrongFric flag, lower all others. |
62 | |
63 | if (!(combineFlags & PxMaterialFlag::eDISABLE_FRICTION)) |
64 | { |
65 | const PxI32 fictionCombineMode = PxMax(a: mat0.getFrictionCombineMode(), b: mat1.getFrictionCombineMode()); |
66 | PxReal dynFriction = 0.f; |
67 | PxReal staFriction = 0.f; |
68 | |
69 | |
70 | switch (fictionCombineMode) |
71 | { |
72 | case PxCombineMode::eAVERAGE: |
73 | dynFriction = 0.5f * (mat0.dynamicFriction + mat1.dynamicFriction); |
74 | staFriction = 0.5f * (mat0.staticFriction + mat1.staticFriction); |
75 | break; |
76 | case PxCombineMode::eMIN: |
77 | dynFriction = PxMin(a: mat0.dynamicFriction, b: mat1.dynamicFriction); |
78 | staFriction = PxMin(a: mat0.staticFriction, b: mat1.staticFriction); |
79 | break; |
80 | case PxCombineMode::eMULTIPLY: |
81 | dynFriction = (mat0.dynamicFriction * mat1.dynamicFriction); |
82 | staFriction = (mat0.staticFriction * mat1.staticFriction); |
83 | break; |
84 | case PxCombineMode::eMAX: |
85 | dynFriction = PxMax(a: mat0.dynamicFriction, b: mat1.dynamicFriction); |
86 | staFriction = PxMax(a: mat0.staticFriction, b: mat1.staticFriction); |
87 | break; |
88 | } |
89 | |
90 | dynFriction*=mDynamicFrictionScaling; |
91 | staFriction*=mStaticFrictionScaling; |
92 | //isotropic case |
93 | const PxReal fDynFriction = PxMax(a: dynFriction, b: 0.f); |
94 | |
95 | const PxReal fStaFriction = physx::intrinsics::fsel(a: staFriction - fDynFriction, b: staFriction*mStaticFrictionScaling, c: fDynFriction); |
96 | /*dest.dynFriction = fDynFriction; |
97 | dest.staFriction = fStaFriction;*/ |
98 | |
99 | dynamicFriction = fDynFriction; |
100 | staticFriction = fStaFriction; |
101 | flags = combineFlags; |
102 | } |
103 | else |
104 | { |
105 | /* dest.flags |= PxMaterialFlag::eDISABLE_STRONG_FRICTION; |
106 | dest.staFriction = 0.0f; |
107 | dest.dynFriction = 0.0f;*/ |
108 | flags = (combineFlags | PxMaterialFlag::eDISABLE_STRONG_FRICTION); |
109 | dynamicFriction = 0.f; |
110 | staticFriction = 0.f; |
111 | } |
112 | |
113 | } |
114 | |
115 | |
116 | //private: |
117 | protected: |
118 | static PX_FORCE_INLINE PxReal combineScalars(PxReal a, PxReal b, PxI32 nxCombineMode) |
119 | { |
120 | switch (nxCombineMode) |
121 | { |
122 | case PxCombineMode::eAVERAGE: |
123 | return 0.5f * (a + b); |
124 | case PxCombineMode::eMIN: |
125 | return PxMin(a,b); |
126 | case PxCombineMode::eMULTIPLY: |
127 | return a * b; |
128 | case PxCombineMode::eMAX: |
129 | return PxMax(a,b); |
130 | default: |
131 | /* Ps::getFoundation().error(PxErrorCode::eINVALID_OPERATION, __FILE__, __LINE__, |
132 | "Sc::MaterialCombiner::combineScalars(): unknown combine mode");*/ |
133 | return PxReal(0); |
134 | } |
135 | } |
136 | |
137 | PxReal mStaticFrictionScaling, mDynamicFrictionScaling; |
138 | |
139 | }; |
140 | |
141 | } |
142 | |
143 | #endif |
144 | |