--- /dev/null
+
+/*] 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
+
namespace moof {
-template <int D = 3>
+extern scalar global_acceleration;
+
+template <int D>
struct linear_state
{
typedef moof::vector< scalar, fixed<D> > vector;
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()
forces.clear();
mass = SCALAR(1.0);
- inverse_mass = 1.0 / mass;
+ inverse_mass = SCALAR(1.0);
}
struct derivative
{
vector f(force);
- for (size_t i = 0; i < forces.size(); ++i)
+ typename std::vector<force_function>::const_iterator it;
+ for (it = forces.begin(); it != forces.end(); ++it)
{
- f += forces[i](*this);
+ f += (*it)(*this);
}
return f;
scalar angular_momentum;
// secondary
- scalar angularVelocity;
+ scalar angular_velocity;
// constant
scalar inertia;
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();
}
// secondary
quaternion spin;
- vector3 angularVelocity;
+ vector3 angular_velocity;
// constant
scalar inertia;
void recalculate()
{
- angularVelocity = angular_momentum * inertia;
+ angular_velocity = angular_momentum * inertia;
}
};
template <class T>
class rigid_body : public entity
{
-protected:
-
- T state_;
- T prev_state_;
-
public:
virtual ~rigid_body() {}
{
return prev_state_;
}
+
+protected:
+
+ T state_;
+ T prev_state_;
};
typedef rigid_body<state2> rigid_body2;