Jump to content
  • Advertisement
Sign in to follow this  

(Solved) MDX - 2D sprite flip /mirror operations

This topic is 3855 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 hope this is the correct forum for my question. I'm trying to learn about making 2D sprite based games using Managed DirectX and C#. But I'm having trouble doing sprite flips and mirrors. (ie Making a sprite that's drawn facing right face left instead or upside down.) I first thought that the way to do this would be to multiply the scaling by -1 (I'll be scaling my sprites anyway). When I test this, the sprite is not drawn (my test code has a clear screen). However, when I scale the sprite by positive numbers it appears drawn and stretched correctly. Can someone please suggest a way of doing this with MDX, and better yet help me understand why my method doesn't work. Here's the relevant part of my sample code.
public Microsoft.DirectX.Direct3D.Device device;
public Microsoft.DirectX.Direct3D.Sprite sprite;
public Microsoft.DirectX.Direct3D.Texture texture;

public bool InitializeGraphics()
{
	PresentParameters presentParams = new PresentParameters();
	presentParams.Windowed = true;
	presentParams.SwapEffect = SwapEffect.Discard;
	
	device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
	
	string fname = "redarrow.bmp";
	texture = TextureLoader.FromFile(device, fname, 0, 0, 1, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, Color.FromArgb(0, 255, 0).ToArgb());
	sprite = new Sprite(device);
}

public void Render()
{
	if (device != null)
	{
		device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
		device.BeginScene();
		
		sprite.Begin(SpriteFlags.AlphaBlend);
		
		Matrix m = Matrix.Identity;
		m.Scale(-1, 2, 1);
		// AS AN ALTERNATIVE I TRIED A 180DEG ROTATION ON THE Z-AXIS - THIS PRODUCED A BLANK SCREEN TOO
		// m.RotateZ((float) Math.PI);
		sprite.Transform = m;
		sprite.Draw(texture, new Vector3(0, 0, 0), new Vector3(30, 30, 0), Color.White.ToArgb());
		
		sprite.End();
		device.EndScene();
		device.Present();
	}
}



[Edited by - Sabeltann on October 30, 2007 2:51:36 AM]

Share this post


Link to post
Share on other sites
Advertisement
Try changing:

sprite.Draw(texture, new Vector3(0, 0, 0), new Vector3(30, 30, 0), Color.White.ToArgb());


to:

sprite.Draw(texture, new Vector3(0, 0, 0), new Vector3(-30, 15, 0), Color.White.ToArgb());


Since your transform is setting x in the negative direction and doubling y, you need to specify a negative x and halve the y.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!