Panda3D
physxShape.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 physxShape.cxx
10  * @author enn0x
11  * @date 2009-09-16
12  */
13 
14 #include "physxShape.h"
15 #include "physxManager.h"
16 #include "physxActor.h"
17 #include "physxBoxShape.h"
18 #include "physxCapsuleShape.h"
19 #include "physxPlaneShape.h"
20 #include "physxSphereShape.h"
21 #include "physxConvexShape.h"
22 #include "physxHeightFieldShape.h"
23 #include "physxTriangleMeshShape.h"
24 #include "physxWheelShape.h"
25 #include "physxGroupsMask.h"
26 #include "physxBounds3.h"
27 #include "physxSphere.h"
28 #include "physxBox.h"
29 #include "physxCapsule.h"
30 #include "physxRay.h"
31 #include "physxRaycastHit.h"
32 #include "physxCcdSkeleton.h"
33 
34 TypeHandle PhysxShape::_type_handle;
35 
36 /**
37  *
38  */
39 void PhysxShape::
40 release() {
41 
42  nassertv(_error_type == ET_ok);
43 
44  unlink();
45  ptr()->getActor().releaseShape(*ptr());
46 }
47 
48 /**
49  *
50  */
51 PhysxShape *PhysxShape::
52 factory(NxShapeType shapeType) {
53 
54  switch (shapeType) {
55 
56  case NX_SHAPE_PLANE:
57  return new PhysxPlaneShape();
58 
59  case NX_SHAPE_SPHERE:
60  return new PhysxSphereShape();
61 
62  case NX_SHAPE_BOX:
63  return new PhysxBoxShape();
64 
65  case NX_SHAPE_CAPSULE:
66  return new PhysxCapsuleShape();
67 
68  case NX_SHAPE_CONVEX:
69  return new PhysxConvexShape();
70 
71  case NX_SHAPE_MESH:
72  return new PhysxTriangleMeshShape();
73 
74  case NX_SHAPE_HEIGHTFIELD:
75  return new PhysxHeightFieldShape();
76 
77  case NX_SHAPE_WHEEL:
78  return new PhysxWheelShape();
79  }
80 
81  physx_cat.error() << "Unknown shape type.\n";
82  return nullptr;
83 }
84 
85 /**
86  * Retrieves the actor which this shape is associated with.
87  */
89 get_actor() const {
90 
91  nassertr(_error_type == ET_ok, nullptr);
92  return (PhysxActor *)(ptr()->getActor().userData);
93 }
94 
95 /**
96  * Sets a name string for this object. The name can be retrieved again with
97  * get_name(). This is for debugging and is not used by the physics engine.
98  */
99 void PhysxShape::
100 set_name(const char *name) {
101 
102  nassertv(_error_type == ET_ok);
103 
104  _name = name ? name : "";
105  ptr()->setName(_name.c_str());
106 }
107 
108 /**
109  * Returns the name string.
110  */
111 const char *PhysxShape::
112 get_name() const {
113 
114  nassertr(_error_type == ET_ok, "");
115  return ptr()->getName();
116 }
117 
118 /**
119  * Sets the specified shape flag.
120  *
121  * The shape may be turned into a trigger by setting one or more of the
122  * TriggerFlags to true. A trigger shape will not collide with other shapes.
123  * Instead, if a shape enters the trigger's volume, a trigger event will be
124  * sent. Trigger events can be listened to by DirectObjects.
125  *
126  * The following trigger events can be sent: - physx-trigger-enter - physx-
127  * trigger-stay - physx-trigger-leave
128  */
129 void PhysxShape::
130 set_flag(PhysxShapeFlag flag, bool value) {
131 
132  nassertv(_error_type == ET_ok);
133 
134  ptr()->setFlag((NxShapeFlag)flag, value);
135 }
136 
137 /**
138  * Returns the specified shape flag.
139  */
140 bool PhysxShape::
141 get_flag(PhysxShapeFlag flag) const {
142 
143  nassertr(_error_type == ET_ok, false);
144 
145  return (ptr()->getFlag((NxShapeFlag)flag)) ? true : false;
146 }
147 
148 /**
149  * Sets the skin width. The skin width must be non-negative.
150  */
151 void PhysxShape::
152 set_skin_width(float skinWidth) {
153 
154  nassertv(_error_type == ET_ok);
155  nassertv(skinWidth >= 0.0f);
156 
157  ptr()->setSkinWidth(skinWidth);
158 }
159 
160 /**
161  * Returns the skin width.
162  */
163 float PhysxShape::
164 get_skin_width() const {
165 
166  nassertr(_error_type == ET_ok, 0.0f);
167 
168  return ptr()->getSkinWidth();
169 }
170 
171 /**
172  * Sets which collision group this shape is part of.
173  *
174  * Default group is 0. Maximum possible group is 31. Collision groups are sets
175  * of shapes which may or may not be set to collision detect with each other;
176  * this can be set using PhysxScene::set_group_collision_flag().
177  */
178 void PhysxShape::
179 set_group(unsigned short group) {
180 
181  nassertv(_error_type == ET_ok);
182  nassertv(group < 32);
183 
184  ptr()->setGroup(group);
185 }
186 
187 /**
188  * Retrieves the collision group set for this shape. The collision group is
189  * an integer between 0 and 31.
190  */
191 unsigned short PhysxShape::
192 get_group() const {
193 
194  nassertr(_error_type == ET_ok, 0);
195 
196  return ptr()->getGroup();
197 }
198 
199 /**
200  * Set the position of the shape in actor space, i.e. relative to the actor
201  * it is owned by.
202  *
203  * Calling this method does NOT wake the associated actor up automatically.
204  *
205  * Calling this method does not automatically update the inertia properties of
206  * the owning actor (if applicable); use PhysxActor::update_mass_from_shapes()
207  * to do this.
208  */
209 void PhysxShape::
210 set_local_pos(const LPoint3f &pos) {
211 
212  nassertv(_error_type == ET_ok);
213 
214  ptr()->setLocalPosition(PhysxManager::point3_to_nxVec3(pos));
215 }
216 
217 /**
218  * Retrieve the position of the shape in actor space, i.e. relative to the
219  * actor it is owned by.
220  */
221 LPoint3f PhysxShape::
222 get_local_pos() const {
223 
224  nassertr(_error_type == ET_ok, LPoint3f::zero());
225 
226  return PhysxManager::nxVec3_to_point3(ptr()->getLocalPosition());
227 }
228 
229 /**
230  * Set the transform of the shape in actor space, i.e. relative to the actor
231  * it is owned by.
232  *
233  * Calling this method does NOT wake the associated actor up automatically.
234  *
235  * Calling this method does not automatically update the inertia properties of
236  * the owning actor (if applicable); use PhysxActor::update_mass_from_shapes()
237  * to do this.
238  */
239 void PhysxShape::
240 set_local_mat(const LMatrix4f &mat) {
241 
242  nassertv(_error_type == ET_ok);
243 
244  ptr()->setLocalPose(PhysxManager::mat4_to_nxMat34(mat));
245 }
246 
247 /**
248  * Retrieve the transform of the shape in actor space, i.e. relative to the
249  * actor it is owned by.
250  */
251 LMatrix4f PhysxShape::
252 get_local_mat() const {
253 
254  nassertr(_error_type == ET_ok, LMatrix4f::zeros_mat());
255 
256  return PhysxManager::nxMat34_to_mat4(ptr()->getLocalPose());
257 }
258 
259 /**
260  * Returns the material index currently assigned to the shape.
261  */
262 unsigned short PhysxShape::
264 
265  nassertr(_error_type == ET_ok, 0);
266  NxMaterialIndex index = ptr()->getMaterial();
267  return (unsigned int)index;
268 }
269 
270 /**
271  * Assigns a material to the shape.
272  */
273 void PhysxShape::
274 set_material(const PhysxMaterial &material) {
275 
276  nassertv(_error_type == ET_ok);
277  ptr()->setMaterial(material.ptr()->getMaterialIndex());
278 }
279 
280 /**
281  * Assigns a material index to the shape.
282  *
283  * The material index can be retrieved by calling
284  * PhysxMaterial::get_material_index(). If the material index is invalid, it
285  * will still be recorded, but the default material (at index 0) will
286  * effectively be used for simulation.
287  */
288 void PhysxShape::
289 set_material_index(unsigned short index) {
290 
291  nassertv(_error_type == ET_ok);
292  ptr()->setMaterial((NxMaterialIndex)index);
293 }
294 
295 /**
296  * Sets 128-bit mask used for collision filtering. Does NOT wake the
297  * associated actor up automatically.
298  */
299 void PhysxShape::
301 
302  nassertv(_error_type == ET_ok);
303  ptr()->setGroupsMask(mask.get_mask());
304 }
305 
306 /**
307  * Gets 128-bit mask used for collision filtering.
308  */
311 
312  PhysxGroupsMask mask;
313  nassertr(_error_type == ET_ok, mask);
314  mask.set_mask(ptr()->getGroupsMask());
315  return mask;
316 }
317 
318 /**
319  * Returns a world space AABB enclosing this shape.
320  */
323 
324  PhysxBounds3 bounds;
325  nassertr(_error_type == ET_ok, bounds);
326  ptr()->getWorldBounds(bounds._bounds);
327  return bounds;
328 }
329 
330 /**
331  * Checks whether the shape overlaps a world-space AABB or not.
332  */
333 bool PhysxShape::
334 check_overlap_aabb(const PhysxBounds3 &world_bounds) const {
335 
336  nassertr(_error_type == ET_ok, false);
337  return ptr()->checkOverlapAABB(world_bounds._bounds);
338 }
339 
340 /**
341  * Checks whether the shape overlaps a world-space capsule or not.
342  */
343 bool PhysxShape::
344 check_overlap_capsule(const PhysxCapsule &world_capsule) const {
345 
346  nassertr(_error_type == ET_ok, false);
347  return ptr()->checkOverlapCapsule(world_capsule._capsule);
348 }
349 
350 /**
351  * Checks whether the shape overlaps a world-space OBB or not.
352  */
353 bool PhysxShape::
354 check_overlap_obb(const PhysxBox &world_box) const {
355 
356  nassertr(_error_type == ET_ok, false);
357  return ptr()->checkOverlapOBB(world_box._box);
358 }
359 
360 /**
361  * Checks whether the shape overlaps a world-space sphere or not.
362  */
363 bool PhysxShape::
364 check_overlap_sphere(const PhysxSphere &world_sphere) const {
365 
366  nassertr(_error_type == ET_ok, false);
367  return ptr()->checkOverlapSphere(world_sphere._sphere);
368 }
369 
370 /**
371  *
372  */
373 PhysxRaycastHit PhysxShape::
374 raycast(const PhysxRay &worldRay, bool firstHit, bool smoothNormal) const {
375 
376  NxRaycastHit hit;
377  nassertr(_error_type == ET_ok, hit);
378 
379  NxU32 hints = NX_RAYCAST_SHAPE | NX_RAYCAST_IMPACT | NX_RAYCAST_DISTANCE;
380  if (smoothNormal == true) {
381  hints |= NX_RAYCAST_NORMAL;
382  }
383  else {
384  hints |= NX_RAYCAST_FACE_NORMAL;
385  }
386 
387  ptr()->raycast(worldRay._ray, worldRay._length, hints, hit, firstHit);
388  return PhysxRaycastHit(hit);
389 }
390 
391 /**
392  *
393  */
394 void PhysxShape::
395 set_ccd_skeleton(PhysxCcdSkeleton *skel) {
396 
397  nassertv(_error_type == ET_ok);
398 
399  ptr()->setCCDSkeleton(skel->ptr());
400  _skel = skel;
401 }
402 
403 /**
404  *
405  */
406 PhysxCcdSkeleton *PhysxShape::
407 get_ccd_skeleton() const {
408 
409  nassertr(_error_type == ET_ok, nullptr);
410 
411  return _skel;
412 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
unsigned short get_material_index() const
Returns the material index currently assigned to the shape.
Definition: physxShape.cxx:263
PhysxActor * get_actor() const
Retrieves the actor which this shape is associated with.
Definition: physxShape.cxx:89
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents a sphere defined by its center point and radius.
Definition: physxSphere.h:25
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_group(unsigned short group)
Sets which collision group this shape is part of.
Definition: physxShape.cxx:179
void set_local_mat(const LMatrix4f &mat)
Set the transform of the shape in actor space, i.e.
Definition: physxShape.cxx:240
bool check_overlap_obb(const PhysxBox &world_box) const
Checks whether the shape overlaps a world-space OBB or not.
Definition: physxShape.cxx:354
Represents an oriented bounding box, as a center point, extents(radii) and a rotation.
Definition: physxBox.h:29
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:63
unsigned short get_group() const
Retrieves the collision group set for this shape.
Definition: physxShape.cxx:192
Abstract base class for shapes.
Definition: physxShape.h:39
This class is a shape instance of a height field object of type PhysxHeightField.
Represention of a axis aligned bounding box.
Definition: physxBounds3.h:29
const char * get_name() const
Returns the name string.
Definition: physxShape.cxx:112
A plane collision detection primitive.
A sphere shaped collision detection primitive.
bool check_overlap_aabb(const PhysxBounds3 &world_bounds) const
Checks whether the shape overlaps a world-space AABB or not.
Definition: physxShape.cxx:334
void set_flag(const PhysxShapeFlag flag, bool value)
Sets the specified shape flag.
Definition: physxShape.cxx:130
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A class for describing a shape's surface properties.
Definition: physxMaterial.h:44
static NxMat34 mat4_to_nxMat34(const LMatrix4f &m)
Converts from LMatrix4f to NxMat34.
Definition: physxManager.I:119
LMatrix4f get_local_mat() const
Retrieve the transform of the shape in actor space, i.e.
Definition: physxShape.cxx:252
void set_material(const PhysxMaterial &material)
Assigns a material to the shape.
Definition: physxShape.cxx:274
void set_local_pos(const LPoint3f &pos)
Set the position of the shape in actor space, i.e.
Definition: physxShape.cxx:210
This structure captures results for a single raycast query.
void set_skin_width(float skinWidth)
Sets the skin width.
Definition: physxShape.cxx:152
bool check_overlap_sphere(const PhysxSphere &world_sphere) const
Checks whether the shape overlaps a world-space sphere or not.
Definition: physxShape.cxx:364
bool check_overlap_capsule(const PhysxCapsule &world_capsule) const
Checks whether the shape overlaps a world-space capsule or not.
Definition: physxShape.cxx:344
float get_skin_width() const
Returns the skin width.
Definition: physxShape.cxx:164
A box shaped collision detection primitive.
Definition: physxBoxShape.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents a capsule.
Definition: physxCapsule.h:25
A special shape used for simulating a car wheel.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Actors are the main simulation objects.
Definition: physxActor.h:44
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A Convex Mesh.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_material_index(unsigned short idx)
Assigns a material index to the shape.
Definition: physxShape.cxx:289
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A shapes which is used to represent an instance of an convex mesh.
void set_name(const char *name)
Sets a name string for this object.
Definition: physxShape.cxx:100
LPoint3f get_local_pos() const
Retrieve the position of the shape in actor space, i.e.
Definition: physxShape.cxx:222
void set_groups_mask(const PhysxGroupsMask &mask)
Sets 128-bit mask used for collision filtering.
Definition: physxShape.cxx:300
static LPoint3f nxVec3_to_point3(const NxVec3 &p)
Converts from NxVec3 to LPoint3f.
Definition: physxManager.I:72
PhysxBounds3 get_world_bounds() const
Returns a world space AABB enclosing this shape.
Definition: physxShape.cxx:322
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool get_flag(const PhysxShapeFlag flag) const
Returns the specified shape flag.
Definition: physxShape.cxx:141
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static LMatrix4f nxMat34_to_mat4(const NxMat34 &m)
Converts from NxMat34 to LMatrix4f.
Definition: physxManager.I:130
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A capsule shaped collision detection primitive, also known as a line swept sphere.
PhysxGroupsMask get_groups_mask() const
Gets 128-bit mask used for collision filtering.
Definition: physxShape.cxx:310
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents an ray as an origin and direction.
Definition: physxRay.h:26
A shapes which is used to represent an instance of an convex mesh.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
128-bit bitmask class.