Game Development Reference
In-Depth Information
class GameObject
{
Vec3 m_Pos;
Vec3 m_Vel;
Vec3 m_Accel;
Vec3List m_Forces;
Vec3List m_Impulses;
float m_Mass;
void AddForce(const Vec3 &force) { m_Forces.push_back(force); }
void AddImpulse(const Vec3 &impulse) { m_Impulses.push_back(impulse); }
void OnUpdate(const float time);
};
This class contains 3D vectors for position, velocity, and acceleration. It also has two
lists: one for constant forces and the other for impulses, each of which is modified by
accessor methods that push the force or impulse onto the appropriate list. The real
action happens in the OnUpdate() call.
void GameObject::OnUpdate(const float time)
{
if (m_Mass == 0.0f)
return;
// Add constant forces...
Vec3 F(0,0,0);
Vec3List::iterator it;
for (it=m_Forces.begin(); it!=m_Forces.end(); it++)
{
F += *it;
}
// Also add all the impulses, and then clear the list
for (it=m_Impulses.begin(); it!=m_Impulses.end(); it++)
{
F += *it;
}
m_Impulses.clear();
// calculate new acceleration
m_Accel = F / m_Mass;
m_Vel += m_Accel * time;
m_Pos += m_Vel * time;
}
Search WWH ::




Custom Search