Panda3D
|
00001 // Filename: linearCylinderVortexForce.cxx 00002 // Created by: charles (24Jul00) 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 "config_physics.h" 00016 #include "linearCylinderVortexForce.h" 00017 #include "nearly_zero.h" 00018 00019 TypeHandle LinearCylinderVortexForce::_type_handle; 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function : LinearCylinderVortexForce 00023 // Access : public 00024 // Description : Simple Constructor 00025 //////////////////////////////////////////////////////////////////// 00026 LinearCylinderVortexForce:: 00027 LinearCylinderVortexForce(float radius, float length, float coef, 00028 float a, bool md) : 00029 LinearForce(a, md), 00030 _radius(radius), _length(length), _coef(coef) { 00031 } 00032 00033 //////////////////////////////////////////////////////////////////// 00034 // Function : LinearCylinderVortexForce 00035 // Access : public 00036 // Description : copy Constructor 00037 //////////////////////////////////////////////////////////////////// 00038 LinearCylinderVortexForce:: 00039 LinearCylinderVortexForce(const LinearCylinderVortexForce ©) : 00040 LinearForce(copy) { 00041 _radius = copy._radius; 00042 _length = copy._length; 00043 _coef = copy._coef; 00044 } 00045 00046 //////////////////////////////////////////////////////////////////// 00047 // Function : ~LinearCylinderVortexForce 00048 // Access : public 00049 // Description : Destructor 00050 //////////////////////////////////////////////////////////////////// 00051 LinearCylinderVortexForce:: 00052 ~LinearCylinderVortexForce() { 00053 } 00054 00055 //////////////////////////////////////////////////////////////////// 00056 // Function : make_copy 00057 // Access : public, virtual 00058 // Description : child copier 00059 //////////////////////////////////////////////////////////////////// 00060 LinearForce *LinearCylinderVortexForce:: 00061 make_copy() { 00062 return new LinearCylinderVortexForce(*this); 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function : get_child_vector 00067 // Access : private, virtual 00068 // Description : returns the centripetal force vector for the 00069 // passed-in object 00070 //////////////////////////////////////////////////////////////////// 00071 LVector3f LinearCylinderVortexForce:: 00072 get_child_vector(const PhysicsObject *po) { 00073 // get the force-space transform- this MUST be the relative matrix 00074 // from the point's local coordinate system to the attached node's 00075 // local system. 00076 // LMatrix4f force_space_xform = LMatrix4f::ident_mat(); 00077 LVector3f force_vec(0.0f, 0.0f, 0.0f); 00078 00079 // project the point into force_space 00080 LPoint3f point = po->get_position(); 00081 00082 // clip along length 00083 if (point[2] < 0.0f || point[2] > _length) 00084 return force_vec; 00085 00086 // clip to radius 00087 float x_squared = point[0] * point[0]; 00088 float y_squared = point[1] * point[1]; 00089 float dist_squared = x_squared + y_squared; 00090 float radius_squared = _radius * _radius; 00091 00092 // squared space increases monotonically wrt linear space, 00093 // so there's no need to sqrt to check inside/outside this disc. 00094 if (dist_squared > radius_squared) 00095 return force_vec; 00096 00097 if IS_NEARLY_ZERO(dist_squared) 00098 return force_vec; 00099 00100 float r = sqrtf(dist_squared); 00101 00102 if IS_NEARLY_ZERO(r) 00103 return force_vec; 00104 00105 LVector3f tangential = point; 00106 tangential[2] = 0.0f; 00107 tangential.normalize(); 00108 tangential = tangential.cross(LVector3f(0,0,1)); 00109 00110 LVector3f centripetal = -point; 00111 centripetal[2] = 0.0f; 00112 centripetal.normalize(); 00113 00114 LVector3f combined = tangential + centripetal; 00115 combined.normalize(); 00116 00117 // a = v^2 / r 00118 //centripetal = centripetal * _coef * (tangential.length_squared() / 00119 // (r + get_nearly_zero_value(r))); 00120 00121 centripetal = combined * _coef * po->get_velocity().length(); 00122 00123 //centripetal = combined * _coef * (po->get_velocity().length() / 00124 // (r + get_nearly_zero_value(r))); 00125 00126 return centripetal; 00127 } 00128 00129 //////////////////////////////////////////////////////////////////// 00130 // Function : output 00131 // Access : Public 00132 // Description : Write a string representation of this instance to 00133 // <out>. 00134 //////////////////////////////////////////////////////////////////// 00135 void LinearCylinderVortexForce:: 00136 output(ostream &out) const { 00137 #ifndef NDEBUG //[ 00138 out<<"LinearCylinderVortexForce"; 00139 #endif //] NDEBUG 00140 } 00141 00142 //////////////////////////////////////////////////////////////////// 00143 // Function : write 00144 // Access : Public 00145 // Description : Write a string representation of this instance to 00146 // <out>. 00147 //////////////////////////////////////////////////////////////////// 00148 void LinearCylinderVortexForce:: 00149 write(ostream &out, unsigned int indent) const { 00150 #ifndef NDEBUG //[ 00151 out.width(indent); out<<""; out<<"LinearCylinderVortexForce:\n"; 00152 LinearForce::write(out, indent+2); 00153 #endif //] NDEBUG 00154 }