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