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