Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

red_sodium

Matrix setup

This topic is 5302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
always start with your translation matrix.. then apply rotation/scaling

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!