Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The matrix code in my directX model from which I am creating the plane below looks like this;

FrameTransformMatrix {
  1.0, 0.0, 0.0, 0.0,
  0.0, 1.0, 0.0, 0.0,
  0.0, 0.0, 1.0, 0.0,
  2750.0, 0.000163, 0.000143, 1.0;;
}


In C# I can create a plane from existing points in the vertex buffer.

C#
Vector3[] vectors = new Vector3[mesh.MeshParts[0].VertexBuffer.VertexCount];
mesh.MeshParts[0].VertexBuffer.GetData<Vector3>(vectors);
Plane plane = new Plane(vectors[mesh.MeshParts[0].StartIndex], vectors[mesh.MeshParts[0].StartIndex + 1], vectors[mesh.MeshParts[0].StartIndex + 2]);


Now, with this information, in the effect.World portion of my render code, how would I render an object with all 3 axes aligned to and touching the plane? Assume the object to be placed has a base centered at Vector3.Zero.

Can the task be accomplished with the existing Matrix functions in C# and XNA or will the math need to be done elsewhere?
Posted
Updated 15-Nov-14 10:04am
v6
Comments
gggustafson 15-Nov-14 13:57pm    
I've added XNA and directX to your tags.
Sergey Alexandrovich Kryukov 15-Nov-14 19:32pm    
Not clear what plain you want to choose and what is your problem. It's pretty apparent that a plane can be defined, for example, by 3 points. So, where are those 3 points? A transformation matrix does not define a geometry object, it just defines the transformation of the space or coordinate system.
—SA
paulrm 15-Nov-14 21:58pm    
The 3 points are defined within the model and are transformed by the matrix given. They could be any 3 points from a mesh within the model. The plane is arbitrary. It can and will change.

I want to align the 3 axes, of an object with a base centered at Vector3.Zero, to the 3 axes of the plane.

The answer will probably be something like;

Matrix rotation = Matrix.CreateRotationX((float)Math.Acos(Vector3.Dot(plane.Normal, Vector3.Right))) * Matrix.CreateRotationY((float)Math.Acos(Vector3.Dot(plane.Normal, Vector3.Up))) * Matrix.CreateRotationZ((float)Math.Acos(Vector3.Dot(plane.Normal, Vector3.Forward)));
paulrm 16-Nov-14 19:00pm    
I was pretty sure the above matrix would be close, but it does not work. The translation is correct, but the rotation is not.

Here's my effect.World line from the Draw function;

effect.World = (absoluteTransforms[mesh.ParentBone.Index] * rotation) * translation * camera.World;

Any help will be greatly appreciated.

Andreas Gieriet 16-Nov-14 19:22pm    
What means "does not work"? What is the effect? Your FrameTransformMatrix is a simple dx/dy/dz shift. Please note that matrix calculations are not commutative. I.e. A * B != B * A. So, the sequence of matrix multiplications does matter.
Cheers
Andi
PS: Sorry for my ignorance: your initial question is about the angle of the plane. Angle with reference to what?

1 solution

Given the plane vector [n] at a given point [p], and you have an object model at the absolute origin, and you want to render the object at point [p] aligned with the plane, then you must find the following transformation:

  1. T1 = rotate the absolute x-z plane around the absolute y-axis by the [n]-angle in the plane through absolute z-axis (i.e. angle of [n] to the projection of [n] to the absolute x-y plane = acos()).
  2. T2 = rotate the absolute x-y plane around the absolute z-axis by the [n]-angle in the absolute x-y plane (i.e. the angle in the absolute x-y plane from the [n] projection above to the absolute x-axis).
  3. T3 = translate to point [p]

This transformation sequence transforms your object to the plane's point [p] and its aligned axes.
Once you found these transformation T1, T2, T3, you multiply the transformed Tx as follows:
T = (T1T x T2T x T3T)T

(MT is the transposed matrix of M, i.e. mirrored at the diagonal top-left to bottom-right)

With that T transformation matrix, you multiply each object's point and so get the object rendered at the desired location.
E.g. vplane = T x vorigin

Given
[n] = [nx, ny, nz, 1]T
[p] = [px, py, pz, 1]T
q = sqrt(nx2 + ny2)
r = sqrt(nx2 + ny2 + nz2)

T1 =
nz/r,0,q/r,0
0,1,0,0
-q/r,0,nz/r,0
0,0,0,1


T2 =
nx/q,-nyq,0,0
ny/q,nx/q,0,0
0,0,1,0
0,0,0,1


T3 =
1,0,0,px
0,1,0,py
0,0,1,pz
0,0,0,1


I leave the calculation of the T-matrix as exercise ;-)

Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900