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

Handle NaN case when relative velocity is 0 #55

Closed domokato closed 8 years ago

domokato commented 8 years ago

From these two lines:

float relativeSpeed2 = relativeVelocity.len2();
float timeToCollision = -relativePosition.dot(relativeVelocity) / relativeSpeed2;

0/0 = NaN

davebaol commented 8 years ago

If I got it right this can only happen when both relative position and relative velocity are 0, i.e. when the two agents have the same position and velocity. If so, shouldn't we return true ?

domokato commented 8 years ago

Anything dot (0,0) will evaluate to 0, so it doesn't matter what relative position is

Although I suppose if the agents are already colliding we will want to return true as you said...how would you suggest we fix this?

davebaol commented 8 years ago

Well, this behavior is collision avoidance, meaning that its target is to prevent collisions by reacting to collision course not to ongoing collisions. Also when you use a physics engine rigid bodies won't overlap, so the issue does not arise. As things stand, I think that your solution is more than acceptable. I've just replaced the isNaN() invocation with the faster check relativeSpeed2 == 0 which is logically equivalent in this particular situation.