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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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::
263 get_material_index() const {
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  */
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  */
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  */
300 set_groups_mask(const PhysxGroupsMask &mask) {
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  */
310 get_groups_mask() const {
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  */
322 get_world_bounds() const {
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  */
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  */
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  */
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  */
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 }
Actors are the main simulation objects.
Definition: physxActor.h:44
Represention of a axis aligned bounding box.
Definition: physxBounds3.h:29
A box shaped collision detection primitive.
Definition: physxBoxShape.h:36
Represents an oriented bounding box, as a center point, extents(radii) and a rotation.
Definition: physxBox.h:29
A capsule shaped collision detection primitive, also known as a line swept sphere.
Represents a capsule.
Definition: physxCapsule.h:25
A Convex Mesh.
A shapes which is used to represent an instance of an convex mesh.
128-bit bitmask class.
This class is a shape instance of a height field object of type PhysxHeightField.
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:63
static NxMat34 mat4_to_nxMat34(const LMatrix4f &m)
Converts from LMatrix4f to NxMat34.
Definition: physxManager.I:119
static LPoint3f nxVec3_to_point3(const NxVec3 &p)
Converts from NxVec3 to LPoint3f.
Definition: physxManager.I:72
static LMatrix4f nxMat34_to_mat4(const NxMat34 &m)
Converts from NxMat34 to LMatrix4f.
Definition: physxManager.I:130
A class for describing a shape's surface properties.
Definition: physxMaterial.h:44
A plane collision detection primitive.
Represents an ray as an origin and direction.
Definition: physxRay.h:26
This structure captures results for a single raycast query.
Abstract base class for shapes.
Definition: physxShape.h:39
void set_local_pos(const LPoint3f &pos)
Set the position of the shape in actor space, i.e.
Definition: physxShape.cxx:210
void set_groups_mask(const PhysxGroupsMask &mask)
Sets 128-bit mask used for collision filtering.
Definition: physxShape.cxx:300
LPoint3f get_local_pos() const
Retrieve the position of the shape in actor space, i.e.
Definition: physxShape.cxx:222
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_group(unsigned short group)
Sets which collision group this shape is part of.
Definition: physxShape.cxx:179
PhysxGroupsMask get_groups_mask() const
Gets 128-bit mask used for collision filtering.
Definition: physxShape.cxx:310
void set_material_index(unsigned short idx)
Assigns a material index to the shape.
Definition: physxShape.cxx:289
bool get_flag(const PhysxShapeFlag flag) const
Returns the specified shape flag.
Definition: physxShape.cxx:141
void set_name(const char *name)
Sets a name string for this object.
Definition: physxShape.cxx:100
void set_material(const PhysxMaterial &material)
Assigns a material to the shape.
Definition: physxShape.cxx:274
bool check_overlap_obb(const PhysxBox &world_box) const
Checks whether the shape overlaps a world-space OBB or not.
Definition: physxShape.cxx:354
void set_skin_width(float skinWidth)
Sets the skin width.
Definition: physxShape.cxx:152
const char * get_name() const
Returns the name string.
Definition: physxShape.cxx:112
float get_skin_width() const
Returns the skin width.
Definition: physxShape.cxx:164
bool check_overlap_sphere(const PhysxSphere &world_sphere) const
Checks whether the shape overlaps a world-space sphere or not.
Definition: physxShape.cxx:364
void set_local_mat(const LMatrix4f &mat)
Set the transform of the shape in actor space, i.e.
Definition: physxShape.cxx:240
unsigned short get_group() const
Retrieves the collision group set for this shape.
Definition: physxShape.cxx:192
LMatrix4f get_local_mat() const
Retrieve the transform of the shape in actor space, i.e.
Definition: physxShape.cxx:252
unsigned short get_material_index() const
Returns the material index currently assigned to the shape.
Definition: physxShape.cxx:263
void set_flag(const PhysxShapeFlag flag, bool value)
Sets the specified shape flag.
Definition: physxShape.cxx:130
PhysxBounds3 get_world_bounds() const
Returns a world space AABB enclosing this shape.
Definition: physxShape.cxx:322
bool check_overlap_capsule(const PhysxCapsule &world_capsule) const
Checks whether the shape overlaps a world-space capsule or not.
Definition: physxShape.cxx:344
PhysxActor * get_actor() const
Retrieves the actor which this shape is associated with.
Definition: physxShape.cxx:89
A sphere shaped collision detection primitive.
Represents a sphere defined by its center point and radius.
Definition: physxSphere.h:25
A shapes which is used to represent an instance of an convex mesh.
A special shape used for simulating a car wheel.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.