Rotate 2D texture XNA - c#

I'm trying to rotate my shot Texture by 180 degress, but when I do the following:
spriteBatch.Draw(TexTiro, Position, null, Color.White, (float)(180), new Vector2(), Vector2.One, SpriteEffects.None, 0f);
the texture appears like this:
What am I doing wrong?

It is because the rotation is in radians.
You can use MathHelper.ToRadians() to easily convert degrees to radians: see here
spriteBatch.Draw(TexTiro, Position, null, Color.White, MathHelper.ToRadians(180), new Vector2(), Vector2.One, SpriteEffects.None, 0f);

In XNA the unit is radians not degrees. so 180 would be (float)Math.PI

Related

Mouse position to World Position. And Mouse position to Screen Re-scaled World Position

So I am using a Camera(View Matrix) to move a tile world. In XNA(MONOGAME)
I am getting the raw mouse position with this code:
MouseState ms = Mouse.GetState();
Vector2 mousePosition = new Vector2(ms.X, ms.Y);
Now If I divide the X by TILE_WIDTH and the Y by TILE_WIDTH it gives me the tile which I can grab out of my array. But once I move the matrix it offsets. How can I add the world offset to my mouse position?
There is also another problem. When I re-size my window. The mouse will offset even more. Anyway of fixing this. So it will work in full-screen windowed in any resolution the mouse can convert to worldPosition?
My Render:
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, c2d.get_transformation(GraphicsDevice));
spriteBatch.End();
My view matrix:
public Matrix get_transformation(GraphicsDevice graphicsDevice)
{
_transform = // Thanks to o KB o for this solution
Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
Matrix.CreateTranslation(new Vector3(graphicsDevice.Viewport.Width * 0.5f,
graphicsDevice.Viewport.Height * 0.5f, 0));
return _transform;
}
My Way Of Getting The Tile:
tile[(int)worldPosition.Y / TILE_SIZE, (int)worldPosition.X / TILE_SIZE].color = Color.Red;
Things I have Tried:
Vector2 worldPosition = Vector2.Transform(mousePosition, Matrix.Invert(viewMatrix));
(https://gamedev.stackexchange.com/questions/21681/how-to-get-mouse-position-relative-to-the-map)
TL;DR
How do I get my mouse position in world view even if I re-size my window move my camera2D, Zoom, X,Y.
You need to "unapply" the matrix to the mouse position. To do that, you first have to invert the matrix:
Matrix inverseTransform = Matrix.Invert(_transform);
After that, you can transform the mouse vector like this:
Vector2 mouseInWorld = Vector2.Transform(new Vector2(ms.X, ms.Y), inverseTransform);

Scale texture to fit a circular body

I have the following code in my MonoGame/Farseer Physics project:
_ball = BodyFactory.CreateCircle(World, 1f, 400f);
_ball.BodyType = BodyType.Dynamic;
_ballSprite = new Sprite(ScreenManager.Content.Load<Texture2D>("Common/ball"));
In my Draw method:
ScreenManager.SpriteBatch.Draw(_ballSprite.Texture, ConvertUnits.ToDisplayUnits(_ball.Position), null, Color.White, _ball.Rotation, _ballSprite.Origin, 1f, SpriteEffects.None, 0f);
The problem is that my texture is 120px by 120px, but when it renders on screen, the _ball body is larger than it in size. What can I do to resize the texture to fit exactly the width and height of the _ball body?
I think I have solved this mystery at last. I needed to compute the scale (_ballTextureScale that is, which was the part that I was stuck on):
var radius = 1f;
_ball = BodyFactory.CreateCircle(World, radius, 400f);
_ball.BodyType = BodyType.Dynamic;
var rectangleTexture = ScreenManager.Content.Load<Texture2D>("Common/ball");
_ballTextureScale = ConvertUnits.ToDisplayUnits(radius * 2) / rectangleTexture.Width;
This converts the radius * 2 (total width of the circle) to display units which it seems the rectangle texture is already in. Then when I go to draw the body, I just needed to make use of this _ballTextureScale property:
ScreenManager.SpriteBatch.Draw(_ballSprite.Texture, ConvertUnits.ToDisplayUnits(_ball.Position), null, Color.White, _ball.Rotation, _ballSprite.Origin, _ballTextureScale, SpriteEffects.None, 0f);
If this is an incorrect approach, I am all ears to a better method for accomplishing this task, however it seems to work very well for my needs.

SharpDX Bitmaps

I've been looking around the internet for a while, and I can't seem to find the answer I've been looking for. So what I intend to do with bitmaps is I want to draw them onto the window, and be able properly resize them whenever. Any help would be appreciated!
For those of you who do not know, you can use matrices to translate, resize, and rotate. Here's how to do it. Also be sure to change the Matrix for the RenderTarget before you draw the actual object.
Vector2
RenderTarget.Transform
RenderTarget.DrawBitmap
Matrix.Transformation2D
Vector2 center = new Vector2(bitmap.Size.Width / 2, bitmap.Size.Height / 2);
renderTarget.Transform = Matrix.Transformation2D(center, 0f, new Vector2(width / bitmap.Size.Width, height / bitmap.Size.Height), center, MathHelper.ToRads(rotation), new Vector2(x - center.X, y - center.Y));
renderTarget.DrawBitmap(bitmap, 1f, BitmapInterpolationMode.NearestNeighbor);
renderTarget.Transform = Matrix.Transformation2D(Vector2.Zero, 0f, new Vector2(1f, 1f), Vector2.Zero, MathHelper.ToRads(0), Vector2.Zero);

Drawing upscaled image with no smoothing

I want to draw a texture2d 5 times larger than original with no smoothing.
Drawing line:
spriteBatch.Draw(texture, new Vector2(0, 0), null, Color.White, 0, Vector2.Zero,
5f, SpriteEffects.None, 0);
The result is a scaled smoothed image. Adding this.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; doesn't help.
What you want is Point Texture filtering. Look here.

Negative scaling with spritebatch in XNA vs Primitives

I am using both primitives and sprites in an XNA project. I draw my primitives using this code (a summary, not verbatim from my project):
transmatrix = Matrix.CreateTranslation(v23(-pos)) * Matrix.CreateScale(scale, -scale, 1f) * Matrix.CreateTranslation(v23(offset));
basicEffect.World = transmatrix;
basicEffect.View = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up);
basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, (float)GraphicsDevice.Viewport.Width, (float)GraphicsDevice.Viewport.Height, 0, 1.0f, 1000.0f);
basicEffect.Begin();
//draw primitives blah blah
basicEffect.End();
spritebatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None, transmatrix);
//draw sprites blah blah
spritebatch.end()
if I have
transmatrix = Matrix.CreateTranslation(v23(-pos)) * Matrix.CreateScale(scale, scale, 1f) * Matrix.CreateTranslation(v23(offset));
both the primitives and the sprites draw (but y is the inverse of what I want).
if I have
transmatrix = Matrix.CreateTranslation(v23(-pos)) * Matrix.CreateScale(scale, -scale, 1f) * Matrix.CreateTranslation(v23(offset));
The primitives draw correctly, but the sprites don't draw at all. What am I missing? I have tried messing around with lots of different things but nothing seems to work.
It turns it was another problem that was causing the issue. I have to scale all the sprites with negative y to get them to draw correctly as mentioned in a msdn community content "Something that is not obvious when using this form of the method to map SpriteBatch to a desired coordinate system is the effect of negative scaling on the results. If you scale by a single negative number in the X or Y axes (if, for instance, you want to flip the Y-axis to match your game coordinates) the polygon that the sprite gets drawn on gets flipped in the process and gets backface-culled. It is necessary to also scale the texture by a negative in the same direction to get the results you expect."
http://msdn.microsoft.com/en-us/library/ff433701.aspx
SpriteBatch sets up its own "camera" using its own parameters. To scale your camera, do this:
basicEffect.View = Matrix.CreateScale(1.0f, -1.0, 1.0f) * Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up);
Or you could just create a different matrix for the spritebatch.
Alternatively, you could pass in your BasicEffect to SpriteBatch:
http://msdn.microsoft.com/en-us/library/ff433700.aspx

Categories