Arrive Function

This topic is 4175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, so I have this code for my AI's Arrive function.
Vector2D EGsoldier::Arrive(Vector2D TargetPos)   //Need to improve on this function
{
Vector2D ToTarget = TargetPos - Pos();
double dist = ToTarget.Length();

if( dist > 0 )
{
double speed = dist/( 1.3 );
speed = min(speed, e_dMaxSpeed);
Vector2D DesiredVelocity = ToTarget*speed/dist;
Vector2D FinalVelocity = DesiredVelocity - Velocity();

return FinalVelocity;
}

return Vector2D(0,0);
}


It returns the 2D vector where my EGsoldier should go. The problem is, that if there is an obstacle in the way, the soldier has no idea what to do (and subsequently runs into the obstacle and stays there). Can someone help me figure out how to improve this function so that the EGsoldier can get to his destination w/o running into an object?

Share on other sites
Create a vector that resembles the difference between the obstacle.pos and the current unit.pos, This should be normalized and converted to appropriate size.

Then add this vector to the current unit.pos

You may want to add a random number to the new direction vector before adding it, since if the collision is head-on, it might get stuck (pinball-style) trying to repell in direct opposite direction.

Share on other sites
Quote:
 Original post by bballmitchCan someone help me figure out how to improve this function so that the EGsoldier can get to his destination w/o running into an object?

Rather than change this function, design a function for 'Avoid', which will generate a movement vector to avoid the object. The resulting movement should be a weighted sum of the outputs of the Arrive and Avoid functions. This weight would be changed depending on the proximity of the object. So, when the soldier is moving toward the object but is still some distance from it, there would be a small perturbation to the path to take it around the object. If the object suddently appeared right in front of it, the Avoid behaviour would dominate and the Arrive behaviour would be minimised, so that the soldier first avoided the object and then went back to heading to the goal. Make sense?

I suggest you look at the full set of steering behaviours available for reactive pathing.

Cheers,

Timkin