]>
Dogcows Code - chaz/yoink/blob - src/moof/rigid_body.hh
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_RIGID_BODY_HH_
13 #define _MOOF_RIGID_BODY_HH_
17 * Classes and functions for simulating rigid body physics.
22 #include <boost/bind.hpp>
23 #include <boost/function.hpp>
25 #include <moof/entity.hh>
26 #include <moof/math.hh>
35 typedef moof::vector
< scalar
, fixed
<D
> > vector
;
36 typedef boost::function
<const vector
& (const linear_state
&)>
51 std::vector
<force_function
> forces
;
61 velocity
= momentum
* inverse_mass
;
67 explicit gravity_force(scalar a
= -9.8)
73 const vector
& operator () (const linear_state
& state
)
75 force
[1] = state
.mass
* acceleration
;
98 inverse_mass
= 1.0 / mass
;
107 derivative
operator * (scalar dt
) const
109 derivative derivative
;
110 derivative
.velocity
= dt
* velocity
;
111 derivative
.force
= dt
* force
;
115 derivative
operator + (const derivative
& other
) const
117 derivative derivative
;
118 derivative
.velocity
= velocity
+ other
.velocity
;
119 derivative
.force
= force
+ other
.force
;
125 vector
total_force() const
129 for (size_t i
= 0; i
< forces
.size(); ++i
)
131 f
+= forces
[i
](*this);
137 void calculate_derivative(derivative
& derivative
, scalar t
) const
139 derivative
.velocity
= velocity
;
140 derivative
.force
= total_force();
143 void step(const derivative
& derivative
, scalar dt
)
145 position
+= dt
* derivative
.velocity
;
146 momentum
+= dt
* derivative
.force
;
152 struct rotational_state2
157 scalar angular_momentum
;
161 scalar angularVelocity
;
166 scalar inverse_inertia
;
171 angularVelocity
= angular_momentum
* inertia
;
177 scalar angularVelocity
;
181 void step(const derivative
& derivative
, scalar dt
)
183 orientation
+= dt
* derivative
.angularVelocity
;
184 angular_momentum
+= dt
* derivative
.torque
;
189 struct rotational_state3
193 quaternion orientation
;
194 vector3 angular_momentum
;
199 vector3 angularVelocity
;
204 scalar inverse_inertia
;
209 angularVelocity
= angular_momentum
* inertia
;
214 struct state2
: public linear_state
<2>, public rotational_state2
218 linear_state
<2>::recalculate();
219 rotational_state2::recalculate();
222 void update(scalar t
, scalar dt
)
224 rk4
<linear_state
<2>,linear_state
<2>::derivative
>(*this, t
, dt
);
228 struct state3
: public linear_state
<3>, public rotational_state3
232 linear_state
<3>::recalculate();
233 rotational_state3::recalculate();
236 void update(scalar t
, scalar dt
)
238 rk4
<linear_state
<3>,linear_state
<3>::derivative
>(*this, t
, dt
);
244 inline T
interpolate(const T
& a
, const T
& b
, scalar alpha
)
246 return lerp(a
, b
, alpha
);
250 inline state2 interpolate
<state2
>(const state2
& a
, const state2
& b
,
254 state
.position
= interpolate(a
.position
, b
.position
, alpha
);
255 state
.momentum
= interpolate(a
.momentum
, b
.momentum
, alpha
);
256 state
.orientation
= interpolate(a
.orientation
, b
.orientation
, alpha
);
257 state
.angular_momentum
= interpolate(a
.angular_momentum
,
258 b
.angular_momentum
, alpha
);
263 inline state3 interpolate
<state3
>(const state3
& a
, const state3
& b
,
267 state
.position
= interpolate(a
.position
, b
.position
, alpha
);
268 state
.momentum
= interpolate(a
.momentum
, b
.momentum
, alpha
);
269 state
.orientation
= slerp(a
.orientation
, b
.orientation
, alpha
);
270 state
.angular_momentum
= interpolate(a
.angular_momentum
,
271 b
.angular_momentum
, alpha
);
278 * Interface for anything that can move.
282 class rigid_body
: public entity
291 virtual ~rigid_body() {}
293 virtual void update(scalar t
, scalar dt
)
295 prev_state_
= state_
;
296 state_
.update(t
, dt
);
299 const T
& state() const
304 T
state(scalar alpha
) const
306 return interpolate(prev_state_
, state_
, alpha
);
309 const T
& getLastState() const
315 typedef rigid_body
<state2
> rigid_body2
;
316 typedef rigid_body
<state3
> rigid_body3
;
321 #endif // _MOOF_RIGID_BODY_HH_
This page took 0.049466 seconds and 4 git commands to generate.