0
\$\begingroup\$

I'm reading a book named "Programming Game AI by Example". Here's a method to help an autonomous character to slow down when it's getting closer and closer to its target:

Vector2D SteeringBehavior::Arrive(Vector2D TargetPos, Deceleration deceleration)
{
    Vector2D ToTarget = TargetPos - m_pVehicle->Pos();

    //calculate the distance to the target
    double dist = ToTarget.Length();

    if (dist > 0)
    {
        //because Deceleration is enumerated as an int, this value is required
        //to provide fine tweaking of the deceleration..
        const double DecelerationTweaker = 0.3;

        //calculate the speed required to reach the target given the desired
        //deceleration
        double speed =  dist / ((double)deceleration * DecelerationTweaker);     

        //make sure the velocity does not exceed the max
        speed = min(speed, m_pVehicle->MaxSpeed());

        //from here proceed just like Seek except we don't need to normalize 
        //the ToTarget vector because we have already gone to the trouble
        //of calculating its length: dist. 
        Vector2D DesiredVelocity =  ToTarget * speed / dist;

        return (DesiredVelocity - m_pVehicle->Velocity());
    }

    return Vector2D(0,0);
}

For this line:

//calculate the speed required to reach the target given the desired
//deceleration
double speed =  dist / ((double)deceleration * DecelerationTweaker);

The Deceleration is an enum like this.

//Arrive makes use of these to determine how quickly a vehicle
//should decelerate to its target
  enum Deceleration{slow = 3, normal = 2, fast = 1};

I just don't understand how can it get a speed by dividing a distance by an acceleration (deceleration)? Did I misunderstand anything?

\$\endgroup\$
  • 2
    \$\begingroup\$ Can you show us where deceleration is defined? This alludes to it being an enumeration. I'm wondering if it was perhaps just imprecisely named and does not correspond exactly to a m/s^2 change in speed over time. \$\endgroup\$ – DMGregory May 22 '18 at 12:07
  • \$\begingroup\$ Sorry for missing that. I updated the code. It is an enum type.enum Deceleration{slow = 3, normal = 2, fast = 1}; \$\endgroup\$ – Phoenix Feng May 23 '18 at 1:23
0
\$\begingroup\$

This is difficult to understand because it’s incredibly unclear code. It begins to make more sense if you read "deceleration" as "factor of slowness", or even more precisely, "deceleration * DecelerationTweaker" is the "amount of time the object spends to traverse the distance".

The speed variable is the distance the object needs to traverse divided by some amount of time (0.9, 0.6, or 0.3) to give a normalized speed. It would be more readable if it were something like:

double speed = distanceToTravel / timeToTravel

The speed is then checked against the object's maximum speed to ensure that it does not exceed it.

Finally, the desired velocity needed to traverse the desired distance is determined on a line that seems poorly structured for readability. The "ToTarget" vector should be first divided by "dist" to show with clarity that the intention is a normalized vector, since "dist" is merely the length of "ToTarget". The normalized vector is multiplied by the speed to obtain the proper velocity vector. It would be more readable if it were something like:

Vector2D DesiredVelocity = (ToTarget / ToTarget.length) * speed;

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.