All Questions


Rabbit One

About Steering.flee_from() function

How flee_from() function should be coded? I know that it's a challenge but I tried to work on this function for a while and I just don't arrive to a solution.

Could you help me by giving me a clue on how to think this? Thanks!

  • Nathan Lovato replied

    I assume you're talking about the challenge, having the leader fade behaviors between fleeing and following the mouse cursor?

    If so, you want to calculate both a flee and an arrive velocity separately using the Steering.flee_from and Steering.arrive_to functions.

    Then, you calculate a weight for the two behaviors based on some maximum distance past which you follow the target position instead of fleeing. A weight of 0 could represent fleeing completely, and a weight of 1 using only the arrive behavior.

    Here's how I calculated the weight in the final assignment's source code:

    var weight: float = clamp(max(to_target - FLEE_THRESHOLD, 0.0) / BEHAVIOR_TRANSITION_RADIUS, 0.0, 1.0)

    I then used the Vector2 class's linear_interpolate method to interpolate between the two:

    _velocity = flee_velocity.linear_interpolate(arrive_velocity, weight)