• Advertisement
Sign in to follow this  

"D3DXMatrixTransformation2D": Rotation Not In Degrees?

This topic is 1539 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

Hi,

 

I am designing a 2D game engine using DirectX 9.0c.

Currently I am working on graphics core.

 

My question now is the following:

"D3DXMatrixTransformation2D" rotation function perimeter does not seem to be in degrees?

How do I change it into degrees? (0'-360')

(see source code below)

 

Thanks!

 

JeZ+Lee

 

Here is the source:

//-------------------------------------------------------------------------------------------------------------------------------
void render( void )
{
    float x = 400; // Test value
    float y = 240; // Test value

    DXDevice->BeginScene();

    DXDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );

    Sprite->Begin(D3DXSPRITE_ALPHABLEND);

    D3DSURFACE_DESC textureInfo;
    SpriteTexture->GetLevelDesc(0, &textureInfo);
    float spriteWidth = (float)textureInfo.Width;
    float spriteHeight = (float)textureInfo.Height;
    RECT spriteRect;
    spriteRect.top = 0;
    spriteRect.left = 0;
    spriteRect.bottom = textureInfo.Height;
    spriteRect.right = textureInfo.Width;

    D3DXVECTOR3 spritePosition;
    spritePosition.x = 0.0f - (spriteWidth/2);
    spritePosition.y = 0.0f - (spriteHeight/2);
    spritePosition.z = 0.0f;

    D3DXMATRIX matrixSpriteTransform;

//    D3DXVECTOR2 spriteCentre = D3DXVECTOR2(spriteWidth/2, spriteHeight/2);

    D3DXVECTOR2 screenPostion = D3DXVECTOR2(x, y);

    float rotation = 0.0f; //<--- How to change to degrees ???

    D3DXVECTOR2 scaling(1.0f, 1.0f);

    D3DXMatrixTransformation2D(&matrixSpriteTransform, NULL, 0.0, &scaling, /*&spriteCentre*/NULL, rotation, &screenPostion);
    Sprite->SetTransform(&matrixSpriteTransform);

    Sprite->Draw( SpriteTexture, &spriteRect, NULL, &spritePosition, D3DCOLOR_RGBA(255,255,255,255) );

    Sprite->End();

    DXDevice->EndScene();
    DXDevice->Present( NULL, NULL, NULL, NULL );
}

//-------------------------------------------------------------------------------------------------------------------------------

Share this post


Link to post
Share on other sites
Advertisement

No, it's not in degrees. You don't change the functions, you need to convert your degress to radians

#define _USE_MATH_DEFINES // define before math.h
#include <math.h>

float ToRadians(float Degrees) 
{
       return Degrees * M_PI / 180.0f; // M_PI is from math.h
} 

Share this post


Link to post
Share on other sites

D3DXMatrixTransformation2D expects angle in radians, you could convert your "degree" variable to radians with D3DXToRadian
 

float rotationInDegrees = 90.0f;
float rotationInRadians = D3DXToRadian( rotationInDegrees );
D3DXMatrixTransformation2D(......., rotationInRadians, ...);

I would avoid evil math.h header and i heard that M_PI is rotten too.

Share this post


Link to post
Share on other sites

#define _USE_MATH_DEFINES // define before math.h
#include <math.h>

float ToRadians(float Degrees) 
{
       return Degrees * M_PI / 180.0f; // M_PI is from math.h
} 

Just a head’s up, you should save yourself from a costly division by writing it as:
return (M_PI / 180.0f) * Degrees;


L. Spiro Edited by L. Spiro

Share this post


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

  • Advertisement