NewProger
NewProger

Reputation: 3075

XNA 4.0 Drawing rotated/distorted texture

Good day everyone. I hope you can help me with my question.

Is there a way to transform or directly render sprite like so?

example Without using 3d. I know it can be easily done in 3d, but the project I'm working on doesn't use 3d at all, so I don't really want to also include 3d just because of that small thing...

(example image is from some random game)

So basically what I need: Take a sprite as a rectangle, and then transform it in a free way, meaning that I can set points of that sprite to any coorditates, not just as rectangle.

Thanks in advance.

Upvotes: 1

Views: 1198

Answers (1)

Blau
Blau

Reputation: 5762

The technique used in that image is raycasting 2d. that was used first time with wolfenstein 3d, and implements a fake 3d over 2D.

here you can find a tutotial http://lodev.org/cgtutor/

though this is not what you want.

the best way to achieve what you want is define two triangles and use GraphicsDevice.DrawUserPrimitives with a basiceffect to draw it.

   // Init Triangles with four points A,B,C and D

   VertexPOsitionTexture[] Vertex = new VertexPositionTexture[6];
   Vertex[0].Position = (A.X,A.Y,0);
   Vertex[1].Position = (B.X,B.Y,0);
   Vertex[2].Position = (C.X,C.Y,0);
   Vertex[3].Position = (C.X,C.Y,0);
   Vertex[4].Position = (B.X,B.Y,0);
   Vertex[5].Position = (D.X,D.Y,0);

   Vertex[0].Texture= (0,0);
   Vertex[1].Texture= (1,0);
   Vertex[2].Texture= (0,1);
   Vertex[3].Texture= (0,1);
   Vertex[4].Texture= (1,0);
   Vertex[5].Texture= (1,1);

   // Init Effect from http://blogs.msdn.com/b/shawnhar/archive/2010/04/05/spritebatch-and-custom-shaders-in-xna-game-studio-4-0.aspx

   Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
   Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);


   BasicEffect effect = new BasicEffect();

   effect.Texture = your texture;
   effect.TextureEnabled = true;
   effect.World = Matrix.Identity;
   effect.View = Matrix.Identity;
   effect.Projection = halfPixelOffset * projection;

   // Draw Triangles
   effect.Apply():
   GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(vertex, TriangleList, ...,...);

this code have to be understand as pseudocode, it's not tested but it shows the relevant actions to be done.

Upvotes: 1

Related Questions