From: Charles McGarvey Date: Wed, 13 Jul 2011 06:58:02 +0000 (-0600) Subject: global gravity for rigid bodies X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=aa67fc8ab5691cad24b55d0e12aba2f6c3e58316;p=chaz%2Fyoink global gravity for rigid bodies --- diff --git a/src/moof/rigid_body.cc b/src/moof/rigid_body.cc new file mode 100644 index 0000000..ce409b4 --- /dev/null +++ b/src/moof/rigid_body.cc @@ -0,0 +1,20 @@ + +/*] Copyright (c) 2009-2011, Charles McGarvey [***************************** +**] All rights reserved. +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +*****************************************************************************/ + +#include "math.hh" + + +namespace moof { + + +scalar global_acceleration; + + +} // namespace moof + diff --git a/src/moof/rigid_body.hh b/src/moof/rigid_body.hh index b8b9048..7ed0a88 100644 --- a/src/moof/rigid_body.hh +++ b/src/moof/rigid_body.hh @@ -27,7 +27,9 @@ namespace moof { -template +extern scalar global_acceleration; + +template struct linear_state { typedef moof::vector< scalar, fixed > vector; @@ -56,22 +58,20 @@ struct linear_state struct gravity_force { - explicit gravity_force(scalar a = -9.8) + gravity_force() { force.zero(); - acceleration = a; } const vector& operator () (const linear_state& state) { - force[1] = state.mass * acceleration; + force[1] = state.mass * global_acceleration; return force; } private: - vector force; - scalar acceleration; + vector force; }; void init() @@ -85,7 +85,7 @@ struct linear_state forces.clear(); mass = SCALAR(1.0); - inverse_mass = 1.0 / mass; + inverse_mass = SCALAR(1.0); } struct derivative @@ -114,9 +114,10 @@ struct linear_state { vector f(force); - for (size_t i = 0; i < forces.size(); ++i) + typename std::vector::const_iterator it; + for (it = forces.begin(); it != forces.end(); ++it) { - f += forces[i](*this); + f += (*it)(*this); } return f; @@ -143,7 +144,7 @@ struct rotational_state2 scalar angular_momentum; // secondary - scalar angularVelocity; + scalar angular_velocity; // constant scalar inertia; @@ -151,18 +152,18 @@ struct rotational_state2 void recalculate() { - angularVelocity = angular_momentum * inertia; + angular_velocity = angular_momentum * inertia; } struct derivative { - scalar angularVelocity; + scalar angular_velocity; scalar torque; }; void step(const derivative& derivative, scalar dt) { - orientation += dt * derivative.angularVelocity; + orientation += dt * derivative.angular_velocity; angular_momentum += dt * derivative.torque; recalculate(); } @@ -176,7 +177,7 @@ struct rotational_state3 // secondary quaternion spin; - vector3 angularVelocity; + vector3 angular_velocity; // constant scalar inertia; @@ -184,7 +185,7 @@ struct rotational_state3 void recalculate() { - angularVelocity = angular_momentum * inertia; + angular_velocity = angular_momentum * inertia; } }; @@ -255,11 +256,6 @@ interpolate(const state3& a, const state3& b, scalar alpha) template class rigid_body : public entity { -protected: - - T state_; - T prev_state_; - public: virtual ~rigid_body() {} @@ -284,6 +280,11 @@ public: { return prev_state_; } + +protected: + + T state_; + T prev_state_; }; typedef rigid_body rigid_body2;