Jump to content
  • Advertisement
Sign in to follow this  
Sean_Seanston

Textured Quad rotating around screen centre

This topic is 3546 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've just implemented the transformation matrices method seen in Issue 3 down the page on this article: http://www.gamedev.net/reference/articles/article1972.asp Problem is... it only rotates around the screen's centre instead of the quad itself. The example code is the same except that the quad is already centred on the screen so if you rotate it, it rotates around it's own centre but only because the screen's centre is the same. I've looked around the code pretty closely... and messed with some values but I can't see anywhere that seems to indicate the point to pivot around or anything like that. Does that require some completely new code or what? Also, I'm having a lot of trouble understanding something about the coordinates in the article. He has a Blit function that starts off like this:
void Blit(IDirect3DTexture9* texture, RECT* rDest, float rotate)
{
    float X;
    float Y;
    D3DXMATRIX matTranslation;
    D3DXMATRIX matScaling;
    D3DXMATRIX matTransform;
    
    //Get coordinates
    X = rDest->left - (float)(RESOLUTION_WIDTH) / 2;
    Y = -rDest->top + (float)(RESOLUTION_HEIGHT) / 2; 

    //Setup translation and scaling matrices
    D3DXMatrixScaling (&matScaling, (float)(rDest->right - rDest->left),
        (float)(rDest->bottom - rDest->top), 1.0f);
    D3DXMatrixTranslation (&matTranslation, X, Y, 0.0f);
    matTransform = matScaling * matTranslation;
The "rDest" that's passed in just has the screen coordinates of the top, bottom, left and right of the texture BTW. What I can't figure out is: - Why is it necessary to subtract half the screen width from the x coordinate and do something similar to the y? Sure enough, if you don't do it, the texture ends up drawn half a screen too far over to the left etc. but I can't see why. - Why is Y set to -rDest->top instead of rDest->top? Maybe I need to know more about how D3DXMatrixTranslation() works? Also, I'm coming to D3D from SDL and I've seen stuff about the left-handed coordinate system D3D uses. Does that mean that (0,0) on the screen is actually the bottom left? It's just that the way the rDest rectangle is set up to draw the image in a certain position seems to contradict that. But then the Y uses the negative value of rDest->top so I'm quite confused :/

Share this post


Link to post
Share on other sites
Advertisement
To rotate about the center of the quad you must first translate it to the origin(so that its center is at the origin), rotate it and then translate it back, something like this:


D3DXMATRIX t1, r, t2, final;
D3DXMatrixTranslation(&t1, -obj.x, -obj.y, 0.f);
D3DXMatrixRotationZ(&r, angle);
D3DXMatrixTranslation(&t2, obj.x, obj.y, 0.f);
final = t1*r*t2;


In that code in your post, I suppose he is working with screen coordinates (origin at the top left corner of the screen, x points to the left, y points to the bottom and coordinates are proportional to pixels). Then, he subtracts/sums half the screen dimensions to get coordinates in a translated coordinate system which has the origin at the center of the screen, x points to the left and y points to the top.

Well, don't know if I answered your question really..

Share this post


Link to post
Share on other sites
http://www.toymaker.info/Games/html/matrices.html

There is object space and world space, read the above link and it may help you understand why translating the object to the origin works.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!