Look rotation viewing vector is zero (618173)

Hi!

before creating this thread, I have already searched all over.

I have a boid algorithm , there are functions to make turns and propelling, but sometimes they need to rotate just a tiny amount and that’s when the problem occurs.

As I have a populations of 25 groups with 15 individuals each (375) each second more than 999 log messages are thrown and more than 2.5Mb of garbage are generated.

First thing:
Would be nice if we could catch those “errors” (as it is not really an error) despite of just logging it without giving us the chance of correct it.

Second:
without correcting that, my code tries to apply the rotation and the real errors appears:

  • CompareApproximately (det, 1.0F, .005f);
  • fRoot >= Vector3f::epsilon;
  • rigidbody.rotation assign attempt for ‘XXXX’ is not valid. Input rotation is { NaN, NaN, NaN, NaN }.

I have already tried to avoid next to zero angles, next to zero magnitudes, but It all stops those small movement corrections that makes the individuals look like alive inside the flock and just make them move straight ahead forever.

There’s no need to put any code in here, the logic is:

  • Each frame theres a resultant vector that adds up each behaviour function for each individual rotating it towards the resultant over time.

That error happens when you call Quaternion.LookRotation on Vector3(0,0,0). The way to catch that is to check the Vector3 before you call that.

You don’t need to avoid “near zero”, only “actually zero”. That should preserve your little movements.

I’m not sure this has ever been true on a post in this forum.

I found that the magnitude of the vector that is causing this error is NaN.
Now I’m looking for a way to filter it.

I did filter the vector causing the problem, checking if it was NaN (float.IsNan), but those vectors that are not NaN still make the errors occurs:

  • CompareApproximately (det, 1.0F, .005f);
  • fRoot >= Vector3f::epsilon;
  • rigidbody.rotation assign attempt for ‘XXXX’ is not valid. Input rotation is { NaN, NaN, NaN, NaN }.

Thank you StarManta!
As You told me, I filtered Vector3.zero, the errors are gone, most of the individuals started to act as expected, but some are still not making any turn, but I think there’s nothing to do with this question.