Thread: D3DXMatrixRotationY and 2D sprites

  1. #1
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004

    D3DXMatrixRotationY and 2D sprites

    I have loaded a sprite into memory like so:

    HRESULT CDirectX::LoadSprite (TCHAR* szFilename, _Sprite* sprite) {
    	HRESULT hr;
    	// Load the texture from file
    	if(FAILED(hr = D3DXCreateTextureFromFileEx(_D3dDevice, szFilename,0,0,0,0,
                                            D3DPOOL_MANAGED, D3DX_DEFAULT,   
                                            D3DCOLOR_XRGB(192,192,192), NULL,  
                                            NULL, &(sprite- >lpD3dTexture))))
                            return hr;
    	if(FAILED(hr = D3DXCreateSprite(_D3dDevice, &(sprite->lpD3dSprite)   )))
    		return hr;
    	return S_OK;
    I have then attempted to rotate the sprite like this:

    D3DXMatrixRotationY(&marioSprite.d3dMario , radian);
    where d3dMario is a D3DXMATRIX and radian is just a float.

    I then render the sprite like this:

    if(SUCCEEDED(marioSprite.Mario[marioSprite.currentMario].lpD3dSprite ->Begin(D3DXSPRITE_ALPHABLEND))) {
    			marioSprite.Mario[marioSprite.currentMario].lpD3dSprite ->SetTransform(&marioSprite.d3dMario);
                                                  (marioSprite.Mario[marioSprite.currentMario].lpD3dTexture , 
                                                  NULL, NULL, &D3DXVECTOR3
                                                 (marioSprite.xPos ,marioSprite.yPos , 
    			marioSprite.Mario[marioSprite.currentMario].lpD3dSprite ->End ();

    Unfortunately when I draw the sprite it disappears off the screen!!!!!!! I know you can rotate primatives around the y-axis. Am I simply just barking up the wrong tree here trying to do it with 2D sprites?

    BTW the code works fine without the attempt at rotating it, unfortunately the sprite only faces forward. To add the animation I simply just toggled between pictures forcing the sprite to appear to walk, hence all the variable mumbo jumbo.

    If it can't be done I guess I will simply just add more pictures to toggle between. I was just trying not to do that.


    well I decided to do the add more pictures thing, that way I could keep messing around with this whole DirectX deal. Still, any information will be greatly accepted. Merry Christmas and Happy New Year everybody.

    Last edited by andyhunter; 12-21-2004 at 06:49 PM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Well are you translating it and then rotating it? You must rotate, scale, and then translate for all of those to be relative to local/model space. Then you can rotate,scale, etc., in world space.

    But if you alter the position of the vertexes prior to rotating, they will NOT rotate around the sprite center because you have effectively moved the sprite center to the origin of the world....thus the sprite will rotate around the world origin, not the local model origin.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Göteborg, Sweden
    When I worked with sprites you didn't have to use matrices at all to draw, rotate and scale sprites for 2D games.

    When did they change this?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    As of DirectX 9.0

Popular pages Recent additions subscribe to a feed