24 #include "gfx/quaternion.h"
27 , MomentOfInertia( I )
29 , NetTorque( 0, 0, 0 )
30 , AngularVelocity( 0, 0, 0 )
43 NetTorque += ResistiveForceCoef*AngularVelocity;
47 NetForce += ResistiveForceCoef*Velocity;
52 float mag = V1.Magnitude();
53 float t = mag/fabs( strength );
58 Vector V1 = AngularVelocity;
59 float mag = V1.Magnitude();
60 float t = mag/fabs( strength );
66 if ( (AngularVelocity.i || AngularVelocity.j || AngularVelocity.k) && ResistiveForceCoef ) {
67 Vector temp = AngularVelocity;
68 NetTorque += (ResistiveForceCoef*AngularVelocity*AngularVelocity)*temp.Normalize();
73 if ( (Velocity.i || Velocity.j || Velocity.k) && ResistiveForceCoef ) {
74 Vector temp = Velocity;
75 NetForce += (ResistiveForceCoef*Velocity*Velocity)*temp.Normalize();
82 NetForce = Vector( 0, 0, 0 );
83 NetTorque = Vector( 0, 0, 0 );
88 float theta = axis.Magnitude();
91 float ootheta = 1/theta;
92 float s =
cos( theta*.5 );
93 Quaternion rot = Quaternion( s, axis*(
sin( theta*.5 )*ootheta) );
94 Quaternion rotprime = rot.Conjugate();
95 Quaternion pquat = rot*Quaternion( 0, *p )*rotprime;
96 Quaternion qquat = rot*Quaternion( 0, *q )*rotprime;
97 Quaternion rquat = rot*Quaternion( 0, *r )*rotprime;
117 ActiveForces[NumActiveForces].
F = Vforce;
118 ActiveForces[NumActiveForces].
t = time;
127 ActiveTorques[NumActiveTorques].
F = (Location.Cast()-*pos).Cast().Cross( Vforce );
128 ActiveTorques[NumActiveTorques].
t = time;
137 ActiveTorques[NumActiveTorques].
F = Location.Cross( Vforce );
138 ActiveTorques[NumActiveTorques].
t = time;
146 ActiveTorques[NumActiveTorques].
F = Location.Cross( Vforce );
147 ActiveTorques[NumActiveTorques].
t = time;
153 Vector temptorque = Time*NetTorque;
154 Vector tempforce = Time*NetForce;
156 for (i = 0; i < NumActiveTorques; ++i) {
157 if (Time >= ActiveTorques[i].t) {
158 temptorque += ActiveTorques[i].
t*ActiveTorques[i].
F;
159 ActiveTorques[i].
F = ActiveTorques[NumActiveTorques-1].
F;
160 ActiveTorques[i].
t = ActiveTorques[NumActiveTorques-1].
t;
164 temptorque += Time*ActiveTorques[i].
F;
165 ActiveTorques[i].
t -= Time;
168 for (i = 0; i < NumActiveForces; ++i) {
169 if (Time >= ActiveForces[i].t) {
170 tempforce += ActiveForces[i].
t*ActiveForces[i].
F;
171 ActiveForces[i].
F = ActiveForces[NumActiveForces-1].
F;
172 ActiveForces[i].
t = ActiveForces[NumActiveForces-1].
t;
176 tempforce += Time*ActiveForces[i].
F;
177 ActiveForces[i].
t -= Time;
180 temptorque = temptorque*(0.5/MomentOfInertia);
181 Rotate( AngularVelocity+0.5*temptorque );
182 AngularVelocity += temptorque;
183 tempforce = tempforce*(0.5/mass);
185 if (fabs( Velocity.i )+fabs( Velocity.j )+fabs( Velocity.k ) > co10) {
186 float magvel = Velocity.Magnitude();
187 float y = (1-magvel*magvel*oocc);
188 tempforce = tempforce*powf( y, 1.5 );
190 *pos += (Velocity+.5*tempforce).Cast();
191 Velocity += tempforce;