]>
Dogcows Code - chaz/yoink/blob - src/Moof/RigidBody.hh
ad6e33ff67c2135115dfe0b2700bdb863bc99686
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_RIGIDBODY_HH_
13 #define _MOOF_RIGIDBODY_HH_
17 #include <boost/bind.hpp>
18 #include <boost/function.hpp>
20 #include <Moof/Entity.hh>
21 #include <Moof/Math.hh>
30 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
31 typedef boost::function
<const Vector
& (const LinearState
&)>
46 std::vector
<ForceFunction
> forces
;
54 void recalculateLinear()
56 velocity
= momentum
* inverseMass
;
62 explicit GravityForce(Scalar a
= -9.8)
68 const Vector
& operator () (const LinearState
& state
)
70 force
[1] = state
.mass
* acceleration
;
92 inverseMass
= 1.0 / mass
;
101 Derivative
operator*(Scalar dt
) const
103 Derivative derivative
;
104 derivative
.velocity
= dt
* velocity
;
105 derivative
.force
= dt
* force
;
109 Derivative
operator+(const Derivative
& other
) const
111 Derivative derivative
;
112 derivative
.velocity
= velocity
+ other
.velocity
;
113 derivative
.force
= force
+ other
.force
;
119 Vector
getForce() const
123 for (size_t i
= 0; i
< forces
.size(); ++i
)
125 f
+= forces
[i
](*this);
131 void getDerivative(Derivative
& derivative
, Scalar t
) const
133 derivative
.velocity
= velocity
;
134 derivative
.force
= getForce();
137 void step(const Derivative
& derivative
, Scalar dt
)
139 position
+= dt
* derivative
.velocity
;
140 momentum
+= dt
* derivative
.force
;
146 struct RotationalState2
151 Scalar angularMomentum
;
155 Scalar angularVelocity
;
160 Scalar inverseInertia
;
163 void recalculateRotational()
165 angularVelocity
= angularMomentum
* inertia
;
171 Scalar angularVelocity
;
175 void step(const Derivative
& derivative
, Scalar dt
)
177 orientation
+= dt
* derivative
.angularVelocity
;
178 angularMomentum
+= dt
* derivative
.torque
;
179 recalculateRotational();
183 struct RotationalState3
187 Quaternion orientation
;
188 Vector3 angularMomentum
;
193 Vector3 angularVelocity
;
198 Scalar inverseInertia
;
201 void recalculateRotational()
203 angularVelocity
= angularMomentum
* inertia
;
208 struct State2
: public LinearState
<2>, public RotationalState2
213 recalculateRotational();
216 void update(Scalar t
, Scalar dt
)
218 rk4
<LinearState
<2>,LinearState
<2>::Derivative
>(*this, t
, dt
);
222 struct State3
: public LinearState
<3>, public RotationalState3
227 recalculateRotational();
230 void update(Scalar t
, Scalar dt
)
232 rk4
<LinearState
<3>,LinearState
<3>::Derivative
>(*this, t
, dt
);
238 inline T
interpolate(const T
& a
, const T
& b
, Scalar alpha
)
240 return cml::lerp(a
, b
, alpha
);
244 inline State2 interpolate
<State2
>(const State2
& a
, const State2
& b
,
248 state
.position
= interpolate(a
.position
, b
.position
, alpha
);
249 state
.momentum
= interpolate(a
.momentum
, b
.momentum
, alpha
);
250 state
.orientation
= interpolate(a
.orientation
, b
.orientation
, alpha
);
251 state
.angularMomentum
= interpolate(a
.angularMomentum
,
252 b
.angularMomentum
, alpha
);
257 inline State3 interpolate
<State3
>(const State3
& a
, const State3
& b
,
261 state
.position
= interpolate(a
.position
, b
.position
, alpha
);
262 state
.momentum
= interpolate(a
.momentum
, b
.momentum
, alpha
);
263 state
.orientation
= cml::slerp(a
.orientation
, b
.orientation
, alpha
);
264 state
.angularMomentum
= interpolate(a
.angularMomentum
,
265 b
.angularMomentum
, alpha
);
272 * Interface for anything that can move.
276 class RigidBody
: public Entity
285 virtual ~RigidBody() {}
287 virtual void update(Scalar t
, Scalar dt
)
290 mState
.update(t
, dt
);
293 const T
& getState() const
298 T
getState(Scalar alpha
) const
300 return interpolate(mPrevState
, mState
, alpha
);
303 const T
& getLastState() const
309 typedef RigidBody
<State2
> RigidBody2
;
310 typedef RigidBody
<State3
> RigidBody3
;
315 #endif // _MOOF_RIGIDBODY_HH_
This page took 0.045582 seconds and 4 git commands to generate.