]> Dogcows Code - chaz/yoink/commitdiff
global gravity for rigid bodies
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Wed, 13 Jul 2011 06:58:02 +0000 (00:58 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Wed, 13 Jul 2011 06:58:02 +0000 (00:58 -0600)
src/moof/rigid_body.cc [new file with mode: 0644]
src/moof/rigid_body.hh

diff --git a/src/moof/rigid_body.cc b/src/moof/rigid_body.cc
new file mode 100644 (file)
index 0000000..ce409b4
--- /dev/null
@@ -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
+
index b8b9048c358e92fa70f01ab9d43af899308545eb..7ed0a88131089dc45badd3c872eb7570928c67dc 100644 (file)
@@ -27,7 +27,9 @@
 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 linear_state
 {
        typedef moof::vector< scalar, fixed<D> > vector;
@@ -56,22 +58,20 @@ struct linear_state
 
        struct gravity_force
        {
 
        struct gravity_force
        {
-               explicit gravity_force(scalar a = -9.8)
+               gravity_force()
                {
                        force.zero();
                {
                        force.zero();
-                       acceleration = a;
                }
 
                const vector& operator () (const linear_state& state)
                {
                }
 
                const vector& operator () (const linear_state& state)
                {
-                       force[1] = state.mass * acceleration;
+                       force[1] = state.mass * global_acceleration;
                        return force;
                }
 
        private:
 
                        return force;
                }
 
        private:
 
-               vector force;
-               scalar acceleration;
+               vector  force;
        };
 
        void init()
        };
 
        void init()
@@ -85,7 +85,7 @@ struct linear_state
                forces.clear();
 
                mass = SCALAR(1.0);
                forces.clear();
 
                mass = SCALAR(1.0);
-               inverse_mass = 1.0 / mass;
+               inverse_mass = SCALAR(1.0);
        }
 
        struct derivative
        }
 
        struct derivative
@@ -114,9 +114,10 @@ struct linear_state
        {
                vector f(force);
 
        {
                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;
                }
 
                return f;
@@ -143,7 +144,7 @@ struct rotational_state2
        scalar          angular_momentum;
 
        // secondary
        scalar          angular_momentum;
 
        // secondary
-       scalar          angularVelocity;
+       scalar          angular_velocity;
 
        // constant
        scalar          inertia;
 
        // constant
        scalar          inertia;
@@ -151,18 +152,18 @@ struct rotational_state2
 
        void recalculate()
        {
 
        void recalculate()
        {
-               angularVelocity = angular_momentum * inertia;
+               angular_velocity = angular_momentum * inertia;
        }
 
        struct derivative
        {
        }
 
        struct derivative
        {
-               scalar  angularVelocity;
+               scalar  angular_velocity;
                scalar  torque;
        };
 
        void step(const derivative& derivative, scalar dt)
        {
                scalar  torque;
        };
 
        void step(const derivative& derivative, scalar dt)
        {
-               orientation += dt * derivative.angularVelocity;
+               orientation += dt * derivative.angular_velocity;
                angular_momentum += dt * derivative.torque;
                recalculate();
        }
                angular_momentum += dt * derivative.torque;
                recalculate();
        }
@@ -176,7 +177,7 @@ struct rotational_state3
 
        // secondary
        quaternion      spin;
 
        // secondary
        quaternion      spin;
-       vector3         angularVelocity;
+       vector3         angular_velocity;
 
        // constant
        scalar          inertia;
 
        // constant
        scalar          inertia;
@@ -184,7 +185,7 @@ struct rotational_state3
 
        void recalculate()
        {
 
        void recalculate()
        {
-               angularVelocity = angular_momentum * inertia;
+               angular_velocity = angular_momentum * inertia;
        }
 };
 
        }
 };
 
@@ -255,11 +256,6 @@ interpolate<state3>(const state3& a, const state3& b, scalar alpha)
 template <class T>
 class rigid_body : public entity
 {
 template <class T>
 class rigid_body : public entity
 {
-protected:
-
-       T       state_;
-       T       prev_state_;
-
 public:
 
        virtual ~rigid_body() {}
 public:
 
        virtual ~rigid_body() {}
@@ -284,6 +280,11 @@ public:
        {
                return prev_state_;
        }
        {
                return prev_state_;
        }
+
+protected:
+
+       T       state_;
+       T       prev_state_;
 };
 
 typedef rigid_body<state2> rigid_body2;
 };
 
 typedef rigid_body<state2> rigid_body2;
This page took 0.025905 seconds and 4 git commands to generate.