Reputation: 38825
I did a search on this, but didn't find a question that quite matched what I was after. I want the user to be able to define a textured plane. The parameters I have are:
So, I want to be able to calculate the 4 vertices of the rectangle given the above information. So, if I wanted a plane facing up, with a width and height of 1000:
So this would define a plane on the X & Z axis, facing upwards. What I do not understand is how to calculate the 4 corners in 3D space given this information. Do I need extra information, or is there a better way to arbitrarily specify a plane?
Edit : Current Code
In the following code:
Normal = 0, 1, 0
Vector3 arb = new Vector3(1, 1, 1);
Vector3 planeY = Vector3.Normalize(Vector3.Cross(Normal, arb));
Vector3 planeX = Vector3.Normalize(Vector3.Cross(Normal, planeY));
planeX *= Size.X / 2;
planeY *= Size.Y / 2;
Vector3[] ret = new Vector3[4]
{
(Center - planeX - planeY),
(Center - planeX + planeY),
(Center + planeX - planeY),
(Center + planeX + planeY)
};
Upvotes: 1
Views: 2550
Reputation: 7478
Your plane isn't fully defined yet. You need another vector going along the plane, the so called 'tangent' vector. In your above example, where should the Y-axis of the texture be pointing? Along the X-axis, along the Z-axis? Or maybe a completely different user defined axis? Your tangent vector is a vector that should point in the general direction where the X-axis of the plane should go.
Let's say we have a tangent vector as well, it doesn't neccesarilly need to point along the plane. You can construct the plane as follows:
Vector3[] vertices(Vector2 size, Vector3 center, Vector3 normal, Vector3 tangent)
{
Vector3 planeY = Vector3.Normalize(Vector3.Cross(normal, tangent));
Vector3 planeX = Vector3.Normalize(Vector3.Cross(normal, planeY));
planeX *= size.X / 2;
planeY *= size.Y / 2;
vertices = new Vector3[]
{
(center - planeX - planeY),
(center - planeX + planeY),
(center + planeX - planeY),
(center + planeX + planeY),
};
return vertices;
}
planeX and planeY are normalized vectors which point along X and Y axes of the plane itself. By multiplying these by size / 2, we get 2 vectors that span from the center to the edge of the plane in both X and Y directions. By adding these two together in different ways, we get the four corners.
Here's a diagram so you can get a better picture in your head. The tangent vector T is "flattened" onto the X-axis.
Upvotes: 1
Reputation: 3317
This is fine as a definition of a plane: you have a point and the normal vector. You need to get two vectors (A & B) on the plane and add one (A * one of the size values) to the origin to get the second corner. Add the second vector (B * the other size value) to get the third corner and add both vectors * their corresponding size values to the origin to get the forth corner.
To get the first vector, calculate the cross product of the normal vector (Direction) with an arbitrary vector (not equal to the direction). This will give you vector A. To get vector B calculate the cross product of A and the Direction.
Upvotes: 1