Panda3D
Loading...
Searching...
No Matches
physicsObjectCollection.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 physicsObjectCollection.cxx
10 * @author joswilso
11 * @date 2006-07-12
12 */
13
15
16#include "indent.h"
17
18/**
19 *
20 */
21PhysicsObjectCollection::
22PhysicsObjectCollection() {
23}
24
25/**
26 *
27 */
28PhysicsObjectCollection::
29PhysicsObjectCollection(const PhysicsObjectCollection &copy) :
30 _physics_objects(copy._physics_objects)
31{
32}
33
34/**
35 *
36 */
37void PhysicsObjectCollection::
38operator = (const PhysicsObjectCollection &copy) {
39 _physics_objects = copy._physics_objects;
40}
41
42/**
43 * Adds a new PhysicsObject to the collection.
44 */
46add_physics_object(PT(PhysicsObject) physics_object) {
47 // If the pointer to our internal array is shared by any other
48 // PhysicsObjectCollections, we have to copy the array now so we won't
49 // inadvertently modify any of our brethren PhysicsObjectCollection objects.
50
51 if (_physics_objects.get_ref_count() > 1) {
52 PhysicsObjects old_physics_objects = _physics_objects;
53 _physics_objects = PhysicsObjects::empty_array(0);
54 _physics_objects.v() = old_physics_objects.v();
55 }
56
57 _physics_objects.push_back(physics_object);
58}
59
60/**
61 * Removes the indicated PhysicsObject from the collection. Returns true if
62 * the physics_object was removed, false if it was not a member of the
63 * collection.
64 */
66remove_physics_object(PT(PhysicsObject) physics_object) {
67 int object_index = -1;
68 for (int i = 0; object_index == -1 && i < (int)_physics_objects.size(); i++) {
69 if (_physics_objects[i] == physics_object) {
70 object_index = i;
71 }
72 }
73
74 if (object_index == -1) {
75 // The indicated physics_object was not a member of the collection.
76 return false;
77 }
78
79 // If the pointer to our internal array is shared by any other
80 // PhysicsObjectCollections, we have to copy the array now so we won't
81 // inadvertently modify any of our brethren PhysicsObjectCollection objects.
82
83 if (_physics_objects.get_ref_count() > 1) {
84 PhysicsObjects old_physics_objects = _physics_objects;
85 _physics_objects = PhysicsObjects::empty_array(0);
86 _physics_objects.v() = old_physics_objects.v();
87 }
88
89 _physics_objects.erase(_physics_objects.begin() + object_index);
90 return true;
91}
92
93/**
94 * Adds all the PhysicsObjects indicated in the other collection to this
95 * collection. The other physics_objects are simply appended to the end of
96 * the physics_objects in this list; duplicates are not automatically removed.
97 */
100 int other_num_physics_objects = other.get_num_physics_objects();
101 for (int i = 0; i < other_num_physics_objects; i++) {
102 add_physics_object(other.get_physics_object(i));
103 }
104}
105
106
107/**
108 * Removes from this collection all of the PhysicsObjects listed in the other
109 * collection.
110 */
113 PhysicsObjects new_physics_objects;
114 int num_physics_objects = get_num_physics_objects();
115 for (int i = 0; i < num_physics_objects; i++) {
116 PT(PhysicsObject) physics_object = get_physics_object(i);
117 if (!other.has_physics_object(physics_object)) {
118 new_physics_objects.push_back(physics_object);
119 }
120 }
121 _physics_objects = new_physics_objects;
122}
123
124/**
125 * Removes any duplicate entries of the same PhysicsObjects on this
126 * collection. If a PhysicsObject appears multiple times, the first
127 * appearance is retained; subsequent appearances are removed.
128 */
131 PhysicsObjects new_physics_objects;
132
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 bool duplicated = false;
137
138 for (int j = 0; j < i && !duplicated; j++) {
139 duplicated = (physics_object == get_physics_object(j));
140 }
141
142 if (!duplicated) {
143 new_physics_objects.push_back(physics_object);
144 }
145 }
146
147 _physics_objects = new_physics_objects;
148}
149
150/**
151 * Returns true if the indicated PhysicsObject appears in this collection,
152 * false otherwise.
153 */
155has_physics_object(PT(PhysicsObject) physics_object) const {
156 for (int i = 0; i < get_num_physics_objects(); i++) {
157 if (physics_object == get_physics_object(i)) {
158 return true;
159 }
160 }
161 return false;
162}
163
164/**
165 * Removes all PhysicsObjects from the collection.
166 */
168clear() {
169 _physics_objects.clear();
170}
171
172/**
173 * Returns true if there are no PhysicsObjects in the collection, false
174 * otherwise.
175 */
177is_empty() const {
178 return _physics_objects.empty();
179}
180
181/**
182 * Returns the number of PhysicsObjects in the collection.
183 */
186 return _physics_objects.size();
187}
188
189/**
190 * Returns the nth PhysicsObject in the collection.
191 */
192PT(PhysicsObject) PhysicsObjectCollection::
193get_physics_object(int index) const {
194 nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
195
196 return _physics_objects[index];
197}
198
199/**
200 * Returns the nth PhysicsObject in the collection. This is the same as
201 * get_physics_object(), but it may be a more convenient way to access it.
202 */
203PT(PhysicsObject) PhysicsObjectCollection::
204operator [] (int index) const {
205 nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
206
207 return _physics_objects[index];
208}
209
210/**
211 * Returns the number of physics objects in the collection. This is the same
212 * thing as get_num_physics_objects().
213 */
214int PhysicsObjectCollection::
215size() const {
216 return _physics_objects.size();
217}
218
219/**
220 * Writes a brief one-line description of the PhysicsObjectCollection to the
221 * indicated output stream.
222 */
223void PhysicsObjectCollection::
224output(std::ostream &out) const {
225 if (get_num_physics_objects() == 1) {
226 out << "1 PhysicsObject";
227 } else {
228 out << get_num_physics_objects() << " PhysicsObjects";
229 }
230}
231
232/**
233 * Writes a complete multi-line description of the PhysicsObjectCollection to
234 * the indicated output stream.
235 */
236void PhysicsObjectCollection::
237write(std::ostream &out, int indent_level) const {
238 for (int i = 0; i < get_num_physics_objects(); i++) {
239 indent(out, indent_level) << get_physics_object(i) << "\n";
240 }
241}
This is a set of zero or more PhysicsObjects.
bool has_physics_object(PT(PhysicsObject) physics_object) const
Returns true if the indicated PhysicsObject appears in this collection, false otherwise.
void clear()
Removes all PhysicsObjects from the collection.
bool is_empty() const
Returns true if there are no PhysicsObjects in the collection, false otherwise.
void add_physics_object(PT(PhysicsObject) physics_object)
Adds a new PhysicsObject to the collection.
bool remove_physics_object(PT(PhysicsObject) physics_object)
Removes the indicated PhysicsObject from the collection.
get_num_physics_objects
Returns the number of PhysicsObjects in the collection.
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_objects_from(const PhysicsObjectCollection &other)
Adds all the PhysicsObjects indicated in the other collection to this collection.
A body on which physics will be applied.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.