Skip to main content
added 209 characters in body
Source Link
Lee
  • 103
  • 3

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?

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);

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

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?

Fixed code indentation, minor typos; removed mistaken 'physics' tag, added more appropriate 'steering-behaviors'tag
Source Link
liggiorgio
  • 4.9k
  • 6
  • 26
  • 37

I'm reading a book named Programming"Programming Game AI by ExampleExample". Here's Here's a method to help aan 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);

I just don't understand how couldcan it get a speed by makedividing a distance divided by aan acceleration  (deceleration)? Did Did I misunderstand anything?

I'm reading a book named Programming Game AI by Example. Here's a method to help a autonomous character 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);

I just don't understand how could it get a speed by make a distance divided by a acceleration(deceleration)? Did I misunderstand anything?

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);

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

Source Link
Lee
  • 103
  • 3

What does distance divided by acceleration mean?

I'm reading a book named Programming Game AI by Example. Here's a method to help a autonomous character 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);

I just don't understand how could it get a speed by make a distance divided by a acceleration(deceleration)? Did I misunderstand anything?