float maxForce = MAX_FORCE;
sf::Vector2f vel = m_Velocity;
sf::Vector2f pos = m_Position;
sf::Vector2f steer; // The steering vector.
sf::Vector2f des = target - pos; // A vector pointing from the location to the target.
float d = sfm::Length(des); // Distance from the target is the magnitude of the vector.
if (d > 0) {
sfm::Normalize(des);
des *= MAX_SPEED;
steer = des - vel;
// Limit to maximum steering force
float steerLength = sfm::Length(steer);
if (steerLength > maxForce)
{
steer *= maxForce/steerLength;
}
} else {
steer = sf::Vector2f(0.0f, 0.0f);
}
m_Acceleration = steer;
Then every frame the position as calculated as such:
m_Velocity += m_Acceleration*m_FrameTime;
if (sfm::Length(m_Velocity) > MAX_SPEED)
{
sfm::Normalize(m_Velocity);
m_Velocity *= MAX_SPEED;
}
m_Position += m_Velocity*m_FrameTime;
m_Acceleration = sf::Vector2f(0.0f, 0.0f);
Any help would be appreciated thanks!