00001 // Filename: forceNode.cxx 00002 // Created by: charles (02Aug00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "forceNode.h" 00016 #include "config_physics.h" 00017 00018 TypeHandle ForceNode::_type_handle; 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function : ForceNode 00022 // Access : public 00023 // Description : default constructor 00024 //////////////////////////////////////////////////////////////////// 00025 ForceNode:: 00026 ForceNode(const string &name) : 00027 PandaNode(name) { 00028 } 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function : ForceNode 00032 // Access : protected 00033 // Description : copy constructor 00034 //////////////////////////////////////////////////////////////////// 00035 ForceNode:: 00036 ForceNode(const ForceNode ©) : 00037 PandaNode(copy), _forces(copy._forces) { 00038 } 00039 00040 //////////////////////////////////////////////////////////////////// 00041 // Function : ~ForceNode 00042 // Access : public, virtual 00043 // Description : destructor 00044 //////////////////////////////////////////////////////////////////// 00045 ForceNode:: 00046 ~ForceNode() { 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function : make_copy 00051 // Access : public, virtual 00052 // Description : dynamic child copy 00053 //////////////////////////////////////////////////////////////////// 00054 PandaNode *ForceNode:: 00055 make_copy() const { 00056 return new ForceNode(*this); 00057 } 00058 00059 //////////////////////////////////////////////////////////////////// 00060 // Function : add_forces_from 00061 // Access : public 00062 // Description : append operation 00063 //////////////////////////////////////////////////////////////////// 00064 void ForceNode:: 00065 add_forces_from(const ForceNode &other) { 00066 pvector< PT(BaseForce) >::iterator last = _forces.end() - 1; 00067 00068 _forces.insert(_forces.end(), 00069 other._forces.begin(), other._forces.end()); 00070 00071 NodePath node_path(this); 00072 for (; last != _forces.end(); last++) { 00073 (*last)->_force_node = this; 00074 (*last)->_force_node_path = node_path; 00075 } 00076 } 00077 00078 //////////////////////////////////////////////////////////////////// 00079 // Function : remove_force 00080 // Access : public 00081 // Description : remove operation 00082 //////////////////////////////////////////////////////////////////// 00083 void ForceNode:: 00084 remove_force(BaseForce *f) { 00085 pvector< PT(BaseForce) >::iterator found; 00086 PT(BaseForce) ptbf = f; 00087 found = find(_forces.begin(), _forces.end(), ptbf); 00088 if (found == _forces.end()) 00089 return; 00090 _forces.erase(found); 00091 } 00092 00093 //////////////////////////////////////////////////////////////////// 00094 // Function : remove_force 00095 // Access : public 00096 // Description : remove operation 00097 //////////////////////////////////////////////////////////////////// 00098 void ForceNode:: 00099 remove_force(int index) { 00100 nassertv(index >= 0 && index <= (int)_forces.size()); 00101 00102 pvector< PT(BaseForce) >::iterator remove; 00103 remove = _forces.begin() + index; 00104 (*remove)->_force_node = (ForceNode *) NULL; 00105 (*remove)->_force_node_path = NodePath(); 00106 00107 _forces.erase(remove); 00108 } 00109 00110 //////////////////////////////////////////////////////////////////// 00111 // Function : output 00112 // Access : Public 00113 // Description : Write a string representation of this instance to 00114 // <out>. 00115 //////////////////////////////////////////////////////////////////// 00116 void ForceNode:: 00117 output(ostream &out) const { 00118 PandaNode::output(out); 00119 out<<" ("<<_forces.size()<<" forces)"; 00120 } 00121 00122 //////////////////////////////////////////////////////////////////// 00123 // Function : write_linear_forces 00124 // Access : Public 00125 // Description : Write a string representation of this instance to 00126 // <out>. 00127 //////////////////////////////////////////////////////////////////// 00128 void ForceNode:: 00129 write_forces(ostream &out, unsigned int indent) const { 00130 #ifndef NDEBUG //[ 00131 out.width(indent); out<<""<<"_forces ("<<_forces.size()<<" forces)"<<"\n"; 00132 for (ForceVector::const_iterator i=_forces.begin(); 00133 i != _forces.end(); 00134 ++i) { 00135 out.width(indent+2); out<<""; out<<"(id "<<&(*i)<<" "<<(*i)->is_linear()<<")\n"; 00136 //#*#(*i)->write(out, indent+2); 00137 } 00138 #endif //] NDEBUG 00139 } 00140 00141 //////////////////////////////////////////////////////////////////// 00142 // Function : write 00143 // Access : Public 00144 // Description : Write a string representation of this instance to 00145 // <out>. 00146 //////////////////////////////////////////////////////////////////// 00147 void ForceNode:: 00148 write(ostream &out, unsigned int indent) const { 00149 #ifndef NDEBUG //[ 00150 out.width(indent); out<<""; out<<"ForceNode (id "<<this<<") "; 00151 //#*#PandaNode::output(out); 00152 out<<"\n"; 00153 //#*#write_forces(out, indent+2); 00154 PandaNode::write(out, indent+4); 00155 #endif //] NDEBUG 00156 }