Panda3D
physical.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 physical.cxx
10  * @author charles
11  * @date 2000-06-16
12  */
13 
14 #include "pointerTo.h"
15 
16 #include "physical.h"
17 #include "physicsManager.h"
18 
19 using std::ostream;
20 
21 TypeHandle Physical::_type_handle;
22 
23 /**
24  * Default Constructor The idea here is that most physicals will NOT be
25  * collections of sets (i.e. particle systems and whatever else). Because of
26  * this, the default constructor, unless otherwise specified, will
27  * automatically allocate and initialize one PhysicalObject. This makes it
28  * easier for high-level work.
29  *
30  * pre-alloc is ONLY for multiple-object physicals, and if true, fills the
31  * physics_object vector with dead nodes, pre-allocating for the speed end of
32  * the speed-vs-overhead deal.
33  */
35 Physical(int total_objects, bool pre_alloc) :
36  _viscosity(0.0),
37  _physics_manager(nullptr),
38  _physical_node(nullptr) {
39 
40  if (total_objects == 1) {
41  _phys_body = new PhysicsObject;
42  add_physics_object(_phys_body);
43  } else {
44  _phys_body = nullptr;
45  // allocate each object.
46  if (pre_alloc == true) {
47  for (int i = 0; i < total_objects; ++i) {
48  PhysicsObject *po = new PhysicsObject;
50  }
51  }
52  }
53 }
54 
55 /**
56  * copy constructor (note- does deep copy of pn's) but does NOT attach itself
57  * to its template's physicsmanager.
58  */
60 Physical(const Physical& copy) :
61  _physics_manager(nullptr),
62  _physical_node(nullptr) {
63 
64  // copy the forces.
65  LinearForceVector::const_iterator lf_cur;
66  LinearForceVector::const_iterator lf_end = copy._linear_forces.end();
67 
68  for (lf_cur = copy._linear_forces.begin(); lf_cur != lf_end; lf_cur++) {
69  _linear_forces.push_back((*lf_cur)->make_copy());
70  }
71 
72  AngularForceVector::const_iterator af_cur;
73  AngularForceVector::const_iterator af_end = copy._angular_forces.end();
74 
75  for (af_cur = copy._angular_forces.begin(); af_cur != af_end; af_cur++) {
76  _angular_forces.push_back((*af_cur)->make_copy());
77  }
78 
79  // copy the physics objects
80  PhysicsObject::Vector::const_iterator p_cur;
81  PhysicsObject::Vector::const_iterator p_end = copy._physics_objects.end();
82 
83  for (p_cur = copy._physics_objects.begin(); p_cur != p_end; p_cur++) {
84  // oooh so polymorphic.
85  _physics_objects.push_back((*p_cur)->make_copy());
86  }
87 
88  // now set the one-element-quick-access pointer
89  if (_physics_objects.size() == 1)
90  _phys_body = _physics_objects[0];
91  else
92  _phys_body = nullptr;
93 }
94 
95 /**
96  * destructor
97  */
100  // note that this removes a physical from a physics manager. this is safe
101  // because the physics manager doesn't keep PT's to physicals, simply *'s,
102  // and also means that we don't have to tell the physics manager ourselves
103  // when one of our physicals is dead.
104  if (_physics_manager != nullptr) {
105  _physics_manager->remove_physical(this);
106  }
107 }
108 
109 /**
110 
111  */
112 const PhysicsObjectCollection Physical::
113 get_objects() const{
115 
116  for (PhysicsObject::Vector::const_iterator i=_physics_objects.begin();
117  i != _physics_objects.end();
118  ++i) {
119  poc.add_physics_object((PhysicsObject*)(*i));
120  }
121 
122  return poc;
123 }
124 
125 /**
126  * Write a string representation of this instance to <out>.
127  */
128 void Physical::
129 output(ostream &out) const {
130  #ifndef NDEBUG //[
131  out<<"Physical";
132  #endif //] NDEBUG
133 }
134 
135 /**
136  * Write a string representation of this instance to <out>.
137  */
138 void Physical::
139 write_physics_objects(ostream &out, int indent) const {
140  #ifndef NDEBUG //[
141  out.width(indent);
142  out<<""<<"_physics_objects ("<<_physics_objects.size()<<" objects)\n";
143  for (PhysicsObject::Vector::const_iterator i=_physics_objects.begin();
144  i != _physics_objects.end();
145  ++i) {
146  (*i)->write(out, indent+2);
147  }
148  #endif //] NDEBUG
149 }
150 
151 /**
152  * Write a string representation of this instance to <out>.
153  */
154 void Physical::
155 write_linear_forces(ostream &out, int indent) const {
156  #ifndef NDEBUG //[
157  out.width(indent);
158  out<<""<<"_linear_forces ("<<_linear_forces.size()<<" forces)\n";
159  for (LinearForceVector::const_iterator i=_linear_forces.begin();
160  i != _linear_forces.end();
161  ++i) {
162  (*i)->write(out, indent+2);
163  }
164  #endif //] NDEBUG
165 }
166 
167 /**
168  * Write a string representation of this instance to <out>.
169  */
170 void Physical::
171 write_angular_forces(ostream &out, int indent) const {
172  #ifndef NDEBUG //[
173  out.width(indent);
174  out<<""<<"_angular_forces ("<<_angular_forces.size()<<" forces)\n";
175  for (AngularForceVector::const_iterator i=_angular_forces.begin();
176  i != _angular_forces.end();
177  ++i) {
178  (*i)->write(out, indent+2);
179  }
180  #endif //] NDEBUG
181 }
182 
183 /**
184  * Write a string representation of this instance to <out>.
185  */
186 void Physical::
187 write(ostream &out, int indent) const {
188  #ifndef NDEBUG //[
189  out.width(indent); out<<""<<"Physical\n";
191  write_linear_forces(out, indent+2);
193  if (_phys_body) {
194  out.width(indent+2); out<<""<<"_phys_body\n";
195  _phys_body->write(out, indent+4);
196  } else {
197  out.width(indent+2); out<<""<<"_phys_body is null\n";
198  }
199  #endif //] NDEBUG
200 }
virtual void write_linear_forces(std::ostream &out=std::cout, int indent=0) const
Write a string representation of this instance to <out>.
Definition: physical.cxx:155
void remove_physical(Physical *p)
takes a physical out of the object list
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A body on which physics will be applied.
Definition: physicsObject.h:27
This is a set of zero or more PhysicsObjects.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write_angular_forces(std::ostream &out=std::cout, int indent=0) const
Write a string representation of this instance to <out>.
Definition: physical.cxx:171
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
virtual ~Physical()
destructor
Definition: physical.cxx:99
Defines a set of physically modeled attributes.
Definition: physical.h:37
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Physical(int total_objects=1, bool pre_alloc=false)
Default Constructor The idea here is that most physicals will NOT be collections of sets (i....
Definition: physical.cxx:35
virtual void write(std::ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
virtual void write(std::ostream &out=std::cout, int indent=0) const
Write a string representation of this instance to <out>.
Definition: physical.cxx:187
void add_physics_object(PT(PhysicsObject) physics_object)
Adds a new PhysicsObject to the collection.
void add_physics_object(PhysicsObject *po)
Adds an object to the physics object vector.
Definition: physical.I:97
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual void write_physics_objects(std::ostream &out=std::cout, int indent=0) const
Write a string representation of this instance to <out>.
Definition: physical.cxx:139
virtual void output(std::ostream &out=std::cout) const
Write a string representation of this instance to <out>.
Definition: physical.cxx:129