00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00024
00025
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
00043
00044
00045
00046 PhysicsObject::
00047 PhysicsObject(const PhysicsObject& copy) {
00048 operator=(copy);
00049 }
00050
00051
00052
00053
00054
00055
00056 PhysicsObject::
00057 ~PhysicsObject() {
00058 }
00059
00060
00061
00062
00063
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
00082
00083
00084
00085 PhysicsObject *PhysicsObject::
00086 make_copy() const {
00087 return new PhysicsObject(*this);
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
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
00113
00114
00115
00116
00117
00118
00119
00120
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;
00136
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
00148
00149
00150
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
00164
00165
00166
00167
00168 LMatrix4 PhysicsObject::
00169 get_inertial_tensor() const {
00170 return LMatrix4::ident_mat();
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 void PhysicsObject::
00180 output(ostream &out) const {
00181 #ifndef NDEBUG //[
00182 out<<"PhysicsObject";
00183 #endif //] NDEBUG
00184 }
00185
00186
00187
00188
00189
00190
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 }