Calculate the new coordinates of points after the plane of the points is transformed
Hi guys,
I have 4 points(p0, p1, p2, p3) in the same plane; I know the coordinates all of these.
Then the plane is translated and rotated,(not scaled); I know the new coordinates of p0, p1, p2; I don't know the coordinate of p3.
How can I calculate the new coordinate of p3?
What I current do, but not working:
Init status:
Vector3 initP0;
Vector3 initNormal;
Vector3 initP3;
void beforeTransform(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
initP0 = p0;
initNormal = Vector3.Cross(p1-p0, p2-p0);
initP3 = p3;
}The plane is translated and rotated,(not scaled)
Vector3 afterTransform(Vector3 p0, Vector3 p1, Vector3 p2)
{
Vector3 new_normal = Vector3.Cross(p1-p0, p2-p0);
Vector3 translation = p0-initP0;
Quaternion rotation = Quaternion.FromToRotation(normal, initNormal);
Matrix4x4 matrix4x4 = Matrix4x4.TRS(translation, rotation, Vector3.one);
return matrix4x4.MultiplyPoint3x4(initP3);
}
Thanks in advance.
Answer by Bunny83 · Dec 04, 2015 at 10:11 PM
Saving just the normal of the plane isn't enough. The normal and a point defines a plane, but such a plane doesn't have an orientation around the normal.
The easiest way is to save the position of the 4th point in relation to the other points.
One way is to express the point as a 2d vector within the plane. As unit vectors you would use the vectors p0->p1 and p0->p2.
Something like that:
public Vector2 pos;
void beforeTransform(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
Vector3 a = (p1 - p0);
Vector3 b = (p2 - p0);
Vector3 c = (p3 - p0);
pos = new Vector2(Vector3.Dot(a, c) / a.sqrMagnitude, Vector3.Dot(b, c) / b.sqrMagnitude);
}
Vector3 afterTransform(Vector3 p0, Vector3 p1, Vector3 p2)
{
Vector3 a = (p1 - p0);
Vector3 b = (p2 - p0);
return p0 + a * pos.x + b * pos.y;
}
Another way would be to calculate the barycentric coordinates of your 4th point in relation to the triangle that p0, p1, p2 define. To get new position you just need to use the barycentric coordinates with the new points.
In both cases the new point will also scale and even skew with the new points.
Thanks Bunny. The "barycentric coordinates" works.
Paste codes in case someone runs into the same problem.
Vector3 barycentric;
void beforeTransform(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
barycentric = GetBarycentric(p0,p1,p2,p3);
}
Vector3 afterTransform(Vector3 p0, Vector3 p1, Vector3 p2)
{
return barycentric.x * p0 + barycentric.y * p1 + barycentric.z * p2;
}
Vector3 GetBarycentric(Vector3 aV1, Vector3 aV2, Vector3 aV3, Vector3 aP)
{
Vector3 a = aV2 - aV3, b = aV1 - aV3, c = aP - aV3;
float aLen = a.x * a.x + a.y * a.y + a.z * a.z;
float bLen = b.x * b.x + b.y * b.y + b.z * b.z;
float ab = a.x * b.x + a.y * b.y + a.z * b.z;
float ca = a.x * c.x + a.y * c.y + a.z * c.z;
float cb = b.x * c.x + b.y * c.y + b.z * c.z;
float d = aLen * bLen - ab * ab;
Vector3 B = new Vector3((aLen * cb - ab * ca), (bLen * ca - ab * cb)) /d;
B.z = 1 - B.x - B.y;
return B;
}
Your answer
Follow this Question
Related Questions
i am using a code to make my cube walk with character controller ,but when i play the cube spins. 0 Answers
Rotation problem 0 Answers
transform rotation inaccuracy 0 Answers
Creating a camera controller for isometric view - Positioning the camera 1 Answer
How can I make an object transform to given position and rotation 2 Answers