Jump to content

  • Log In with Google      Sign In   
  • Create Account


riku743

Member Since 22 Mar 2013
Offline Last Active Mar 23 2013 01:05 PM
-----

Topics I've Started

DirectX: Unable to Flip Sprite Horizontally

22 March 2013 - 03:10 PM

Hi,

 

I've been working on a 2D game using DirectX, and it has been going pretty smoothly. I have a structure set up to contain data for a sprite, including its position and the actual sprite object (lpSprite) as well as some variables dealing with movement speed and etc. There is a function I've made to draw a given sprite with a given texture, and it works well. Until recently, I had two separate textures for one of the sprites, with each texture facing a different direction, and it would draw the sprite with one of the textures depending on which direction it was facing. This worked fine, but I didn't want to have two textures for each as I started adding more sprites to the game, so I started looking into how to flip a sprite horizontally using SetTransform. Below is a snippet of code taken from my function that draws the sprite.

	D3DXVECTOR3 vCenter( 0.0f, 0.0f, 0.0f );
	D3DXVECTOR3 vPosition( s_sprite.xPosit, s_sprite.yPosit, 0.0f );
																			
	s_sprite.lpSprite->Begin( D3DXSPRITE_ALPHABLEND );

	if(s_sprite.flipHoriz)
	{
		D3DXMATRIX transMatrix;
		D3DXMatrixScaling(&transMatrix, 1.0f, -1.0f, 1.0f );
		s_sprite.lpSprite->SetTransform(&transMatrix);
	}


    s_sprite.lpSprite->Draw( t_texture.lpTexture,
                          &srcRect,
                          &vCenter,
                          &vPosition,
                          D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f) );

 

The part that I added recently is the if(s_sprite.flipHoriz), which is a bool, and the three lines inside the if statement. Now, after adjusting the code so that when the sprite should be facing left flipHoriz is true, when I run the program and move the sprite to be facing left, it is no longer visible. Its position remains at what it should be, and the program doesn't stop running, but you can't see it anymore. When I move it back to be facing right, it still is not visible.

 

What did I do wrong? After looking around on the internet, it seems that my sprite's center being at 0,0 is an oddity and this may be part of the problem, but up until this point, it worked fine. Also, I tried setting the center to the actual center of the sprite, but the same problem persisted (and all of the sprites were drawn in the slightly wrong place).


PARTNERS