Matrix setup

Started by
5 comments, last by red_sodium 20 years ago
I am using DirectX's ID3DXSprite interface to render and translate 2D sprites on screen. So far that part of the code looks as follows:

    D3DXMatrixIdentity( &d3dMat );
    D3DXMatrixScaling( &d3dScalingMat, att.scaleX, att.scaleY, 1.0f );
    float radians = att.rotation*(PI/180);
    D3DXMatrixRotationZ( &d3dRotMat, radians );
    D3DXMatrixTranslation( &d3dTransMat, att.x, att.y, 0.0f );
    D3DXMatrixMultiply( &d3dMat, &d3dMat, &d3dRotMat );
    D3DXMatrixMultiply( &d3dMat, &d3dMat, &d3dScalingMat );
    D3DXMatrixMultiply( &d3dMat, &d3dMat, &d3dTransMat );
    pD3DXSprite->SetTransform( &d3dMat );

        D3DXVECTOR3 d3dCenterPos;
        d3dCenterPos.x = att.centerX;
        d3dCenterPos.y = att.centerY;
        d3dCenterPos.z = 0;
        if( FAILED( pD3DXSprite->Draw( pD3DTexture9, &renderRect, &d3dCenterPos, NULL, D3DCOLOR_ARGB( att.alpha, att.red, att.green, att.blue ) ) ) )
where the att struct contains my sprite's attributes. My knowledge of matrices is enough to know what they do but not enough to know how they work (except for that they are a 3x3 or 4x4 grid of numbers which can multiply by each other to combine or by a vector to transform that vector by the matrix). So far, this code has been fine for rotating, scaling and translating, but recently I need to transform a bunch of points (collision points, btw) by the same matrix as my sprite is translated by. The problem is that in the Draw() function I set the position to rotate from (centre of sprite), and when I translate my points by d3dMat, they do not transform the same as the sprite. How do I go about this? Do I need to translate d3dMat before I rotate, and simply set the 3rd parameter of Draw() to NULL? I am using the D3DX library function D3DXVec2TransformCoord(), is this the correct function to use to transform a vector by a given matrix? Thanks for the help! Stay Clausal, Red Sodium [edited by - red_sodium on April 11, 2004 11:24:37 AM] [edited by - red_sodium on April 11, 2004 11:25:20 AM] [edited by - red_sodium on April 11, 2004 11:26:16 AM]
"Learn as though you would never be able to master it,
hold it as though you would be in fear of losing it" - Confucius
Advertisement
always start with your translation matrix.. then apply rotation/scaling
But doesn't this rotate the sprite around the origin from where I have translated? In this case, if I translate by (100,100) the radius of rotation is well over 100.

EDIT: Just tried it, and yes it does, it sends my sprite on a huge orbit.

Stay Clausal,

Red Sodium

[edited by - red_sodium on April 11, 2004 11:23:43 AM]
"Learn as though you would never be able to master it,
hold it as though you would be in fear of losing it" - Confucius
Do you mean I should translate to origin, or do all my translation before rotation?

Stay Clausal,

Red Sodium
"Learn as though you would never be able to master it,
hold it as though you would be in fear of losing it" - Confucius
Could anyone explain to me in which order should i transform and why?

Stay Clausal,

Red Sodium
"Learn as though you would never be able to master it,
hold it as though you would be in fear of losing it" - Confucius
Um, the AP was completely backwards. Just think about transformations for a second.. if you translated an object a hundred units away from the origin, then applied a rotation matrix, the rotation makes the whole world revolve around the origin, so the object would rotate, but be a hundred units away in the wrong direction . So unless you''re aiming for that effect, you should always rotate first, then translate your objects. (Which works assuming your object starts at the origin.)
Thanks y''all, managed to get it working by translating centre of sprite to origin then rotating, then scaling, then translating (I gave solution just in case anyone else finds this when searching, etc )

Stay Clausal,

Red Sodium
"Learn as though you would never be able to master it,
hold it as though you would be in fear of losing it" - Confucius

This topic is closed to new replies.

Advertisement