D3DXMatrixRotationY and 2D sprites

This is a discussion on D3DXMatrixRotationY and 2D sprites within the Game Programming forums, part of the General Programming Boards category; I have loaded a sprite into memory like so: Code: HRESULT CDirectX::LoadSprite (TCHAR* szFilename, _Sprite* sprite) { HRESULT hr; // ...

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

    D3DXMatrixRotationY and 2D sprites

    I have loaded a sprite into memory like so:

    Code:
    HRESULT CDirectX::LoadSprite (TCHAR* szFilename, _Sprite* sprite) {
    
    	HRESULT hr;
    
    	// Load the texture from file
    	if(FAILED(hr = D3DXCreateTextureFromFileEx(_D3dDevice, szFilename,0,0,0,0,
                                            D3DFMT_UNKNOWN,   
                                            D3DPOOL_MANAGED, D3DX_DEFAULT,   
                                            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:

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

    I then render the sprite like this:

    Code:
    if(SUCCEEDED(marioSprite.Mario[marioSprite.currentMario].lpD3dSprite ->Begin(D3DXSPRITE_ALPHABLEND))) {
    			marioSprite.Mario[marioSprite.currentMario].lpD3dSprite ->SetTransform(&marioSprite.d3dMario);
    			marioSprite.Mario[marioSprite.currentMario].lpD3dSprite->Draw
                                                  (marioSprite.Mario[marioSprite.currentMario].lpD3dTexture , 
                                                  NULL, NULL, &D3DXVECTOR3
                                                 (marioSprite.xPos ,marioSprite.yPos , 
                                                  0.0f),D3DCOLOR_XRGB
                                                  (192,192,192));
    			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.

    [edit]

    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.

    [/edit]
    Last edited by andyhunter; 12-21-2004 at 05:49 PM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    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
    Location
    Göteborg, Sweden
    Posts
    2,072
    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?
    http://msdn.microsoft.com/archive/de...prite/Draw.asp
    http://msdn.microsoft.com/library/de...prite/draw.asp
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

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

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21