Panda3D
linearCylinderVortexForce.cxx
1 // Filename: linearCylinderVortexForce.cxx
2 // Created by: charles (24Jul00)
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 "config_physics.h"
16 #include "linearCylinderVortexForce.h"
17 #include "nearly_zero.h"
18 #include "cmath.h"
19 
20 TypeHandle LinearCylinderVortexForce::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function : LinearCylinderVortexForce
24 // Access : public
25 // Description : Simple Constructor
26 ////////////////////////////////////////////////////////////////////
28 LinearCylinderVortexForce(PN_stdfloat radius, PN_stdfloat length, PN_stdfloat coef,
29  PN_stdfloat a, bool md) :
30  LinearForce(a, md),
31  _radius(radius), _length(length), _coef(coef) {
32 }
33 
34 ////////////////////////////////////////////////////////////////////
35 // Function : LinearCylinderVortexForce
36 // Access : public
37 // Description : copy Constructor
38 ////////////////////////////////////////////////////////////////////
41  LinearForce(copy) {
42  _radius = copy._radius;
43  _length = copy._length;
44  _coef = copy._coef;
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function : ~LinearCylinderVortexForce
49 // Access : public
50 // Description : Destructor
51 ////////////////////////////////////////////////////////////////////
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function : make_copy
58 // Access : public, virtual
59 // Description : child copier
60 ////////////////////////////////////////////////////////////////////
61 LinearForce *LinearCylinderVortexForce::
62 make_copy() {
63  return new LinearCylinderVortexForce(*this);
64 }
65 
66 ////////////////////////////////////////////////////////////////////
67 // Function : get_child_vector
68 // Access : private, virtual
69 // Description : returns the centripetal force vector for the
70 // passed-in object
71 ////////////////////////////////////////////////////////////////////
72 LVector3 LinearCylinderVortexForce::
73 get_child_vector(const PhysicsObject *po) {
74  // get the force-space transform- this MUST be the relative matrix
75  // from the point's local coordinate system to the attached node's
76  // local system.
77  // LMatrix4 force_space_xform = LMatrix4::ident_mat();
78  LVector3 force_vec(0.0f, 0.0f, 0.0f);
79 
80  // project the point into force_space
81  LPoint3 point = po->get_position();
82 
83  // clip along length
84  if (point[2] < 0.0f || point[2] > _length)
85  return force_vec;
86 
87  // clip to radius
88  PN_stdfloat x_squared = point[0] * point[0];
89  PN_stdfloat y_squared = point[1] * point[1];
90  PN_stdfloat dist_squared = x_squared + y_squared;
91  PN_stdfloat radius_squared = _radius * _radius;
92 
93  // squared space increases monotonically wrt linear space,
94  // so there's no need to sqrt to check inside/outside this disc.
95  if (dist_squared > radius_squared)
96  return force_vec;
97 
98  if IS_NEARLY_ZERO(dist_squared)
99  return force_vec;
100 
101  PN_stdfloat r = csqrt(dist_squared);
102 
103  if IS_NEARLY_ZERO(r)
104  return force_vec;
105 
106  LVector3 tangential = point;
107  tangential[2] = 0.0f;
108  tangential.normalize();
109  tangential = tangential.cross(LVector3(0,0,1));
110 
111  LVector3 centripetal = -point;
112  centripetal[2] = 0.0f;
113  centripetal.normalize();
114 
115  LVector3 combined = tangential + centripetal;
116  combined.normalize();
117 
118  // a = v^2 / r
119  //centripetal = centripetal * _coef * (tangential.length_squared() /
120  // (r + get_nearly_zero_value(r)));
121 
122  centripetal = combined * _coef * po->get_velocity().length();
123 
124  //centripetal = combined * _coef * (po->get_velocity().length() /
125  // (r + get_nearly_zero_value(r)));
126 
127  return centripetal;
128 }
129 
130 ////////////////////////////////////////////////////////////////////
131 // Function : output
132 // Access : Public
133 // Description : Write a string representation of this instance to
134 // <out>.
135 ////////////////////////////////////////////////////////////////////
137 output(ostream &out) const {
138  #ifndef NDEBUG //[
139  out<<"LinearCylinderVortexForce";
140  #endif //] NDEBUG
141 }
142 
143 ////////////////////////////////////////////////////////////////////
144 // Function : write
145 // Access : Public
146 // Description : Write a string representation of this instance to
147 // <out>.
148 ////////////////////////////////////////////////////////////////////
150 write(ostream &out, unsigned int indent) const {
151  #ifndef NDEBUG //[
152  out.width(indent); out<<""; out<<"LinearCylinderVortexForce:\n";
153  LinearForce::write(out, indent+2);
154  #endif //] NDEBUG
155 }
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
LinearCylinderVortexForce(PN_stdfloat radius=1.0f, PN_stdfloat length=0.0f, PN_stdfloat coef=1.0f, PN_stdfloat a=1.0f, bool md=false)
Simple Constructor.
virtual void output(ostream &out) const
Write a string representation of this instance to <out>.
A body on which physics will be applied.
Definition: physicsObject.h:29
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
A force that acts on a PhysicsObject by way of an Integrator.
Definition: linearForce.h:25
LVector3 get_velocity() const
Velocity Query per second.
float length() const
Returns the length of the vector, by the Pythagorean theorem.
Definition: lvecBase3.h:766
LPoint3 get_position() const
Position Query.
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
Defines a cylinder inside of which all forces are tangential to the theta of the particle wrt the z-a...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual ~LinearCylinderVortexForce()
Destructor.
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:783