00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "RigidBody.h"
00013
#include <OpenCAL/PhaseSpace.h>
00014
using namespace OpenCAL;
00015
00016
using namespace std;
00017
00018
00019
00020
00021
00022
00023 RigidBody::RigidBody(
System *parent,
float mass)
00024 :
PhysicalObject(parent, mass),
Rigid()
00025 {
00026
#ifdef VERBOSE
00027
Debug::print(
"RigidBody constructor", 2);
00028
#endif // VERBOSE
00029
00030 }
00031
00032 RigidBody::~RigidBody()
00033 {
00034
#ifdef VERBOSE
00035
Debug::print(
"RigidBody destructor", 2);
00036
#endif // VERBOSE
00037
}
00038
00039
00040
00041
00042
00043
00044
void RigidBody::addForce(
const Vector3 &force,
const Vector3 &position)
00045 {
00046 m_force += force;
00047
00048 m_torque += position.crossProduct(force);
00049 }
00050
00051
00052
00053
00054
00055
00056 Vector3 RigidBody::getAngularVelocity()
const
00057
{
00058
Matrix33 rot =
m_rotation.
toMatrix33();
00059
Matrix33 invInertia = rot *
m_invInertia * rot.
transposed();
00060
return invInertia * m_angularMomentum;
00061 }
00062
00063 void RigidBody::setAngularVelocity(
const Vector3 &velocity)
00064 {
00066
00067 cout <<
"InvInertia:" << endl;
00068
m_invInertia.print();
00069
00070 Matrix33 inertia =
m_invInertia;
00071 inertia.set(0,0, 1.0f / inertia.get(0,0));
00072 inertia.set(1,1, 1.0f / inertia.get(1,1));
00073 inertia.set(2,2, 1.0f / inertia.get(2,2));
00074 cout <<
"Inertia:" << endl;
00075 inertia.print();
00076
00077 Matrix33 rot = m_rotation.
toMatrix33();
00078 inertia = rot * inertia * rot.transposed();
00079 cout <<
"Inertia world:" << endl;
00080 inertia.print();
00081
00082 m_angularMomentum = inertia * velocity;
00083 }
00084
00085
void RigidBody::initialize()
00086 {
00087
00088
00089
00090
00091
00092
00093 PhysicalObject::initialize();
00094
00095 cout <<
"Initializing rigid body!" << endl;
00096
00097 calculateVolume();
00098 calculateInversedInertia();
00099 }
00100
00101
void RigidBody::addToState(
PhaseSpace *state)
00102 {
00103 state->
concat(m_position);
00104 state->
concat(m_rotation);
00105 state->
concat(m_velocity);
00106 state->
concat(m_angularMomentum);
00107 }
00108
00109 void RigidBody::addToDerivative(
PhaseSpace *deriv)
00110 {
00111
if(isFixed())
00112 deriv->
concatZeros(13);
00113
else
00114 {
00115
00116
00118 Matrix33 rot = m_rotation.
toMatrix33();
00119 Matrix33 invInertia = rot *
m_invInertia * rot.transposed();
00120 Vector3 omega = invInertia * m_angularMomentum;
00121
Quaternion qdot = 0.5f * (omega * m_rotation);
00122
00123
00124
00125 deriv->
concat(m_velocity);
00126 deriv->
concat(qdot);
00127 deriv->
concat(getAccelleration());
00128 deriv->
concat(m_torque);
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 }
00146 }
00147
00148 void RigidBody::fromState(
PhaseSpace *state)
00149 {
00150
if(isFixed())
00151 state->
skip(13);
00152
else
00153 {
00154 m_position.setX(state->
getNext());
00155 m_position.setY(state->
getNext());
00156 m_position.setZ(state->
getNext());
00157
00158 m_rotation.
setScalar(state->
getNext());
00159 m_rotation.
setVector(Vector3(state->
getNext(), state->
getNext(), state->
getNext()));
00160
00161 m_velocity.setX(state->
getNext());
00162 m_velocity.setY(state->
getNext());
00163 m_velocity.setZ(state->
getNext());
00164
00165 m_angularMomentum.setX(state->
getNext());
00166 m_angularMomentum.setY(state->
getNext());
00167 m_angularMomentum.setZ(state->
getNext());
00168 }
00169
00171 m_force = Vector3::zero;
00172 m_torque = Vector3::zero;
00173 }
00174
00175
void RigidBody::disable()
00176 {
00177 m_force = m_torque = Vector3::zero;
00178 m_velocity = m_angularMomentum = Vector3::zero;
00179 m_enabled =
false;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206