Nick Winters

I have the following matrix
1 0 0 0
0 -1 0 0
0 0 -1 0
0 1 0 1

and transform a zero vector by that. There is a transformation in the matrix, but I can't figure out how the math is done. Shouldn't this just be a 4x4 matrix multiplied by a 4x1 matrix For the fourth component of the vector, I tried just using 1. But when I multiplied them together by hand, I get a 0 vector. What am I missing

Thanks.

-Nick


Re: XNA Framework How does Vector3.Transform work?

ikanreed

What do you mean "By hand" In my head that comes out to be:
[0
0
0
1]

Since you're casting that vector4 to a vector3 I might surmise the final value is chopped.

I'm not sure what kind of transformation you're trying to do(it seems as though it might be the union of two different transformations) more detail on the inputs and desired outputs would help. As just an arbitrary collection of data I see no problems with it.




Re: XNA Framework How does Vector3.Transform work?

Jon Watte

Note:

- XNA and DirectX use ROW VECTORS ON THE LEFT.
- This means that translation lives on the bottom.
- Point transformations assume a "1" as the fourth vector element.
- Normal transformations assume a "0" as the fourth vector element.

In your example, a vector V (0, 0, 0, 1) times your matrix M will multiply as follows:

out.X = dot4(V, M.Col0) = 0;
out.Y = dot4(V, M.Col1) = 1;
out.Z = dot4(V, M.Col2) = 0;
out.W = dot4(V, M.Col3) = 1; // optional

The output result will be 0, 1, 0 (, 1)

Note that XNA quaternions multiply from right to left, whereas XNA matrices and vertices multiply from left to right. In DirectX, they all multiply from left to right.







Re: XNA Framework How does Vector3.Transform work?

Nick Winters

That makes sense. I'm currently attempting to do a hierarchy. I use a matrix to store translation relative to the parent, and a quaternion to store rotation relative to the parent.

public void SetGlobalMatrix(Matrix parent)
{
Matrix quat = Matrix.CreateFromQuaternion(m_orientation);
m_globalTransformation = parent * m_translation;
Matrix temp = m_globalTransformation * quat;
foreach(Item child in m_children)
{
child.SetGlobalMatrix(temp);
}
}

Is this the correct order For the translation matrix, I'm using Matrix.CreateTranslation, and for quaternions, here's my RotateZ (all others are cut, pasted and edited off of this)

public void RotateZ(float radians)
{
float halfsin = (float)Math.Sin(radians / 2.0f);
float halfcos = (float)Math.Cos(radians / 2.0f);
Quaternion quat = new Quaternion(0.0f, 0.0f, halfsin, halfcos);
m_orientation = Quaternion.Multiply(m_orientation, quat);
}

The current behavior I'm getting is that translations continue through to the children, but orientations do not. The orientation just works on the current scene item, and then its children do not translate by the local axis, they translate by the global axis.




Re: XNA Framework How does Vector3.Transform work?

Nick Winters

So turns out if I change it to

public void SetGlobalMatrix(Matrix parent)
{
Matrix quat = Matrix.CreateFromQuaternion(m_orientation);
m_globalTransformation = m_translation * parent;
Matrix temp = quat * m_globalTransformation;
foreach (Bone bone in m_children)
{
bone.SetGlobalMatrix(temp);
}
}

which is changing the order in which I multiply things, the rotation and translation are inherited by the children, but the translation is in the negative direction, and the rotation is the negative angle.