Unity plane constructor bug?
I ran into some issues with one of the constructors for planes in unity 5.3.2. It's easy to reproduce:
Vector3 target = new Vector3(2.6f, 4f, 3.5f);
Vector3 cameraPosition = new Vector3(10.5f, 14.3f, 12.4f);
Vector3 normal = target - cameraPosition;
float planarDistance = Vector3.Project(target, normal).magnitude;
Plane normalPlane = new Plane(normal, planarDistance);
Plane insanePlane = new Plane(normal, target);
Debug.Log("Correct planar distance: " + normalPlane.distance + ", Other, inexplicable distance: " + insanePlane.distance);
Which prints:
Correct planar distance: 5.901973, Other, inexplicable distance: 92.89
Am I misunderstanding how that constructor is supposed to work? How could the plane distance possibly be 93? That's significantly larger than all of the input numbers.
If I understand correctly, the 2 vector constructor should do exactly what I did with the projection.
From the docs: "inNormal must be a normalized vector."
I wonder why they don't normalize it? There are other constructors in unity that automatically normalize their inputs when that is required for validity.
Perhaps that constructor is very performance sensitive for some reason that isn't clear to me.
$$anonymous$$ost standard (non-Unity) math operations require a normalized input. The ones Unity normalizes are (what's a nicer word for dumbed down?) simplified for people without math backgrounds, and run slower because of it.
$$anonymous$$aking a plane in code is a standard, very mathy thing, so it makes sense to do it the real way. In other words, I knew the input had to be normalized before even looking it up (and I only $$anonymous$$ored in it.) That might seem elitist, but imagine you ask a math guy for help -- she's going to feel very comfortable with Unity's commands.
Answer by vegetablebread · Oct 02, 2016 at 08:05 PM
@owen-reynolds is correct. This test:
Vector3 target = new Vector3(2.6f, 4f, 3.5f);
Vector3 cameraPosition = new Vector3(10.5f, 14.3f, 12.4f);
Vector3 normal = (target - cameraPosition).normalized;
float planarDistance = Vector3.Project(target, normal).magnitude;
Plane normalPlane = new Plane(normal, planarDistance);
Plane insanePlane = new Plane(normal, target);
Debug.Log("Correct planar distance: " + normalPlane.distance + ", Other, inexplicable distance: " + insanePlane.distance);
Yields this result:
Correct planar distance: 5.901972, Other, inexplicable distance: 5.901973
Your answer
Follow this Question
Related Questions
how to calculate the angle between two vectors? 6 Answers
Convert 2D direction vectors to 3D vectors around sphere 2 Answers
Non physics determinism of floating point math across platforms 1 Answer
Vector Math - Linear Algebra 2 Answers
Get a Vector3 to always have a radius of 1 from 0, 0, 0 2 Answers