Panda3D
physxForceFieldShapeGroup.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 physxForceFieldShapeGroup.cxx
10  * @author enn0x
11  * @date 2009-11-11
12  */
13 
16 #include "physxForceField.h"
17 #include "physxForceFieldShape.h"
18 
19 TypeHandle PhysxForceFieldShapeGroup::_type_handle;
20 
21 /**
22  *
23  */
24 void PhysxForceFieldShapeGroup::
25 link(NxForceFieldShapeGroup *groupPtr) {
26 
27  // Link self
28  _ptr = groupPtr;
29  _ptr->userData = this;
30  _error_type = ET_ok;
31 
32  set_name(groupPtr->getName());
33 
34  PhysxScene *scene = (PhysxScene *)_ptr->getScene().userData;
35  scene->_ffgroups.add(this);
36 
37  // Link shapes
38  NxU32 nShapes = _ptr->getNbShapes();
39  _ptr->resetShapesIterator();
40  for (NxU32 i=0; i < nShapes; i++) {
41  NxForceFieldShape *shapePtr = _ptr->getNextShape();
42  PhysxForceFieldShape *shape = PhysxForceFieldShape::factory(shapePtr->getType());
43  shape->link(shapePtr);
44  }
45 }
46 
47 /**
48  *
49  */
50 void PhysxForceFieldShapeGroup::
51 unlink() {
52 
53  // Unlink shapes
54  NxU32 nShapes = _ptr->getNbShapes();
55  _ptr->resetShapesIterator();
56  for (NxU32 i=0; i < nShapes; i++) {
57  NxForceFieldShape *shapePtr = _ptr->getNextShape();
58  PhysxForceFieldShape *shape = (PhysxForceFieldShape *)shapePtr->userData;
59  shape->unlink();
60  }
61 
62  // Unlink self
63  _ptr->userData = nullptr;
64  _error_type = ET_released;
65 
66  PhysxScene *scene = (PhysxScene *)_ptr->getScene().userData;
67  scene->_ffgroups.remove(this);
68 }
69 
70 /**
71  * Releases the force field shape.
72  */
74 release() {
75 
76  nassertv(_error_type == ET_ok);
77 
78  unlink();
79  _ptr->getScene().releaseForceFieldShapeGroup(*_ptr);
80  _ptr = nullptr;
81 }
82 
83 /**
84  * Returns the scene that owns this force field shape group.
85  */
87 get_scene() const {
88 
89  nassertr(_error_type == ET_ok, nullptr);
90  return (PhysxScene *)(_ptr->getScene().userData);
91 }
92 
93 /**
94  * Returns the force field of this group if this is an include group. If not
95  * NULL will be returned.
96  */
98 get_force_field() const {
99 
100  nassertr(_error_type == ET_ok, nullptr);
101 
102  if (_ptr->getForceField() == nullptr) {
103  return nullptr;
104  }
105  else {
106  return (PhysxForceField *)(_ptr->getForceField()->userData);
107  }
108 }
109 
110 /**
111  * Saves the state of the force field shape group object to a descriptor.
112  */
115 
116  nassertv(_error_type == ET_ok);
117  _ptr->saveToDesc(groupDesc._desc);
118 }
119 
120 /**
121  * Sets a name string for the object that can be retrieved with get_name().
122  * This is for debugging and is not used by the engine.
123  */
125 set_name(const char *name) {
126 
127  nassertv(_error_type == ET_ok);
128 
129  _name = name ? name : "";
130  _ptr->setName(_name.c_str());
131 }
132 
133 /**
134  * Returns the name string.
135  */
137 get_name() const {
138 
139  nassertr(_error_type == ET_ok, "");
140  return _ptr->getName();
141 }
142 
143 /**
144  * Returns the number of shapes assigned to the force field shape group.
145  */
146 unsigned int PhysxForceFieldShapeGroup::
147 get_num_shapes() const {
148 
149  nassertr(_error_type == ET_ok, -1);
150 
151  return _ptr->getNbShapes();
152 }
153 
154 /**
155  * Creates a force field shape and adds it to the group.
156  */
159 
160  nassertr(_error_type == ET_ok, nullptr);
161  nassertr(desc.is_valid(),nullptr);
162 
163  PhysxForceFieldShape *shape = PhysxForceFieldShape::factory(desc.ptr()->getType());
164  nassertr(shape, nullptr);
165 
166  NxForceFieldShape *shapePtr = _ptr->createShape(*desc.ptr());
167  nassertr(shapePtr, nullptr);
168 
169  shape->link(shapePtr);
170 
171  return shape;
172 }
173 
174 /**
175  * Returns the i-th shape in the force field group.
176  */
178 get_shape(unsigned int idx) const {
179 
180  nassertr(_error_type == ET_ok, nullptr);
181  nassertr_always(idx < _ptr->getNbShapes(), nullptr);
182 
183  NxForceFieldShape *shapePtr;
184  NxU32 nShapes = _ptr->getNbShapes();
185 
186  _ptr->resetShapesIterator();
187  for (NxU32 i=0; i <= idx; i++) {
188  shapePtr = _ptr->getNextShape();
189  }
190 
191  return (PhysxForceFieldShape *)(shapePtr->userData);
192 }
Abstract base class for descriptors for force field shapes descriptors.
get_shape
Returns the i-th shape in the force field group.
PhysxForceFieldShape * create_shape(PhysxForceFieldShapeDesc &desc)
Creates a force field shape and adds it to the group.
get_num_shapes
Returns the number of shapes assigned to the force field shape group.
void save_to_desc(PhysxForceFieldShapeGroupDesc &groupDesc) const
Saves the state of the force field shape group object to a descriptor.
PhysxForceField * get_force_field() const
Returns the force field of this group if this is an include group.
const char * get_name() const
Returns the name string.
PhysxScene * get_scene() const
Returns the scene that owns this force field shape group.
void set_name(const char *name)
Sets a name string for the object that can be retrieved with get_name().
void release()
Releases the force field shape.
Abstract base class for force field shapes.
A force field effector.
A scene is a collection of bodies, constraints, and effectors which can interact.
Definition: physxScene.h:69
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.