1

I'm working on implementing steering behaviors in an OpenGL Android project of mine, but I'm having a little trouble understanding why my Arrive behavior code isn't behaving like it should.

During the update loop my program calculates a steering force with which to update the position of a mobile agent. Currently, it behaves identically to the "Seek" behavior, in that it moves to the position defined in Targetpos, but instead of slowing down and stopping when it nears the point like it should, it keeps moving forward and overshoots the target over and over again.

Any help on this would be greatly appreciated, below is the code that's supposed to return the proper steering force for the agent.

Deceleration is simply an enum of 1-3 encoding different rates at which the agent should slow.

private Vector2 Arrive(Vector2 Targetpos, Deceleration deceleration) {
        Vector2 ToTarget = sub(Targetpos, agentPos);

        //calculate the distance to the target
        float dist = ToTarget.len();

        if (dist > 0.2f) {

            final float DecelerationTweaker = 0.3f;

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

            //make sure the velocity does not exceed the max
            speed = Math.min(speed, agentMaxSpeed);

            Vector2 DesiredVelocity = mul(ToTarget, speed / dist);

            return sub(DesiredVelocity, agentVelocity);
        }

        return new Vector2(0, 0);
    }
ziggystar
  • 28,410
  • 9
  • 72
  • 124
RGrun
  • 350
  • 3
  • 16
  • Starting at a certain distance with a certain speed and using a given deceleration results in a deterministic behaviour all the way to standstill, either before or after the target. You have to *calculate* the deceleration to arrive exactly at a certain point. – laune Jan 15 '15 at 07:15

0 Answers0