Rendering with floating point numbers

Hello everyone.

I’m making my first Monogame project after several years of working with LibGDX, and i can see they’re very similar, but i’m still trying to figure out things that you may encounter to be basic.

Right now i’m using the Camera component from the Extended extension and it works very well, however i’m stumbling with the rendering of sprites right now.

My “Game World” size is very small (26 x 15 units), since i’m doing a Pixel Art game and the size of the world is actually the number of tiles that fit the screen (26 x 15 tiles, each tile of 16x16 dimension, so the actual screen size would be 416 * 240 pixels)

So, i’m doing my rendering of sprites based on the “Game World” size. Based on that, my main character would be of size 1x2

The problem is that every Draw method of the SpriteBatch where the destination bounds of the screen can be set, that bound is represented with a Rectangle parameter that only takes integer values, and since my “Game World” size is so small, i need my character to be positioned on floating point values

There are other Draw methods that takes the position of the texture as a Vector2, but on those methods i can’t set the size so they render with the size of the texture, which is much larger than the size of the character represented on the “Game World” coordinates

So, is my approach of using a “Game World” of small size correct?. If so, how can i render my textures using floating point numbers?


There is a Draw() method that allow you scale the texture as well, but using Rectangle is much simpler, all you need is to define the Camera to be 416x240 pixels.

Using the following code you can draw on a virtual 416x240 resolution.
You’d also have to handle different aspect ratios, possibly by adjusting the viewport.

        private BasicEffect _spriteBatchEffect;
            _spriteBatchEffect = new BasicEffect(_graphics.GraphicsDevice);
            _spriteBatchEffect.TextureEnabled = true;
           _spriteBatchEffect.World = Matrix.Identity;
           _spriteBatchEffect.View = Matrix.Identity;
           _spriteBatchEffect.Projection = Matrix.CreateOrthographicOffCenter(0, 416, 240, 0, 0f, -1f);
           _spriteBatch.Begin(SpriteSortMode.Deferred, efffect: _spriteBatchEffect);
           // draw here ...

I didn’t understand very well what have you done here. My SpriteBatch is already using the projection of the Camera, which accurately translates the World coordinates to screen. I’m able to render the sprites with its corresponding size, but the problem is in the render position.

Let’s say for example that i have my main character sprite with the following bounds: [x=1.5f, y= 3.5f, w=1, h=2]. I’m currently unable to draw this because the position [X, Y] is using floating point numbers so i can’t use any of the Draw methods that uses a Rectangle as a destination bound, and if i use a Draw method that has only the sprite position as parameter, my sprite will be drawn with the same size of its texture (16x32) and it looks gigantic.

I tried using one of the Draw methods that has a scaling parameter using a scale of 1/16f, and it seems to work fine. But i think it’s overly too complicated compared to rendering sprites on LibGDX. I mean, it would be so much simple if only the Draw methods supported RectangleF instead of Rectangle. Is there a technical reason for this?

I see, so you want to position the sprite in between pixels, in that case use the Draw() that accept Vector3Vector2. If the projection match that of your sprite’s resolution you wouldn’t need to scale it.
But you would need to use 416x240 as your resolution, not 26x15.

I am not familiar with the Extended camera, but I suppose it does let you to set a virtual resolution.

mmm I don’t seem to find that Draw method with a Vector3 parameter you’re mentioning

Sorry, I meant Vector2.

As Nkast said, there is a overload method with a vector2 where you can also set a scale and rotation factor. Of you want to add transparency you can also multiply colour by a float from 0 to 1. Eg Color.white * 0.5f

You may have to make the origin the center of the sprite size.

	public void Draw 
		 Texture2D texture,
		 Vector2 position,
		 Nullable<Rectangle> sourceRectangle,
		 Color color,
		 float rotation,
		 Vector2 origin,
		 Vector2 scale,
		 SpriteEffects effect,
		 float depth