Rotation about local origin

Started by
20 comments, last by iedoc 12 years, 5 months ago
Hi

D3DXQUATERNION q;
D3DXMATRIX m;
D3DXMatrixIdentity(&m);
D3DXQuaternionRotationYawPitchRoll(&q, v.x, 0, 0);
D3DXMatrixTransformation(&m, NULL, NULL, NULL, NULL , &q, NULL);

This code snippet doesn't rotate the object around its local origin, appears to rotate about the world origin. Any help fixing it?
Thanks
Jack
Advertisement
D3DXMATRIX inv; D3DXMatrixInverse(&inv, NULL, &m_matWorld); D3DXMATRIX local; local = *m_pCombineFrameMatrix * inv; D3DXVECTOR3 v = m_pOperator->GetRot(); // The trolley facing direction is opposite to the man's walking direction v = -v; D3DXQUATERNION q; D3DXMATRIX m; D3DXMatrixIdentity(&m); D3DXQuaternionRotationYawPitchRoll(&q, v.x, 0 , 0); D3DXMatrixTransformation(&m, NULL, NULL, NULL, NULL , &q, NULL); local *= m; m_matWorld = m_matWorld * local; still not working! please help.. The trolley is just flying around. Why is this happening?

D3DXMATRIX inv;
D3DXMatrixInverse(&inv, NULL, &m_matWorld);

D3DXMATRIX local;
// trolley's combined transformation, child of the hand of the operator
local = *m_pCombineFrameMatrix * inv;

D3DXVECTOR3 v = m_pOperator->GetRot();
// The trolley facing direction is opposite to the man's walking direction
v = -v;
D3DXQUATERNION q;
D3DXMATRIX m;
D3DXMatrixIdentity(&m);
D3DXQuaternionRotationYawPitchRoll(&q, v.x, 0 , 0);
D3DXMatrixTransformation(&m, NULL, NULL, NULL, NULL , &q, NULL);

local *= m;
m_matWorld = m_matWorld * local;


Still not working. Please help.. The trolley is just flying around.
Why is this happening?
Jack
the way that you multiply your transformation matrices matter.

the reason it is rotating about the world origin, is because you are translating it first, then you are rotating, so your code probably looks like this

objectTransformMatrix = translation * rotation;

when it should be this

objectTransformMatrix = rotation * translation;
so in other words, it looks like your probably need to switch:

m_matWorld = m_matWorld * local

to

m_matWorld = local * m_matWorld
http://imageshack.us/photo/my-images/835/problemwyf.png/

http://www.gamedev.net/topic/613843-orientation-problem-on-object-attachment/

Tried it. Doesn't work. Do I have to change other parts of my code base?

Thanks for your help
Jack
http://imageshack.us/photo/my-images/835/problemwyf.png/

http://www.gamedev.net/topic/613843-orientation-problem-on-object-attachment/

Tried it. Doesn't work. Do I have to change other parts of my code base?

Also, unlike the sword and other weapon in any combat warrior games, I'd like to have the attached object to stay on the floor.
How can it be made possible?

Thanks for your help
Jack



// Make sure the trolley is on the floor, also keep it orthographical
m_matWorld._11 = m_fSize;
m_matWorld._12 = 0.0f;
m_matWorld._13 = 0.0f;
m_matWorld._14 = 0.0f;
m_matWorld._21 = 0.0f;
m_matWorld._22 = m_fSize;
m_matWorld._23 = 0.0f;
m_matWorld._24 = 0.0f;
m_matWorld._31 = 0.0f;
m_matWorld._32 = 0.0f;
m_matWorld._33 = m_fSize;
m_matWorld._34 = 0.0f;
m_matWorld._44 = 1.0f;

m_pCombineFrameMatrix->_11 = m_fSize;
m_pCombineFrameMatrix->_12 = 0.0f;
m_pCombineFrameMatrix->_13 = 0.0f;
m_pCombineFrameMatrix->_14 = 0.0f;
m_pCombineFrameMatrix->_21 = 0.0f;
m_pCombineFrameMatrix->_22 = m_fSize;
m_pCombineFrameMatrix->_23 = 0.0f;
m_pCombineFrameMatrix->_24 = 0.0f;
m_pCombineFrameMatrix->_31 = 0.0f;
m_pCombineFrameMatrix->_32 = 0.0f;
m_pCombineFrameMatrix->_33 = m_fSize;
m_pCombineFrameMatrix->_34 = 0.0f;
m_pCombineFrameMatrix->_44 = 1.0f;

D3DXMATRIX inv;
D3DXMatrixInverse(&inv, NULL, &m_matWorld);

D3DXMATRIX local;
// trolley's combined transformation, child of the hand of the operator
local = inv * *m_pCombineFrameMatrix;

D3DXVECTOR3 v = m_pOperator->GetRot();
// The trolley facing direction is opposite to the man's walking direction
v = -v;
D3DXQUATERNION q;
D3DXMATRIX m;
D3DXMatrixIdentity(&m);
D3DXQuaternionRotationYawPitchRoll(&q, v.x, 0 , 0);
D3DXMatrixTransformation(&m, NULL, NULL, NULL, NULL , &q, NULL);

local *= m;

// final output matrix
m_matWorld = local * m_matWorld;




Here are some changes using brute force.
The direction seems to be okay (not quite), the position is incorrect
Any further assistance is greatly appreciated
Thanks
I'm not sure what the goal is. However, here are some comments:

1. The shown code snippet let the positional transformation of m_matWorld as well as m_pCombineFrameMatrix undefined, although it is essential to know how translation is incorporated.

2. You should avoid incorporating scaling that way if possible. Scaling makes many things complicated.

3. The world matrix of a child node is computed as the product
world[sub]Child[/sub] := local[sub]Child[/sub] * world[sub]Parent[/sub]
(which may applied recursively if needed, e.g. a parent node may be a child of its own parent node).

4. If you want to use a local origin o for any transformation M, then shift the local origin temporarily to global 0:
T(-o) * M * T(o) =: M'
where T denotes a translation matrix from the specified argument.
What I seem to be seeing, is you are still transforming before you rotate. You should set "local" to "m" first, THEN multiply it with "[font="CourierNew, monospace"]inv [/font][color="#666600"][font="CourierNew, monospace"]* [/font][color="#666600"][font="CourierNew, monospace"]*[/font][font="CourierNew, monospace"]m_pCombineFrameMatrix".
[/font]
I'm not sure if this is the problem or not, but you should try it:

updated code

[color="#880000"]// Make sure the trolley is on the floor, also keep it orthographical[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_11 [color="#666600"]=[color="#000000"] m_fSize[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_12 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_13 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_14 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_21 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_22 [color="#666600"]=[color="#000000"] m_fSize[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_23 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_24 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_31 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_32 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_33 [color="#666600"]=[color="#000000"] m_fSize[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_34 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_matWorld[color="#666600"].[color="#000000"]_44 [color="#666600"]= [color="#006666"]1.0f[color="#666600"];[color="#000000"]

m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_11 [color="#666600"]=[color="#000000"] m_fSize[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_12 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_13 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_14 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_21 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_22 [color="#666600"]=[color="#000000"] m_fSize[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_23 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_24 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_31 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_32 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_33 [color="#666600"]=[color="#000000"] m_fSize[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_34 [color="#666600"]= [color="#006666"]0.0f[color="#666600"];[color="#000000"]
m_pCombineFrameMatrix[color="#666600"]->[color="#000000"]_44 [color="#666600"]= [color="#006666"]1.0f[color="#666600"];[color="#000000"]

D3DXMATRIX inv[color="#666600"];[color="#000000"]
D3DXMatrixInverse[color="#666600"](&[color="#000000"]inv[color="#666600"],[color="#000000"] NULL[color="#666600"], [color="#666600"]&[color="#000000"]m_matWorld[color="#666600"]);[color="#000000"]

D3DXMATRIX [color="#000088"]local[color="#666600"];
[color="#000000"] D3DXVECTOR3 v [color="#666600"]=[color="#000000"] m_pOperator[color="#666600"]->[color="#660066"]GetRot[color="#666600"](); [color="#880000"]// The trolley facing direction is opposite to the man's walking direction[color="#000000"]
v [color="#666600"]= [color="#666600"]-[color="#000000"]v[color="#666600"];[color="#000000"]
D3DXQUATERNION q[color="#666600"];[color="#000000"]
D3DXMATRIX m[color="#666600"];[color="#000000"]
D3DXMatrixIdentity[color="#666600"](&[color="#000000"]m[color="#666600"]);[color="#000000"]
D3DXQuaternionRotationYawPitchRoll[color="#666600"](&[color="#000000"]q[color="#666600"],[color="#000000"] v[color="#666600"].[color="#000000"]x[color="#666600"], [color="#006666"]0 [color="#666600"], [color="#006666"]0[color="#666600"]);[color="#000000"]
D3DXMatrixTransformation[color="#666600"](&[color="#000000"]m[color="#666600"],[color="#000000"] NULL[color="#666600"],[color="#000000"] NULL[color="#666600"],[color="#000000"] NULL[color="#666600"],[color="#000000"] NULL [color="#666600"], [color="#666600"]&[color="#000000"]q[color="#666600"],[color="#000000"] NULL[color="#666600"]);
[color="#000088"] local [color="#666600"]=[color="#000000"] m[color="#666600"];
[color="#666600"]
[color="#880000"]// trolley's combined transformation, child of the hand of the operator
[color="#000088"] local[color="#000000"] *[color="#666600"]=[color="#000000"] inv [color="#666600"]* [color="#666600"]*[color="#000000"]m_pCombineFrameMatrix[color="#666600"]; [color="#880000"]// final output matrix[color="#000000"]
m_matWorld [color="#666600"]= [color="#000088"]local [color="#666600"]*[color="#000000"] m_matWorld[color="#666600"];

This topic is closed to new replies.

Advertisement