libgdx / gdx-ai

Artificial Intelligence framework for games based on libGDX or not. Features: Steering Behaviors, Formation Motion, Pathfinding, Behavior Trees and Finite State Machines
Apache License 2.0
1.18k stars 241 forks source link

"Time to target" must not be set to 0 #112

Closed JackRainy closed 2 years ago

JackRainy commented 3 years ago

While playing with SteeringBehavior tests, I have noticed that some of the sliders allow to set the zero value for "Time to target". However, the behavior utilizes this value as targetVelocity.sub(owner.getLinearVelocity()).scl(1f / timeToTarget).limit(actualLimiter.getMaxLinearAcceleration()); where 0-value leads to division by zero and silent crash (in the tests the character just disappear and can't be restored).

I come with two options to fix that: 1) correct the behavior code: to perform a check for such wrong input, and when it happens to use some non-zero value. 2) correct the tests and do not allow to enter the wrong values.

This PR implements 2nd option. I suggest leaving for user's own responsibility providing the correct values to the behavior algorithms in one's code; rather than slowing down the algorithm by excessive sanity checks and providing the behavior different from the input (though invalid input) parameters.

tommyettinger commented 2 years ago

This is nice and simple, does the job it should, and only improves test quality, so I'll merge this.

JackRainy commented 2 years ago

@tommyettinger, thank you for the review. At some point I thought the project was already "dead" and not maintained; good to know it's still alive and I can contribute more. :)