Panda3D
 All Classes Functions Variables Enumerations
linearFrictionForce.cxx
1 // Filename: linearFrictionForce.cxx
2 // Created by: charles (23Jun00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "linearFrictionForce.h"
16 #include "config_physics.h"
17 
18 TypeHandle LinearFrictionForce::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function : LinearFrictionForce
22 // Access : Public
23 // Description : Constructor
24 ////////////////////////////////////////////////////////////////////
26 LinearFrictionForce(PN_stdfloat coef, PN_stdfloat a, bool m) :
27  LinearForce(a, m) {
28  set_coef(coef);
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function : LinearFrictionForce
33 // Access : Public
34 // Description : copy constructor
35 ////////////////////////////////////////////////////////////////////
38  LinearForce(copy) {
39  _coef = copy._coef;
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function : LinearFrictionForce
44 // Access : Public
45 // Description : destructor
46 ////////////////////////////////////////////////////////////////////
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function : make_copy
53 // Access : Public
54 // Description : copier
55 ////////////////////////////////////////////////////////////////////
56 LinearForce *LinearFrictionForce::
57 make_copy() {
58  return new LinearFrictionForce(*this);
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function : LinearFrictionForce
63 // Access : Public
64 // Description : Constructor
65 ////////////////////////////////////////////////////////////////////
66 LVector3 LinearFrictionForce::
67 get_child_vector(const PhysicsObject* po) {
68  LVector3 v = po->get_velocity();
69  assert(_coef>=0.0f && _coef<=1.0f);
70  // Create a force vector in the opposite direction of v:
71  LVector3 friction = v * -_coef;
72  physics_debug(" v "<<v<<" len "<<v.length()
73  <<" friction "<<friction<<" len "<<friction.length()
74  <<" dot "<<(normalize(v).dot(normalize(friction))));
75  assert(friction.almost_equal(LVector3::zero())
76  || IS_NEARLY_EQUAL(normalize(v).dot(normalize(friction)), -1.0f));
77  // cary said to cap this at zero so that friction can't reverse
78  // your direction, but it seems to me that if you're computing:
79  // v + (-v * _coef), _coef in [0, 1]
80  // that this will always be greater than or equal to zero.
81  return friction;
82 }
83 
84 ////////////////////////////////////////////////////////////////////
85 // Function : output
86 // Access : Public
87 // Description : Write a string representation of this instance to
88 // <out>.
89 ////////////////////////////////////////////////////////////////////
91 output(ostream &out) const {
92  #ifndef NDEBUG //[
93  out<<"LinearFrictionForce";
94  #endif //] NDEBUG
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function : write
99 // Access : Public
100 // Description : Write a string representation of this instance to
101 // <out>.
102 ////////////////////////////////////////////////////////////////////
104 write(ostream &out, unsigned int indent) const {
105  #ifndef NDEBUG //[
106  out.width(indent); out<<""; out<<"LinearFrictionForce:\n";
107  out.width(indent+2); out<<""; out<<"_coef "<<_coef<<":\n";
108  LinearForce::write(out, indent+2);
109  #endif //] NDEBUG
110 }
Friction-based drag force.
LVector3 get_velocity() const
Velocity Query per second.
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to &lt;out&gt;.
A body on which physics will be applied.
Definition: physicsObject.h:29
static const LVector3f & zero()
Returns a zero-length vector.
Definition: lvector3.h:269
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
LinearFrictionForce(PN_stdfloat coef=1.0f, PN_stdfloat a=1.0f, bool m=false)
Constructor.
A force that acts on a PhysicsObject by way of an Integrator.
Definition: linearForce.h:25
float length() const
Returns the length of the vector, by the Pythagorean theorem.
Definition: lvecBase3.h:765
bool almost_equal(const LVecBase3f &other, float threshold) const
Returns true if two vectors are memberwise equal within a specified tolerance.
Definition: lvecBase3.h:1264
virtual void output(ostream &out) const
Write a string representation of this instance to &lt;out&gt;.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to &lt;out&gt;.
virtual ~LinearFrictionForce()
destructor