Drawing Texture2D with an angle

Note: the images below aren’t supposed to have whitespace. I am just trying to show you that i am trying to create a rectangle from a 1x64 image, positioned as shown in the second picture

I know how to rotate an image like that in Monogame

2

But how is it possible to draw a sprite in this angle this way ?

2

I can’t seem to be able to find an answer on that and i’m sort on ideas
This can help you understand why i am trying to do this, http://gamedev.stackexchange.com/questions/92489/lightning-bolt-effect-with-particles-is-not-continuous

What you are looking for is called a skew. SpriteBatch has no built-in support for it, but you can pass a transformation matrix containing your scew in SpriteBatch.Begin:

http://www.senocular.com/flash/tutorials/transformmatrix/

However, this is a rather ugly because you would have to start a new spritebatch for every sprite. I recommend drawing your primitives manually instead:

http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF

Thanks a lot :slight_smile: i will try this out :slight_smile:

Would it be inefficient if i started a new spritebatch for every sprite though ?

Yes. If you use spritebatch it is recommended to do as many things in a single run as possible.

This may seem like a stupid question, but I’ve been thinking about it the last few days. If you have 2 groups of things you want to render. Say for instance, if you need to overload the spriteBatch.begin() with something that will modify everything, while omitting a few items from that modify. The only way around this that I see is calling spriteBatch.Begin() again. Is there an alternative to using 2 spriteBatch calls in your draw method?

Depending on your parameter drawing the primitives manually might be better. Still, 2 batches are fine. For example if you use one batch to draw everything to a rendertarget then another batch to draw the content of the rendertarget to the backbuffer with an effect, then there is no better way to do it.

1 Like

I am sorry, I am not sure if I understood your answer. Let me explain what i meant. I am going to be using spriteBatch.Begin a lot because i will use this for something like a particle engine where i will draw 200 of those “skewed sprites” every frame. You wrote in your first reply: Quoting: "However, this is a rather ugly because you would have to start a new spritebatch for every sprite" By saying ugly do you imply that this would inefficient / non-optimal coding as well ?

Specify the skew (or shear) matrix in the call to SpriteBatch.Begin. Then have several calls to SpriteBatch.Draw each with a different position. Then call SpriteBatch.End.

However, with that many particles, you would still be better off using a DynamicVertexBuffer. It’s a bit more work, but will end up being faster overall. Do a web search for “XNA Dynamic Vertex Buffer sample” for many samples of how to use dynamic vertex buffers.

The problem with that is that the skew is created with Rand() method, so its unique for every position. Is it bad to make 100+ calls in SpriteBatch.Begin just to draw this animation performance-wise ?

I will definately search for “XNA Dynamic Vertex Buffer”, i haven’t heard it before.

It would kill your performance.