- Home /
Find point in 3D plane
I have four points in a 3D space, example:
(0,0,1)
(1,0,1)
(1,0,2)
(0,0,2)
Then I have a 2D position on that square plane:
x = 0.5
y = 0.5
I need to find out the 3D space point of that position in the plane. In this example it's easy: (0.5,0,1.5) Because Y is zero. But imagine that Y was not zero (and not all the same), that the plane is leaning in some direction. How would I calculate the point in that case?
I imagine this should be a pretty easy thing to solve, but I can't figure it out. Please answer in programming terms and not in straight math terms.
Update with image: The gray plane (made out of two triangles) are the real one actually existing. I create a non-existing plane on top of this, the ABCD corners are exactly the same, however it doesn't slope. What I need to do is project a pixel (blue one in example) from the non-existing plane to the existing plane. It will be in the exact same location, except that it has gained a Y value from the sloping plane.
What I've been able to work out so far on my own is which one of the two triangles to use in the gray plane and the normal of that triangle. I basically just need to figure out how I can project the pixel.
Saw you'd mentioned the word 'project'.
http://docs.unity3d.com/ScriptReference/Vector3.Project.html
Answer by Hotdug · Jul 04, 2014 at 05:29 PM
Figured it out mostly thanks to http://gamedeveloperjourney.blogspot.com/2009/04/point-plane-collision-detection.html
Made me realize I had to verify the normal a bit closer, turns out my plane's grid was being rendered a little different than the actual coordinates for the verticles. No wonder this was so hard to get right! The pixel was projected correctly but rendered incorrectly.
Thanks for all the help guys, that blogspot entry can't take all the credits. You helped me too.
Sorry, but that's not a valid answer since it doesn't answer the question at all.
Answer by Benproductions1 · Jul 04, 2014 at 03:58 AM
OK, lets say we have a plane made of 3 points (4 points are not guaranteed to be planar), lets call them: A, B and C.
Now we want to calculate a point on the plane, given two of the points coordinates.
To find the other coordinate we can do a simple line-plane intersection test, where the line goes along the missing axis. Say we have X and Y, but want the Z coordinate, out line would be:
(x, y, z) = (X, Y, 0) + t(0, 0, 1)
Lets call then:
P = O + tD
Now that we have a line to intersect with, we just need to do the intersection test. The first step is to calculate the normal, N:
N = AC × AB
t
is then given by the formula:
t = - (OA · N) / (N · D)
Since t
is multiplied by a direction along the missing axis, it is equivalent to the value of your missing axis. The resulting point is therefore given by (using the same example):
(X, Y, t)
Obviously you can adapt this to find any axis, but implementation is something you should do yourself.
This is really just standard line-plane intersection. If you don't understand any of the maths involved, I suggest you learn some linear algebra (Wikipedia), more specifically algebra involving vectors, lines and planes.
Is it possible to get that in a program$$anonymous$$g example? I don't really understand that otherwise. What confuses me the most is "now we want to calculate a point on the plane, given two of the points coordinates", surely I would need to use all three points of the plane to calculate the fourth point that I need? If the answer was put in program$$anonymous$$g terms (an example) maybe I would understand better what you mean.
Since it's a very special case it doesn't actually need any fancy math. All you need is to cross multiply. From the prcture it's not clear if AB is the x or the z axis.
If "AB" is the x axis all you need is:
//C#
Vector3 Project(Vector3 aPos, Vector3 A, Vector3 DTop, Vector3 D)
{
float ratio = (D.y - DTop.y) / (A.z - DTop.z);
return new Vector3(aPos.x, (aPos.z - A.z)*ratio, aPos.z);
}
If "AB" is the z axis all you need is:
//C#
Vector3 Project(Vector3 aPos, Vector3 A, Vector3 DTop, Vector3 D)
{
float ratio = (D.y - DTop.y) / (A.x - DTop.x);
return new Vector3(aPos.x, (aPos.x - A.x)*ratio, aPos.z);
}
Of course if one or more of the A / D / DTop components are 0 the formula can be simplified a bit. Anyways we actually just need to calculating the slope. Since x and z stay the same we just need to calculate y based on the position in the direction of the slope. It's actually a simple 2D problem.
Answer by robertbu · Jul 04, 2014 at 02:47 PM
Not completely sure of the criteria. I believe you want to project the point onto the plane:
function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 {
planeNormal.Normalize();
var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
return point + planeNormal * distance;
}
If that's not what you want, note that Unity's mathematical Plane class has a Raycast() function. Also you might want to take a look at the Math3d class in the Unity wiki.
Your answer
Follow this Question
Related Questions
GUI Coordinate System 0 Answers
3D. How to get 1 pixel to equal 1 degree camera FOV 0 Answers
3d drawing based on movable planes? 0 Answers
Retrieve 2d primitives from 3d colliders 1 Answer
Pixel values of a plane (RectTransform)? 0 Answers