Hope this helps. It's a simple steering.
I do not manage the angular velocity, only the linear, because on my game the rotation is only visual.
I splitted the code to comment it in the post, but you could write that in 5 lines.
I use those values on my game :
maxSpeed = 5
control = 0.05
I do not manage the angular velocity, only the linear, because on my game the rotation is only visual.
Code:
//----------------------------------------------------------------------------------------
// dt : frame delta time
// targetPosition : the position to aim
// maxSpeed : the maximum missile speed
// control : a value bewteen [0,1]. 1 means reach the desired velocity in one frame. 0 means never reach the desired velocity.
//----------------------------------------------------------------------------------------
void Missile::UpdateAiming(float dt, const b2Vec2& targetPosition, float maxSpeed, float control)
{
b2Vec2 missilePosition = m_body->GetPosition();
b2Vec2 diff = targetPosition - missilePosition;
float dist = diff.Length();
if (dist > 0)
{
// compute the aiming direction
b2Vec2 direction = diff / dist;
// get the current missile velocity because we will apply a force to compensate this.
b2Vec2 currentVelocity = m_body->GetLinearVelocity();
// the missile ideal velocity is the direction to the target multiplied by the max speed
b2Vec2 desireVelocity = direction * maxSpeed;
// compensate the current missile velocity by the desired velocity, based on the control factor
b2Vec2 finalVelocity = control * (desireVelocity - currentVelocity);
// transform our velocity into an impulse (get rid of the time and mass factor)
b2Vec2 finalForce = finalVelocity * (m_body->GetMass() / dt);
// finaly apply the force
m_body->ApplyForce(finalForce, b2Vec2_zero);
}
}
// dt : frame delta time
// targetPosition : the position to aim
// maxSpeed : the maximum missile speed
// control : a value bewteen [0,1]. 1 means reach the desired velocity in one frame. 0 means never reach the desired velocity.
//----------------------------------------------------------------------------------------
void Missile::UpdateAiming(float dt, const b2Vec2& targetPosition, float maxSpeed, float control)
{
b2Vec2 missilePosition = m_body->GetPosition();
b2Vec2 diff = targetPosition - missilePosition;
float dist = diff.Length();
if (dist > 0)
{
// compute the aiming direction
b2Vec2 direction = diff / dist;
// get the current missile velocity because we will apply a force to compensate this.
b2Vec2 currentVelocity = m_body->GetLinearVelocity();
// the missile ideal velocity is the direction to the target multiplied by the max speed
b2Vec2 desireVelocity = direction * maxSpeed;
// compensate the current missile velocity by the desired velocity, based on the control factor
b2Vec2 finalVelocity = control * (desireVelocity - currentVelocity);
// transform our velocity into an impulse (get rid of the time and mass factor)
b2Vec2 finalForce = finalVelocity * (m_body->GetMass() / dt);
// finaly apply the force
m_body->ApplyForce(finalForce, b2Vec2_zero);
}
}
I splitted the code to comment it in the post, but you could write that in 5 lines.
I use those values on my game :
maxSpeed = 5
control = 0.05