Panda3D
physxMaterial.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file physxMaterial.cxx
10  * @author enn0x
11  * @date 2009-09-21
12  */
13 
14 #include "physxMaterial.h"
15 #include "physxMaterialDesc.h"
16 #include "physxManager.h"
17 
18 TypeHandle PhysxMaterial::_type_handle;
19 
20 /**
21  *
22  */
23 void PhysxMaterial::
24 link(NxMaterial *materialPtr) {
25 
26  // Link self
27  _ptr = materialPtr;
28  _ptr->userData = this;
29  _error_type = ET_ok;
30 
31  PhysxScene *scene = (PhysxScene *)_ptr->getScene().userData;
32  scene->_materials.add(this);
33 }
34 
35 /**
36  *
37  */
38 void PhysxMaterial::
39 unlink() {
40 
41  // Unlink self
42  _ptr->userData = nullptr;
43  _error_type = ET_released;
44 
45  PhysxScene *scene = (PhysxScene *)_ptr->getScene().userData;
46  scene->_materials.remove(this);
47 }
48 
49 /**
50  *
51  */
52 void PhysxMaterial::
53 release() {
54 
55  nassertv(_error_type == ET_ok);
56 
57  unlink();
58  _ptr->getScene().releaseMaterial(*_ptr);
59  _ptr = nullptr;
60 }
61 
62 /**
63  * Returns the scene that owns this material.
64  */
66 get_scene() const {
67 
68  nassertr(_error_type == ET_ok, nullptr);
69  return (PhysxScene *)(_ptr->getScene().userData);
70 }
71 
72 /**
73  * Returns the material index for this material.
74  *
75  * Materials are associated with mesh faces and shapes using material index
76  * identifiers.
77  *
78  * If you release a material while its material index is still in use by
79  * shapes or meshes, the material usage of these objects becomes undefined as
80  * the material index gets recycled.
81  */
82 unsigned short PhysxMaterial::
84 
85  nassertr(_error_type == ET_ok, 0);
86  return _ptr->getMaterialIndex();
87 }
88 
89 /**
90  * Loads the entire state of the material from a descriptor with a single
91  * call.
92  */
93 void PhysxMaterial::
94 load_from_desc(const PhysxMaterialDesc &materialDesc) {
95 
96  nassertv(_error_type == ET_ok);
97  _ptr->loadFromDesc(materialDesc._desc);
98 }
99 
100 /**
101  * Saves the state of the material object to a descriptor.
102  */
103 void PhysxMaterial::
104 save_to_desc(PhysxMaterialDesc & materialDesc) const {
105 
106  nassertv(_error_type == ET_ok);
107  _ptr->saveToDesc(materialDesc._desc);
108 }
109 
110 /**
111  * Sets the coefficient of restitution. A coefficient of 0 makes the object
112  * bounce as little as possible, higher values up to 1.0 result in more
113  * bounce.
114  */
115 void PhysxMaterial::
116 set_restitution(float restitution) {
117 
118  nassertv(_error_type == ET_ok);
119  _ptr->setRestitution(restitution);
120 }
121 
122 /**
123  * Returns the coefficient of restitution.
124  */
125 float PhysxMaterial::
127 
128  nassertr(_error_type == ET_ok, 0.0f);
129  return _ptr->getRestitution();
130 }
131 
132 /**
133  * Sets the coefficient of static friction. The coefficient of static
134  * friction should be in the range [0, +inf]. If the flag MF_anisotropic is
135  * set, then this value is used for the primary direction of anisotropy (U
136  * axis).
137  */
138 void PhysxMaterial::
139 set_static_friction(float coef) {
140 
141  nassertv(_error_type == ET_ok);
142  _ptr->setStaticFriction(coef);
143 }
144 
145 /**
146  * Returns the coefficient of static friction.
147  */
148 float PhysxMaterial::
150 
151  nassertr(_error_type == ET_ok, 0.0f);
152  return _ptr->getStaticFriction();
153 }
154 
155 /**
156  * Sets the coefficient of dynamic friction. The coefficient of dynamic
157  * friction should be in [0, +inf]. If set to greater than staticFriction, the
158  * effective value of staticFriction will be increased to match. If the flag
159  * MF_anisotropic is set, then this value is used for the primary direction of
160  * anisotropy (U axis).
161  */
162 void PhysxMaterial::
163 set_dynamic_friction(float coef) {
164 
165  nassertv(_error_type == ET_ok);
166  _ptr->setDynamicFriction(coef);
167 }
168 
169 /**
170  * Returns the DynamicFriction value.
171  */
172 float PhysxMaterial::
174 
175  nassertr(_error_type == ET_ok, 0.0f);
176  return _ptr->getDynamicFriction();
177 }
178 
179 /**
180  * Sets the static friction coefficient along the secondary (V) axis. This is
181  * used when anisotropic friction is being applied. I.e. the flag
182  * MF_anisotropic is set.
183  */
184 void PhysxMaterial::
186 
187  nassertv(_error_type == ET_ok);
188  _ptr->setStaticFrictionV(coef);
189 }
190 
191 /**
192  * Returns the static friction coefficient for the V direction.
193  */
194 float PhysxMaterial::
196 
197  nassertr(_error_type == ET_ok, 0.0f);
198  return _ptr->getStaticFrictionV();
199 }
200 
201 /**
202  * Sets the dynamic friction coefficient along the secondary (V) axis. This
203  * is used when anisotropic friction is being applied. I.e. the flag
204  * MF_anisotropic is set.
205  */
206 void PhysxMaterial::
208 
209  nassertv(_error_type == ET_ok);
210  _ptr->setDynamicFrictionV(coef);
211 }
212 
213 /**
214  * Returns the dynamic friction coefficient for the V direction.
215  */
216 float PhysxMaterial::
218 
219  nassertr(_error_type == ET_ok, 0.0f);
220  return _ptr->getDynamicFrictionV();
221 }
222 
223 /**
224  * Sets the value of a single flag.
225  */
226 void PhysxMaterial::
227 set_flag(PhysxMaterialFlag flag, bool value) {
228 
229  nassertv(_error_type == ET_ok);
230  NxU32 flags = _ptr->getFlags();
231 
232  if (value == true) {
233  flags |= flag;
234  }
235  else {
236  flags &= ~(flag);
237  }
238 
239  _ptr->setFlags(flags);
240 }
241 
242 /**
243  * Returns the value of a single flag.
244  */
245 bool PhysxMaterial::
246 get_flag(PhysxMaterialFlag flag) const {
247 
248  nassertr(_error_type == ET_ok, false);
249  return (_ptr->getFlags() & flag) ? true : false;
250 }
251 
252 /**
253  * Sets the shape space direction (unit vector) of anisotropy. This is only
254  * used if the flag MF_anisotropic is set.
255  */
256 void PhysxMaterial::
257 set_dir_of_anisotropy(const LVector3f dir) {
258 
259  nassertv(_error_type == ET_ok);
260  _ptr->setDirOfAnisotropy(PhysxManager::vec3_to_nxVec3(dir));
261 }
262 
263 /**
264  * Returns the direction of anisotropy value.
265  */
266 LVector3f PhysxMaterial::
268 
269  nassertr(_error_type == ET_ok, LVector3f::zero());
270  return PhysxManager::nxVec3_to_vec3(_ptr->getDirOfAnisotropy());
271 }
272 
273 /**
274  * Sets the friction combine mode. - CM_average : Average: (a + b)/2. -
275  * CM_min : Minimum: min(a,b). - CM_multiply : Multiply: a*b. - CM_max :
276  * Maximum: max(a,b).
277  */
278 void PhysxMaterial::
279 set_friction_combine_mode(PhysxCombineMode mode) {
280 
281  nassertv(_error_type == ET_ok);
282  _ptr->setFrictionCombineMode((NxCombineMode)mode);
283 }
284 
285 /**
286  * Returns the friction combine mode.
287  */
288 PhysxEnums::PhysxCombineMode PhysxMaterial::
290 
291  nassertr(_error_type == ET_ok, CM_average);
292  return (PhysxCombineMode)_ptr->getFrictionCombineMode();
293 }
294 
295 /**
296  * Sets the restitution combine mode. - CM_average : Average: (a + b)/2. -
297  * CM_min : Minimum: min(a,b). - CM_multiply : Multiply: a*b. - CM_max :
298  * Maximum: max(a,b).
299  */
300 void PhysxMaterial::
301 set_restitution_combine_mode(PhysxCombineMode mode) {
302 
303  nassertv(_error_type == ET_ok);
304  _ptr->setRestitutionCombineMode((NxCombineMode)mode);
305 }
306 
307 /**
308  * Returns the restitution combine mode.
309  */
310 PhysxEnums::PhysxCombineMode PhysxMaterial::
312 
313  nassertr(_error_type == ET_ok, CM_average);
314  return (PhysxCombineMode)_ptr->getRestitutionCombineMode();
315 }
Descriptor class for materials.
PhysxScene * get_scene() const
Returns the scene that owns this material.
float get_static_friction() const
Returns the coefficient of static friction.
void set_static_friction_v(float coef)
Sets the static friction coefficient along the secondary (V) axis.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void load_from_desc(const PhysxMaterialDesc &materialDesc)
Loads the entire state of the material from a descriptor with a single call.
float get_static_friction_v() const
Returns the static friction coefficient for the V direction.
void set_restitution_combine_mode(PhysxCombineMode mode)
Sets the restitution combine mode.
PhysxCombineMode get_restitution_combine_mode() const
Returns the restitution combine mode.
A scene is a collection of bodies, constraints, and effectors which can interact.
Definition: physxScene.h:69
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LVector3f get_dir_of_anisotropy() const
Returns the direction of anisotropy value.
float get_dynamic_friction() const
Returns the DynamicFriction value.
PhysxCombineMode get_friction_combine_mode() const
Returns the friction combine mode.
void set_static_friction(float coef)
Sets the coefficient of static friction.
static NxVec3 vec3_to_nxVec3(const LVector3f &v)
Converts from LVector3f to NxVec3.
Definition: physxManager.I:27
static LVector3f nxVec3_to_vec3(const NxVec3 &v)
Converts from NxVec3 to LVector3f.
Definition: physxManager.I:36
float get_dynamic_friction_v() const
Returns the dynamic friction coefficient for the V direction.
unsigned short get_material_index() const
Returns the material index for this material.
void set_dynamic_friction_v(float coef)
Sets the dynamic friction coefficient along the secondary (V) axis.
void set_flag(PhysxMaterialFlag flag, bool value)
Sets the value of a single flag.
void set_friction_combine_mode(PhysxCombineMode mode)
Sets the friction combine mode.
float get_restitution() const
Returns the coefficient of restitution.
void set_dynamic_friction(float coef)
Sets the coefficient of dynamic friction.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
bool get_flag(PhysxMaterialFlag flag) const
Returns the value of a single flag.
void save_to_desc(PhysxMaterialDesc &materialDesc) const
Saves the state of the material object to a descriptor.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_restitution(float rest)
Sets the coefficient of restitution.
void set_dir_of_anisotropy(const LVector3f dir)
Sets the shape space direction (unit vector) of anisotropy.