Panda3D
 All Classes Functions Variables Enumerations
physicsObject.cxx
1 // Filename: physicsObject.cxx
2 // Created by: charles (13Jun00)
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 "physicsObject.h"
16 
17 ConfigVariableDouble PhysicsObject::_default_terminal_velocity
18 ("default_terminal_velocity", 400.0f);
19 
20 TypeHandle PhysicsObject::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function : PhysicsObject
24 // Access : Public
25 // Description : Default Constructor
26 ////////////////////////////////////////////////////////////////////
29  _terminal_velocity(_default_terminal_velocity),
30  _mass(1.0f),
31  _process_me(false),
32  _oriented(true)
33 {
34  _position.set(0.0f, 0.0f, 0.0f);
35  _last_position = _position;
36  _velocity.set(0.0f, 0.0f, 0.0f);
37  _orientation.set(1.0 ,0.0f, 0.0f, 0.0f);
38  _rotation = LRotation::ident_quat();
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function : PhysicsObject
43 // Access : Public
44 // Description : copy constructor
45 ////////////////////////////////////////////////////////////////////
48  operator=(copy);
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function : ~PhysicsObject
53 // Access : Public
54 // Description : Destructor
55 ////////////////////////////////////////////////////////////////////
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function : Assignment operator
62 // Access : Public
63 // Description :
64 ////////////////////////////////////////////////////////////////////
65 const PhysicsObject &PhysicsObject::
66 operator =(const PhysicsObject &other) {
67  _process_me = other._process_me;
68  _mass = other._mass;
69  _position = other._position;
70  _last_position = other._last_position;
71  _velocity = other._velocity;
72  _orientation = other._orientation;
73  _rotation = other._rotation;
74  _terminal_velocity = other._terminal_velocity;
75  _oriented = other._oriented;
76 
77  return *this;
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function : make_copy
82 // Access : Public, virtual
83 // Description : dynamic copy.
84 ////////////////////////////////////////////////////////////////////
86 make_copy() const {
87  return new PhysicsObject(*this);
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function : add_local_impact
92 // Access : Public
93 // Description : Adds an impulse and/or torque (i.e. an instantanious
94 // change in velocity) based on how well the offset and
95 // impulse align with the center of mass (aka position).
96 // If you wanted to immitate this function you could
97 // work out the impulse and torque and call add_impulse
98 // and add_torque respectively.
99 // offset and force are in local coordinates.
100 ////////////////////////////////////////////////////////////////////
101 void PhysicsObject::
102 add_local_impact(const LPoint3 &offset_from_center_of_mass,
103  const LVector3 &force) {
104  nassertv(!offset_from_center_of_mass.is_nan());
105  nassertv(!force.is_nan());
106  add_impact(
107  _orientation.xform(offset_from_center_of_mass),
108  _orientation.xform(force));
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function : add_impact
113 // Access : Public
114 // Description : Adds an impulse and/or torque (i.e. an instantanious
115 // change in velocity) based on how well the offset and
116 // impulse align with the center of mass (aka position).
117 // If you wanted to immitate this function you could
118 // work out the impulse and torque and call add_impulse
119 // and add_torque respectively.
120 // offset and force are in global (or parent) coordinates.
121 ////////////////////////////////////////////////////////////////////
122 void PhysicsObject::
123 add_impact(const LPoint3 &offset,
124  const LVector3 &force) {
125  nassertv(!offset.is_nan());
126  nassertv(!force.is_nan());
127  LVector3 a = offset;
128  LVector3 b = force;
129  a.normalize();
130  b.normalize();
131  a = a.cross(b);
132  PN_stdfloat angle = a.length();
133  if (angle) {
134  LRotation torque;
135  PN_stdfloat spin = force.length()*0.1; // todo: this should account for
136  // impact distance and mass.
137  a.normalize();
138  assert(IS_THRESHOLD_EQUAL(a.length(), 1.0f, 0.001f));
139  torque.set_from_axis_angle(spin, a);
140  add_torque(torque);
141  }
142  LVector3 impulse = (1.0f - angle) * force;
143  add_impulse(impulse);
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function : get_lcs
148 // Access : Public
149 // Description : returns a transform matrix to this object's
150 // local coordinate system.
151 ////////////////////////////////////////////////////////////////////
153 get_lcs() const {
154  LMatrix4 m = LMatrix4::translate_mat(_position);
155  if (_oriented) {
156  m=m*_orientation;
157  }
158  nassertr(!m.is_nan(), m);
159  return m;
160 }
161 
162 ////////////////////////////////////////////////////////////////////
163 // Function : get_inertial_tensor
164 // Access : Public
165 // Description : returns a transform matrix that represents the
166 // object's willingness to be forced.
167 ////////////////////////////////////////////////////////////////////
170  return LMatrix4::ident_mat();
171 }
172 
173 ////////////////////////////////////////////////////////////////////
174 // Function : output
175 // Access : Public
176 // Description : Write a string representation of this instance to
177 // <out>.
178 ////////////////////////////////////////////////////////////////////
179 void PhysicsObject::
180 output(ostream &out) const {
181  #ifndef NDEBUG //[
182  out<<"PhysicsObject";
183  #endif //] NDEBUG
184 }
185 
186 ////////////////////////////////////////////////////////////////////
187 // Function : write
188 // Access : Public
189 // Description : Write a string representation of this instance to
190 // <out>.
191 ////////////////////////////////////////////////////////////////////
192 void PhysicsObject::
193 write(ostream &out, unsigned int indent) const {
194  #ifndef NDEBUG //[
195  out.width(indent);
196  out<<""<<"PhysicsObject "<<_name<<"\n";
197  out.width(indent+2); out<<""; out<<"_position "<<_position<<"\n";
198  out.width(indent+2); out<<""; out<<"_last_position "<<_last_position<<"\n";
199  out.width(indent+2); out<<""; out<<"_velocity "<<_velocity<<"\n";
200  out.width(indent+2); out<<""; out<<"(implicit velocity "<<get_implicit_velocity()<<")\n";
201  out.width(indent+2); out<<""; out<<"_orientation "<<_orientation<<"\n";
202  out.width(indent+2); out<<""; out<<"(hpr "<<_orientation.get_hpr()<<")\n";
203  out.width(indent+2); out<<""; out<<"_rotation "<<_rotation<<"\n";
204  out.width(indent+2); out<<""; out<<"_terminal_velocity "<<_terminal_velocity<<"\n";
205  out.width(indent+2); out<<""; out<<"_mass "<<_mass<<"\n";
206  out.width(indent+2); out<<""; out<<"_process_me "<<_process_me<<"\n";
207  out.width(indent+2); out<<""; out<<"_oriented "<<_oriented<<"\n";
208  #endif //] NDEBUG
209 }
static const LQuaternionf & ident_quat()
Returns an identity quaternion.
Definition: lquaternion.h:851
static const LMatrix4f & ident_mat()
Returns an identity matrix.
Definition: lmatrix.h:903
This is a unit quaternion representing a rotation.
Definition: lrotation.h:92
static LMatrix4f translate_mat(const LVecBase3f &trans)
Returns a matrix that applies the indicated translation.
Definition: lmatrix.h:2397
virtual LMatrix4 get_inertial_tensor() const
returns a transform matrix that represents the object&#39;s willingness to be forced. ...
void add_impulse(const LVector3 &impulse)
Adds an impulse force (i.e.
PhysicsObject()
Default Constructor.
virtual void add_local_impact(const LPoint3 &offset_from_center_of_mass, const LVector3 &impulse)
Adds an impulse and/or torque (i.e.
A body on which physics will be applied.
Definition: physicsObject.h:29
LVector3 get_implicit_velocity() const
Velocity Query over the last dt.
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
bool is_nan() const
Returns true if any component of the vector is not-a-number, false otherwise.
Definition: lvecBase3.h:463
float length() const
Returns the length of the vector, by the Pythagorean theorem.
Definition: lvecBase3.h:765
virtual ~PhysicsObject()
Destructor.
This is a convenience class to specialize ConfigVariable as a floating-point type.
LVecBase3f xform(const LVecBase3f &v) const
Transforms a 3-d vector by the indicated rotation.
Definition: lquaternion.h:280
virtual PhysicsObject * make_copy() const
dynamic copy.
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
void add_torque(const LRotation &torque)
Adds an torque force (i.e.
virtual LMatrix4 get_lcs() const
returns a transform matrix to this object&#39;s local coordinate system.
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
void set_from_axis_angle(float angle_deg, const LVector3f &axis)
angle_deg is the angle about the axis in degrees.
Definition: lquaternion.h:604
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:782
virtual void add_impact(const LPoint3 &offset_from_center_of_mass, const LVector3 &impulse)
Adds an impulse and/or torque (i.e.
LVecBase3f get_hpr(CoordinateSystem cs=CS_default) const
Extracts the equivalent Euler angles from the unit quaternion.
bool is_nan() const
Returns true if any component of the matrix is not-a-number, false otherwise.
Definition: lmatrix.h:1417
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to &lt;out&gt;.