Panda3D
 All Classes Functions Variables Enumerations
linearFrictionForce.cxx
00001 // Filename: linearFrictionForce.cxx
00002 // Created by:  charles (23Jun00)
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 "linearFrictionForce.h"
00016 #include "config_physics.h"
00017 
00018 TypeHandle LinearFrictionForce::_type_handle;
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //    Function : LinearFrictionForce
00022 //      Access : Public
00023 // Description : Constructor
00024 ////////////////////////////////////////////////////////////////////
00025 LinearFrictionForce::
00026 LinearFrictionForce(PN_stdfloat coef, PN_stdfloat a, bool m) :
00027   LinearForce(a, m) {
00028   set_coef(coef);
00029 }
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //    Function : LinearFrictionForce
00033 //      Access : Public
00034 // Description : copy constructor
00035 ////////////////////////////////////////////////////////////////////
00036 LinearFrictionForce::
00037 LinearFrictionForce(const LinearFrictionForce &copy) :
00038   LinearForce(copy) {
00039   _coef = copy._coef;
00040 }
00041 
00042 ////////////////////////////////////////////////////////////////////
00043 //    Function : LinearFrictionForce
00044 //      Access : Public
00045 // Description : destructor
00046 ////////////////////////////////////////////////////////////////////
00047 LinearFrictionForce::
00048 ~LinearFrictionForce() {
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //    Function : make_copy
00053 //      Access : Public
00054 // Description : copier
00055 ////////////////////////////////////////////////////////////////////
00056 LinearForce *LinearFrictionForce::
00057 make_copy() {
00058   return new LinearFrictionForce(*this);
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //    Function : LinearFrictionForce
00063 //      Access : Public
00064 // Description : Constructor
00065 ////////////////////////////////////////////////////////////////////
00066 LVector3 LinearFrictionForce::
00067 get_child_vector(const PhysicsObject* po) {
00068   LVector3 v = po->get_velocity();
00069   assert(_coef>=0.0f && _coef<=1.0f);
00070   // Create a force vector in the opposite direction of v:
00071   LVector3 friction = v * -_coef;
00072   physics_debug(" v "<<v<<" len "<<v.length()
00073       <<" friction "<<friction<<" len "<<friction.length()
00074       <<" dot "<<(normalize(v).dot(normalize(friction))));
00075   assert(friction.almost_equal(LVector3::zero()) 
00076       || IS_NEARLY_EQUAL(normalize(v).dot(normalize(friction)), -1.0f));
00077   // cary said to cap this at zero so that friction can't reverse
00078   // your direction, but it seems to me that if you're computing:
00079   //     v + (-v * _coef), _coef in [0, 1]
00080   // that this will always be greater than or equal to zero.
00081   return friction;
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function : output
00086 //       Access : Public
00087 //  Description : Write a string representation of this instance to
00088 //                <out>.
00089 ////////////////////////////////////////////////////////////////////
00090 void LinearFrictionForce::
00091 output(ostream &out) const {
00092   #ifndef NDEBUG //[
00093   out<<"LinearFrictionForce";
00094   #endif //] NDEBUG
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function : write
00099 //       Access : Public
00100 //  Description : Write a string representation of this instance to
00101 //                <out>.
00102 ////////////////////////////////////////////////////////////////////
00103 void LinearFrictionForce::
00104 write(ostream &out, unsigned int indent) const {
00105   #ifndef NDEBUG //[
00106   out.width(indent); out<<""; out<<"LinearFrictionForce:\n";
00107   out.width(indent+2); out<<""; out<<"_coef "<<_coef<<":\n";
00108   LinearForce::write(out, indent+2);
00109   #endif //] NDEBUG
00110 }
 All Classes Functions Variables Enumerations