- Home /
Good old trig and NaN
Got a function that uses a lot of trig
private static Vector3 AdjustGravityToNormal(float velocity, Vector3 groundNormal) { if (groundNormal == Vector3.up) return Vector3.zero;
var angle = Mathf.Acos(groundNormal.y);
var magnitude = velocity * Mathf.Sin(angle);
var vMagnitude = magnitude * Mathf.Sin(angle);
var hMagnitude = -magnitude * Mathf.Cos(angle);
var horizontal = new Vector2(groundNormal.x, groundNormal.z).normalized * hMagnitude;
return new Vector3(horizontal.x, vMagnitude, horizontal.y);
}
It used to quite regularly return Vector3(NaN, NaN, NaN) when on a flat (Vector3 == Vector3.Up) even though it shouldn't.
Input position is { NaN, NaN, NaN }.
So I added the if (groundNormal == Vector3.up) return Vector3.zero; part and it got rid of most of the errors. But I still get the very rare occasional one and I'd like to get rid of it.
Any ideas?
Bonus Question which will probably solve original question so I'll leave it in same answer. Haven't really done vectors/trig in maybe 6 years so I'm a bit rusty. But does anyone know how to rewrite my function a lot cleaner? I'm pretty sure there are ways to do it using cross products etc. Pretty much the function is manually calculating the force in the x,y,z direction on a slope for gravity.
Answer by tertle · Mar 04, 2011 at 12:02 AM
Think I've solved this by adding
groundNormal = Vector3.ClampMagnitude(groundNormal, 1);
Up the top. Guess normal was returning a number marginally over 1 due to floating point error or something.
Regarding the numerical errors, you should probably clamp the input to Acos() to the range [-1, 1] rather than clamping the vector's magnitude. The latter might work in this case, but the former is the generally accepted method of ensuring that the input to Acos() is valid.
Your answer
Follow this Question
Related Questions
Changing the objects rather than time. 2 Answers
Walking on walls JS 2 Answers
Artificial Gravity 2 Answers
Firing projectile in curve 1 Answer