- Home /
Projecting a point on plane
Hi I am making a mesh builder which needs me to get all the vertices of a cube. I want to make a crooked cube with sides not at 90 degree but parallel to the ground as a parallelogram. I am already using project on plane where it is needed but. I want to find out the point on a plane which is not a game object but in code without using raycast.
It is in unity 3d not in 2d
I would be very thankful for your answer!
Answer by Insok · Jul 09, 2020 at 10:02 PM
Because you put effort in your question and made a drawing, I put effort in giving a decent answer.
This problem converts to a math problem of finding the intersection of a plane (given by a point and a normal vector), and a line (given by a direction vector and a point).
• Let's call the first point of the line (point to object) P1 (x1, y1, z1) and the second point P2 (x2, y2, z2). (P2 is another random point on the line, for example: P1 + direction vector towards plane).
• Let's call the known point on the plane P3 (x3, y3, z3) and the normal vector N (nx, ny, nz).
• Let's call the needed point P (x, y, z)
We can write the line equation in vector form as:
(P2-P1) . k + P1 (Where k is a random number)
1. We know that the needed point is on this line, for a certain value of k.
x = k.(x2 - x1) + x1
y = k.(y2 - y1) + y1
z = k.(z2 - z1) + z1
We also know that the vector (P3-P) must be perpendicular to the normal vector N. (Since (P3-P) is a vector of the plane)
2. For perpendicular vectors applies that their dot product equals zero:
(x3 - x).nx + (y3 - y).ny + (z3 - z)*nz = 0
Applying these 2 conditions, we get 4 equations with 4 variables: x, y, z and k.
When we solve these equations, we get:
x = ( ((y3 - y2).ny + (z3 - z2).nz + nx.x3).x1 - x2.((y3 - y1).ny + (z3 - z1).nz + nx.x3) ) / ( (x1 - x2).nx + (y1 - y2).ny + (z1 - z2).nz )
y = ( ((x3 - x2).nx + (z3 - z2).nz + ny.y3).y1 - y2.((x3 - x1).nx + (z3 - z1).nz + ny.y3) ) / ( (y1 - y2).ny + (x1 - x2).nx + (z1 - z2).nz )
z = ( ((y3 - y2).ny + (x3 - x2).nx + nz.z3).z1 - z2.((y3 - y1).ny + (x3 - x1).nx + nz.z3) ) / ( (z1 - z2).nz + (y1 - y2).ny + (x1 - x2).nx )
k = ( (x1 - x3).nx + (y1 - y3).ny + (z1 - z3).nz ) / ( (x1 - x2).nx + (y1 - y2).ny + (z1 - z2).nz )
You can either calculate x, y and z separately or calculate k and use it with the line equation.
I hope you understood somewhat what I tried to explain, best of luck!
$$anonymous$$agnificent, It works flawlessly I truly appreciate your help. Thanks for your immense effort. Please take a look what I have created with your help. And thanks again. Flushed Game object to the surface. With 1,2 known points and 5,6 calculated.
I'm glad to hear that it works, I calculated it but didn't have the chance to test it. The result looks great!