Panda3D
 All Classes Functions Variables Enumerations
physicsObject.cxx
00001 // Filename: physicsObject.cxx
00002 // Created by:  charles (13Jun00)
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 "physicsObject.h"
00016 
00017 ConfigVariableDouble PhysicsObject::_default_terminal_velocity
00018 ("default_terminal_velocity", 400.0f);
00019 
00020 TypeHandle PhysicsObject::_type_handle;
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function : PhysicsObject
00024 //       Access : Public
00025 //  Description : Default Constructor
00026 ////////////////////////////////////////////////////////////////////
00027 PhysicsObject::
00028 PhysicsObject() :
00029   _terminal_velocity(_default_terminal_velocity),
00030   _mass(1.0f),
00031   _process_me(false),
00032   _oriented(true)
00033 {
00034   _position.set(0.0f, 0.0f, 0.0f);
00035   _last_position = _position;
00036   _velocity.set(0.0f, 0.0f, 0.0f);
00037   _orientation.set(1.0 ,0.0f, 0.0f, 0.0f);
00038   _rotation = LRotation::ident_quat();
00039 }
00040 
00041 ////////////////////////////////////////////////////////////////////
00042 //     Function : PhysicsObject
00043 //       Access : Public
00044 //  Description : copy constructor
00045 ////////////////////////////////////////////////////////////////////
00046 PhysicsObject::
00047 PhysicsObject(const PhysicsObject& copy) {
00048   operator=(copy);
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function : ~PhysicsObject
00053 //       Access : Public
00054 //  Description : Destructor
00055 ////////////////////////////////////////////////////////////////////
00056 PhysicsObject::
00057 ~PhysicsObject() {
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function : Assignment operator
00062 //       Access : Public
00063 //  Description :
00064 ////////////////////////////////////////////////////////////////////
00065 const PhysicsObject &PhysicsObject::
00066 operator =(const PhysicsObject &other) {
00067   _process_me = other._process_me;
00068   _mass = other._mass;
00069   _position = other._position;
00070   _last_position = other._last_position;
00071   _velocity = other._velocity;
00072   _orientation = other._orientation;
00073   _rotation = other._rotation;
00074   _terminal_velocity = other._terminal_velocity;
00075   _oriented = other._oriented;
00076 
00077   return *this;
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function : make_copy
00082 //       Access : Public, virtual
00083 //  Description : dynamic copy.
00084 ////////////////////////////////////////////////////////////////////
00085 PhysicsObject *PhysicsObject::
00086 make_copy() const {
00087   return new PhysicsObject(*this);
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //    Function : add_local_impact
00092 //      Access : Public
00093 // Description : Adds an impulse and/or torque (i.e. an instantanious
00094 //               change in velocity) based on how well the offset and
00095 //               impulse align with the center of mass (aka position).
00096 //               If you wanted to immitate this function you could
00097 //               work out the impulse and torque and call add_impulse
00098 //               and add_torque respectively.
00099 //               offset and force are in local coordinates.
00100 ////////////////////////////////////////////////////////////////////
00101 void PhysicsObject::
00102 add_local_impact(const LPoint3 &offset_from_center_of_mass,
00103     const LVector3 &force) {
00104   nassertv(!offset_from_center_of_mass.is_nan());
00105   nassertv(!force.is_nan());
00106   add_impact(
00107       _orientation.xform(offset_from_center_of_mass),
00108       _orientation.xform(force));
00109 }
00110 
00111 ////////////////////////////////////////////////////////////////////
00112 //    Function : add_impact
00113 //      Access : Public
00114 // Description : Adds an impulse and/or torque (i.e. an instantanious
00115 //               change in velocity) based on how well the offset and
00116 //               impulse align with the center of mass (aka position).
00117 //               If you wanted to immitate this function you could
00118 //               work out the impulse and torque and call add_impulse
00119 //               and add_torque respectively.
00120 //               offset and force are in global (or parent) coordinates.
00121 ////////////////////////////////////////////////////////////////////
00122 void PhysicsObject::
00123 add_impact(const LPoint3 &offset,
00124     const LVector3 &force) {
00125   nassertv(!offset.is_nan());
00126   nassertv(!force.is_nan());
00127   LVector3 a = offset;
00128   LVector3 b = force;
00129   a.normalize();
00130   b.normalize();
00131   a = a.cross(b);
00132   PN_stdfloat angle = a.length();
00133   if (angle) {
00134     LRotation torque;
00135     PN_stdfloat spin = force.length()*0.1; // todo: this should account for
00136                                         // impact distance and mass.
00137     a.normalize();
00138     assert(IS_THRESHOLD_EQUAL(a.length(), 1.0f, 0.001f));
00139     torque.set_from_axis_angle(spin, a);
00140     add_torque(torque);
00141   }
00142   LVector3 impulse = (1.0f - angle) * force;
00143   add_impulse(impulse);
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //     Function : get_lcs
00148 //       Access : Public
00149 //  Description : returns a transform matrix to this object's
00150 //                local coordinate system.
00151 ////////////////////////////////////////////////////////////////////
00152 LMatrix4 PhysicsObject::
00153 get_lcs() const {
00154   LMatrix4 m = LMatrix4::translate_mat(_position);
00155   if (_oriented) {
00156     m=m*_orientation;
00157   }
00158   nassertr(!m.is_nan(), m);
00159   return m;
00160 }
00161 
00162 ////////////////////////////////////////////////////////////////////
00163 //     Function : get_inertial_tensor
00164 //       Access : Public
00165 //  Description : returns a transform matrix that represents the
00166 //                object's willingness to be forced.
00167 ////////////////////////////////////////////////////////////////////
00168 LMatrix4 PhysicsObject::
00169 get_inertial_tensor() const {
00170   return LMatrix4::ident_mat();
00171 }
00172 
00173 ////////////////////////////////////////////////////////////////////
00174 //     Function : output
00175 //       Access : Public
00176 //  Description : Write a string representation of this instance to
00177 //                <out>.
00178 ////////////////////////////////////////////////////////////////////
00179 void PhysicsObject::
00180 output(ostream &out) const {
00181   #ifndef NDEBUG //[
00182   out<<"PhysicsObject";
00183   #endif //] NDEBUG
00184 }
00185 
00186 ////////////////////////////////////////////////////////////////////
00187 //     Function : write
00188 //       Access : Public
00189 //  Description : Write a string representation of this instance to
00190 //                <out>.
00191 ////////////////////////////////////////////////////////////////////
00192 void PhysicsObject::
00193 write(ostream &out, unsigned int indent) const {
00194   #ifndef NDEBUG //[
00195   out.width(indent);
00196   out<<""<<"PhysicsObject "<<_name<<"\n";
00197   out.width(indent+2); out<<""; out<<"_position "<<_position<<"\n";
00198   out.width(indent+2); out<<""; out<<"_last_position "<<_last_position<<"\n";
00199   out.width(indent+2); out<<""; out<<"_velocity "<<_velocity<<"\n";
00200   out.width(indent+2); out<<""; out<<"(implicit velocity "<<get_implicit_velocity()<<")\n";
00201   out.width(indent+2); out<<""; out<<"_orientation "<<_orientation<<"\n";
00202   out.width(indent+2); out<<""; out<<"(hpr "<<_orientation.get_hpr()<<")\n";
00203   out.width(indent+2); out<<""; out<<"_rotation "<<_rotation<<"\n";
00204   out.width(indent+2); out<<""; out<<"_terminal_velocity "<<_terminal_velocity<<"\n";
00205   out.width(indent+2); out<<""; out<<"_mass "<<_mass<<"\n";
00206   out.width(indent+2); out<<""; out<<"_process_me "<<_process_me<<"\n";
00207   out.width(indent+2); out<<""; out<<"_oriented "<<_oriented<<"\n";
00208   #endif //] NDEBUG
00209 }
 All Classes Functions Variables Enumerations