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