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