Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/physics/physical.cxx

Go to the documentation of this file.
00001 // Filename: physical.cxx
00002 // Created by:  charles (16Jun00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include <pointerTo.h>
00020 
00021 #include "physical.h"
00022 #include "physicsManager.h"
00023 
00024 TypeHandle Physical::_type_handle;
00025 
00026 // the idea here is that most physicals will NOT be collections of
00027 // sets (i.e. particle systems and whatever else).  Because of this,
00028 // the default constructor, unless otherwise specified, will
00029 // automatically allocate and initialize one PhysicalObject.
00030 // this makes it easier for high-level work.
00031 
00032 // pre-alloc is ONLY for multiple-object physicals, and if true,
00033 // fills the physics_object vector with dead nodes, pre-allocating
00034 // for the speed end of the speed-vs-overhead deal.
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function : Physical
00038 //       Access : Public
00039 //  Description : Default Constructor
00040 ////////////////////////////////////////////////////////////////////
00041 Physical::
00042 Physical(int ttl_objects, bool pre_alloc) {
00043   _physical_node = (PhysicalNode *) NULL;
00044   _physics_manager = (PhysicsManager *) NULL;
00045 
00046   if (ttl_objects == 1) {
00047     _phys_body = new PhysicsObject;
00048     add_physics_object(_phys_body);
00049   }
00050   else {
00051     int i;
00052     _phys_body = (PhysicsObject *) NULL;
00053 
00054     // allocate each object.
00055     if (pre_alloc == true) {
00056       PhysicsObject *po;
00057 
00058       for (i = 0; i < ttl_objects; i++) {
00059         po = new PhysicsObject;
00060         add_physics_object(po);
00061       }
00062     }
00063   }
00064 }
00065 
00066 ////////////////////////////////////////////////////////////////////
00067 //     Function : Physical
00068 //       Access : Public
00069 //  Description : copy constructor (note- does deep copy of pn's)
00070 //                but does NOT attach itself to its template's
00071 //                physicsmanager.
00072 ////////////////////////////////////////////////////////////////////
00073 Physical::
00074 Physical(const Physical& copy) {
00075   _physics_manager = (PhysicsManager *) NULL;
00076 
00077   // copy the forces.
00078   pvector< PT(LinearForce) >::const_iterator lf_cur;
00079   pvector< PT(LinearForce) >::const_iterator lf_end = copy._linear_forces.end();
00080 
00081   for (lf_cur = copy._linear_forces.begin(); lf_cur != lf_end; lf_cur++) {
00082     _linear_forces.push_back((*lf_cur)->make_copy());
00083   }
00084 
00085   pvector< PT(AngularForce) >::const_iterator af_cur;
00086   pvector< PT(AngularForce) >::const_iterator af_end = copy._angular_forces.end();
00087 
00088   for (af_cur = copy._angular_forces.begin(); af_cur != af_end; af_cur++) {
00089     _angular_forces.push_back((*af_cur)->make_copy());
00090   }
00091 
00092   // copy the physics objects
00093   pvector< PT(PhysicsObject) >::const_iterator p_cur;
00094   pvector< PT(PhysicsObject) >::const_iterator p_end = copy._physics_objects.end();
00095 
00096   for (p_cur = copy._physics_objects.begin(); p_cur != p_end; p_cur++) {
00097     // oooh so polymorphic.
00098     _physics_objects.push_back((*p_cur)->make_copy());
00099   }
00100 
00101   // now set the one-element-quick-access pointer
00102   if (_physics_objects.size() == 1)
00103     _phys_body = _physics_objects[0];
00104   else
00105     _phys_body = (PhysicsObject *) NULL;
00106 }
00107 
00108 ////////////////////////////////////////////////////////////////////
00109 //     Function : ~Physical
00110 //       Access : Public
00111 //  Description : destructor
00112 ////////////////////////////////////////////////////////////////////
00113 Physical::
00114 ~Physical(void) {
00115   // note that this removes a physical from a physics manager.
00116   // this is safe because the physics manager doesn't keep PT's to
00117   // physicals, simply *'s, and also means that we don't have to tell
00118   // the physics manager ourselves when one of our physicals is dead.
00119   if (_physics_manager != (PhysicsManager *) NULL) {
00120     _physics_manager->remove_physical(this);
00121   }
00122 }
00123 
00124 
00125 

Generated on Fri May 2 00:43:01 2003 for Panda by doxygen1.3