Panda3D
 All Classes Functions Variables Enumerations
physicsObjectCollection.cxx
1 // Filename: physicsObjectCollection.cxx
2 // Created by: joswilso (12Jul06)
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 "physicsObjectCollection.h"
16 
17 #include "indent.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: PhysicsObjectCollection::Constructor
21 // Access: Published
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 PhysicsObjectCollection::
25 PhysicsObjectCollection() {
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: PhysicsObjectCollection::Copy Constructor
30 // Access: Published
31 // Description:
32 ////////////////////////////////////////////////////////////////////
33 PhysicsObjectCollection::
34 PhysicsObjectCollection(const PhysicsObjectCollection &copy) :
35  _physics_objects(copy._physics_objects)
36 {
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: PhysicsObjectCollection::Copy Assignment Operator
41 // Access: Published
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 void PhysicsObjectCollection::
45 operator = (const PhysicsObjectCollection &copy) {
46  _physics_objects = copy._physics_objects;
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: PhysicsObjectCollection::add_physics_object
51 // Access: Published
52 // Description: Adds a new PhysicsObject to the collection.
53 ////////////////////////////////////////////////////////////////////
55 add_physics_object(PT(PhysicsObject) physics_object) {
56  // If the pointer to our internal array is shared by any other
57  // PhysicsObjectCollections, we have to copy the array now so we won't
58  // inadvertently modify any of our brethren PhysicsObjectCollection
59  // objects.
60 
61  if (_physics_objects.get_ref_count() > 1) {
62  PhysicsObjects old_physics_objects = _physics_objects;
63  _physics_objects = PhysicsObjects::empty_array(0);
64  _physics_objects.v() = old_physics_objects.v();
65  }
66 
67  _physics_objects.push_back(physics_object);
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: PhysicsObjectCollection::remove_physics_object
72 // Access: Published
73 // Description: Removes the indicated PhysicsObject from the collection.
74 // Returns true if the physics_object was removed, false if it was
75 // not a member of the collection.
76 ////////////////////////////////////////////////////////////////////
78 remove_physics_object(PT(PhysicsObject) physics_object) {
79  int object_index = -1;
80  for (int i = 0; object_index == -1 && i < (int)_physics_objects.size(); i++) {
81  if (_physics_objects[i] == physics_object) {
82  object_index = i;
83  }
84  }
85 
86  if (object_index == -1) {
87  // The indicated physics_object was not a member of the collection.
88  return false;
89  }
90 
91  // If the pointer to our internal array is shared by any other
92  // PhysicsObjectCollections, we have to copy the array now so we won't
93  // inadvertently modify any of our brethren PhysicsObjectCollection
94  // objects.
95 
96  if (_physics_objects.get_ref_count() > 1) {
97  PhysicsObjects old_physics_objects = _physics_objects;
98  _physics_objects = PhysicsObjects::empty_array(0);
99  _physics_objects.v() = old_physics_objects.v();
100  }
101 
102  _physics_objects.erase(_physics_objects.begin() + object_index);
103  return true;
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: PhysicsObjectCollection::add_physics_objects_from
108 // Access: Published
109 // Description: Adds all the PhysicsObjects indicated in the other
110 // collection to this collection. The other
111 // physics_objects are simply appended to the end of
112 // the physics_objects in this list;
113 // duplicates are not automatically removed.
114 ////////////////////////////////////////////////////////////////////
117  int other_num_physics_objects = other.get_num_physics_objects();
118  for (int i = 0; i < other_num_physics_objects; i++) {
119  add_physics_object(other.get_physics_object(i));
120  }
121 }
122 
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: PhysicsObjectCollection::remove_physics_objects_from
126 // Access: Published
127 // Description: Removes from this collection all of the PhysicsObjects
128 // listed in the other collection.
129 ////////////////////////////////////////////////////////////////////
132  PhysicsObjects new_physics_objects;
133  int num_physics_objects = get_num_physics_objects();
134  for (int i = 0; i < num_physics_objects; i++) {
135  PT(PhysicsObject) physics_object = get_physics_object(i);
136  if (!other.has_physics_object(physics_object)) {
137  new_physics_objects.push_back(physics_object);
138  }
139  }
140  _physics_objects = new_physics_objects;
141 }
142 
143 ////////////////////////////////////////////////////////////////////
144 // Function: PhysicsObjectCollection::remove_duplicate_physics_objects
145 // Access: Published
146 // Description: Removes any duplicate entries of the same PhysicsObjects
147 // on this collection. If a PhysicsObject appears multiple
148 // times, the first appearance is retained; subsequent
149 // appearances are removed.
150 ////////////////////////////////////////////////////////////////////
153  PhysicsObjects new_physics_objects;
154 
155  int num_physics_objects = get_num_physics_objects();
156  for (int i = 0; i < num_physics_objects; i++) {
157  PT(PhysicsObject) physics_object = get_physics_object(i);
158  bool duplicated = false;
159 
160  for (int j = 0; j < i && !duplicated; j++) {
161  duplicated = (physics_object == get_physics_object(j));
162  }
163 
164  if (!duplicated) {
165  new_physics_objects.push_back(physics_object);
166  }
167  }
168 
169  _physics_objects = new_physics_objects;
170 }
171 
172 ////////////////////////////////////////////////////////////////////
173 // Function: PhysicsObjectCollection::has_physics_object
174 // Access: Published
175 // Description: Returns true if the indicated PhysicsObject appears in
176 // this collection, false otherwise.
177 ////////////////////////////////////////////////////////////////////
179 has_physics_object(PT(PhysicsObject) physics_object) const {
180  for (int i = 0; i < get_num_physics_objects(); i++) {
181  if (physics_object == get_physics_object(i)) {
182  return true;
183  }
184  }
185  return false;
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function: PhysicsObjectCollection::clear
190 // Access: Published
191 // Description: Removes all PhysicsObjects from the collection.
192 ////////////////////////////////////////////////////////////////////
194 clear() {
195  _physics_objects.clear();
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: PhysicsObjectCollection::is_empty
200 // Access: Published
201 // Description: Returns true if there are no PhysicsObjects in the
202 // collection, false otherwise.
203 ////////////////////////////////////////////////////////////////////
205 is_empty() const {
206  return _physics_objects.empty();
207 }
208 
209 ////////////////////////////////////////////////////////////////////
210 // Function: PhysicsObjectCollection::get_num_physics_objects
211 // Access: Published
212 // Description: Returns the number of PhysicsObjects in the collection.
213 ////////////////////////////////////////////////////////////////////
216  return _physics_objects.size();
217 }
218 
219 ////////////////////////////////////////////////////////////////////
220 // Function: PhysicsObjectCollection::get_physics_object
221 // Access: Published
222 // Description: Returns the nth PhysicsObject in the collection.
223 ////////////////////////////////////////////////////////////////////
225 get_physics_object(int index) const {
226  nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
227 
228  return _physics_objects[index];
229 }
230 
231 ////////////////////////////////////////////////////////////////////
232 // Function: PhysicsObjectCollection::operator []
233 // Access: Published
234 // Description: Returns the nth PhysicsObject in the collection. This is
235 // the same as get_physics_object(), but it may be a more
236 // convenient way to access it.
237 ////////////////////////////////////////////////////////////////////
239 operator [] (int index) const {
240  nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
241 
242  return _physics_objects[index];
243 }
244 
245 ////////////////////////////////////////////////////////////////////
246 // Function: PhysicsObjectCollection::size
247 // Access: Published
248 // Description: Returns the number of physics objects in the
249 // collection. This is the same thing as
250 // get_num_physics_objects().
251 ////////////////////////////////////////////////////////////////////
253 size() const {
254  return _physics_objects.size();
255 }
256 
257 ////////////////////////////////////////////////////////////////////
258 // Function: PhysicsObjectCollection::output
259 // Access: Published
260 // Description: Writes a brief one-line description of the
261 // PhysicsObjectCollection to the indicated output stream.
262 ////////////////////////////////////////////////////////////////////
264 output(ostream &out) const {
265  if (get_num_physics_objects() == 1) {
266  out << "1 PhysicsObject";
267  } else {
268  out << get_num_physics_objects() << " PhysicsObjects";
269  }
270 }
271 
272 ////////////////////////////////////////////////////////////////////
273 // Function: PhysicsObjectCollection::write
274 // Access: Published
275 // Description: Writes a complete multi-line description of the
276 // PhysicsObjectCollection to the indicated output stream.
277 ////////////////////////////////////////////////////////////////////
279 write(ostream &out, int indent_level) const {
280  for (int i = 0; i < get_num_physics_objects(); i++) {
281  indent(out, indent_level) << get_physics_object(i) << "\n";
282  }
283 }
284 
void output(ostream &out) const
Writes a brief one-line description of the PhysicsObjectCollection to the indicated output stream...
bool remove_physics_object(PT(PhysicsObject) physics_object)
Removes the indicated PhysicsObject from the collection.
A body on which physics will be applied.
Definition: physicsObject.h:29
void clear()
Removes all PhysicsObjects from the collection.
int size() const
Returns the number of physics objects in the collection.
int get_num_physics_objects() const
Returns the number of PhysicsObjects in the collection.
This is a set of zero or more PhysicsObjects.
void write(ostream &out, int indent_level=0) const
Writes a complete multi-line description of the PhysicsObjectCollection to the indicated output strea...
void add_physics_objects_from(const PhysicsObjectCollection &other)
Adds all the PhysicsObjects indicated in the other collection to this collection. ...
bool is_empty() const
Returns true if there are no PhysicsObjects in the collection, false otherwise.
void remove_physics_objects_from(const PhysicsObjectCollection &other)
Removes from this collection all of the PhysicsObjects listed in the other collection.
void remove_duplicate_physics_objects()
Removes any duplicate entries of the same PhysicsObjects on this collection.
void add_physics_object(PT(PhysicsObject) physics_object)
Adds a new PhysicsObject to the collection.
bool has_physics_object(PT(PhysicsObject) physics_object) const
Returns true if the indicated PhysicsObject appears in this collection, false otherwise.