Sprite Distortion (Ghosting While Moving and Artifacts when Mirroring to External Display)

I am working on my first MonoGame project. I love the framework so far!

I have implemented my own letterbox/pillarboxing to scale my native resolution by the maximum integer scale allowable on my display. Basically, I determine the maximum integer scale, set my PreferredBackBuffer to the screen resolution, create a Viewport that is my native resolution * maximum scale, and then set my SpriteBatch to draw everything at Matrix.CreateScale(max_scale).

This works much better than rendering to a texture that has my native resolution and then scaling it up. (Scaling using a Matrix in SpriteBatch, as opposed to just rendering to a texture and then scaling it up, allows you to fake “subpixel rendering” to some extent).

That said, I am facing two issues.

  1. When my sprite moves, there is very subtle “ghosting” happening. The sprite is subtly blurry and there’s a faint ghostly trail behind it.

  2. When I mirror to an external monitor, there is less ghosting, but there is ugly artifacting on the outside of sprite when stationary. See below.

enter image description here

Rounding my player’s position to integers in the Draw() command doesn’t help for either problem.

Does anyone have any thoughts about how to fix these issues?

Here is my game class:

using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;  namespace MyMonoGame {     public class MyGame : Game     {         // declare variables         GraphicsDeviceManager graphics;         SpriteBatch sprite_batch;          // resolution management         int native_width;         int native_height;         int screen_width;         int screen_height;         int max_scale;         int horizontal_margin;         int vertical_margin;          // objects         Player player;          public MyGame()         {             // create GraphicsDeviceManager instance             graphics = new GraphicsDeviceManager(this);             // specify root directory             Content.RootDirectory = "Content";         }          protected override void Initialize()         {             // set window title             this.Window.Title = "My Game";              // create SpriteBatch instance, which can be used to draw textures.             sprite_batch = new SpriteBatch(GraphicsDevice);              // initialize some variables             native_width = 160;             native_height = 144;              // resolution management             // get screen size             screen_width = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;             screen_height = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;             // get max_scale, the maximum integer scale that will fit on the screen             // note: must be integer to prevent pixel distortion             int width_divisor = (int) Math.Floor((float)screen_width/(float)native_width);             int height_divisor = (int) Math.Floor((float)screen_height/(float)native_height);             max_scale = Math.Min(width_divisor, height_divisor);             // get margins for letterboxing and pillarboxing             int max_width = native_width * max_scale;             int max_height = native_height * max_scale;             horizontal_margin = (int)((screen_width - max_width)/2f);             vertical_margin = (int)((screen_height - max_height)/2f);              // toggle fullscreen             graphics.PreferredBackBufferWidth = screen_width;             graphics.PreferredBackBufferHeight = screen_height;             graphics.ToggleFullScreen();             GraphicsDevice.Viewport = new Viewport(horizontal_margin, vertical_margin, native_width * max_scale, native_height * max_scale);             graphics.ApplyChanges();              // objects             player = new Player(this);         }          protected override void LoadContent()         {         }          protected override void UnloadContent()         {         }          protected override void Update(GameTime gameTime)         {             if (Keyboard.GetState().IsKeyDown(Keys.Escape))                 Exit();              // update objects             player.Update(gameTime);         }          protected override void Draw(GameTime gameTime)         {             // clear window & fill with solid color             GraphicsDevice.Clear(Color.DarkRed);              // draw objects             var transform_matrix = Matrix.CreateScale(max_scale);             sprite_batch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, transformMatrix: transform_matrix);             player.Draw(sprite_batch);             sprite_batch.End();         }     } } 

How to animate two or more unrelated properties on a sprite GameObject using animation layers?

I have a sprite in Unity 2019.3. It is animated like so (with provided mockups):

  • Its base animation is just one frame; i.e. it has the same Sprite whether idle or moving.
  • When the player is invincible, the ship flashes in and out. This is implemented by setting the alpha to 0 and 1 and back.
  • The player can acquire up to three levels of shields. The is implemented as a child GameObject with its own Sprite (but not its own Animator).
  • When a player acquires a power-up, they briefly flash colors. This can happen while they’re shielded, invincible, or both. This is implemented with a custom palette-swap shader, not by replacing the Sprite.

These animations are all independent of one another, and can all occur at any time. For example, when a shielded player takes a hit their shield downgrades but they also gain brief invincibility. It would look like this:

A single state machine to account for these possibilities (and others, depending on whether or not I add other power-ups or mechanics) would be prohibitively complex. So I’m going with animation layers.

I was hoping that it would be as simple as adding separate animation layers with their own state machines, then marking each layer as additive. Nope. I have no idea what I’m doing.

How can I use Mecanim to animate my sprite as I have previously described?

How do i draw sprite on fixture position?

b2BodyDef bd;     bd.position.Set(1700 / sfdd::SCALE, 150 / sfdd::SCALE);     bd.type = b2_dynamicBody;     b2PolygonShape squareShape;     squareShape.SetAsBox(70 / sfdd::SCALE, 13 / sfdd::SCALE);     m_body1 = m_world->CreateBody(&bd);     m_body1->CreateFixture(&squareShape, 100);     squareShape.SetAsBox(38 / sfdd::SCALE, 16 / sfdd::SCALE,b2Vec2(-54/sfdd::SCALE,-30/sfdd::SCALE),90*DEGTORAD);     m_body1->CreateFixture(&squareShape, 20);      squareShape.SetAsBox(30 / sfdd::SCALE, 10 / sfdd::SCALE, b2Vec2(60 / sfdd::SCALE, -20 / sfdd::SCALE), 90 * DEGTORAD);     m_body1->CreateFixture(&squareShape, 100); 

Blurry background sprite, other sprites fine

Why is the background blurry, but all the other sprites fine?

Background sprite blurred

The background sprite has the same settings as the others. It’s on the same Z position (0). It happened after I made some changes to the original image, just deleting some colours on it, not changing it’s dimensions. It’s coming from a sprite sheet of 11 images. All the images are blurred in the same way

Sprite improt settings

Blurry background sprite, other sprites fine

Why is the background blurry, but all the other sprites fine?

Background sprite blurred

The background sprite has the same settings as the others. It’s on the same Z position (0). It happened after I made some changes to the original image, just deleting some colours on it, not changing it’s dimensions. It’s coming from a sprite sheet of 11 images. All the images are blurred in the same way

Sprite improt settings

Sprite is getting out of screen when changing resolution – Unity

Hello I am making this layout:

enter image description here

I used sprites for birds but used UI for the blue food thrower. I used UI for it so it will not get cut when screen resolution changes. But I also need to add food itself which I need to make 2D to interact with birds. Now It is not visible over UI, So I changes Canvas-Render Mode to world space. But I cannot adjust text in that mode. Its not visible. Then I tried converting the food thrower to Sprite but it get cut. Any authentic and cleaner way to do it?

How do I assign the sprite layer in the function definition in pygame?

I am trying to code a game called ‘Scrolling Shooter’ with Pygame. I am trying to draw sprites over each other in the right order. In the initialization function of each sprite class, I assign an attribute called ‘layer’. According to the pygame docs, this should change the layer of the sprite in the sprite in the allSprites = pygame.sprite.LayeredUpdates() function and the sprites with a higher value of layer should be drawn on top of sprites with a lower value. Despite the parameter, if I use get_layer_of_sprite in the draw function, it just gives 0. Somebody on the r/pygame subreddit said that the attribute should be _layer instead of layer but that didn’t work either.

The allSprites.draw(display) function is in my draw() funtion. The draw() function is called in each iteration of the main loop.

You can find the relevant code here: https://github.com/CiriOfRivia/Pygame-Scrolling-Shooters/blob/master/hi

You can find the full code here: https://github.com/CiriOfRivia/Pygame-Scrolling-Shooters/blob/master/Layering%20issue

Pygame docs: “If the sprite you add has an attribute layer then that layer will be used. If the **kwarg contains ‘layer’ then the sprites passed will be added to that layer (overriding the sprite.layer attribute). If neither sprite has attribute layer nor **kwarg then the default layer is used to add the sprites.”

Thanks in advance for helping me. I probably did one little thing wrong which messes up everything, which is so often the case when I’m coding.

If I add the layer to the sprites after initializing them, it seems to work fine. However, I want to be able to set the layer in the initialization function.

Expected result: The layer of the plane is higher than the layer of the island, thus the plane should be drawn over the island. The output should be: Plane 3 Island 1

Actual result: The layer of the plane is equal to the layer of the island. The drawing order is random and the island gets drawn over the plane The output is: Plane 0 Island 0

Aligning a sprite with a tilemap

I’m trying to create a hybrid system for my Unity game. I want to use tilemap for collision and use sprites for the world.

I drew a sprite onto a 40×40 checkerboard grid as can be seen here. I wanted an easy way to create a collider for this without spending 30+ minutes hand placing box colliders and I decided to use a tilemap to place red tiles and use a tilemap collider for collision geometry and just hide the red tiles in-game.

Issue is, I have no clue how to align the checkerboard with the Unity tilemap grid.

Here’s a screenshot of the sprite in the unity editor with the tilemap grid visible.

If it isn’t obvious, then the white lines is the grid for the tilemaps. The cellsize for each cell on the grid isn’t the same size as the cells on the checkerboard pattern and I have no idea how to fix it.

The PPU (pixels per unit) of the sprite is 140. The PPU for the tileset tile(singular since there is literally only 1 tile in the entire tileset) is also 140. The checkerboard in paint.net is 40×40 and so is the tile. How can I get the cellsize of the grid in unity to match the cell size of the checkerboard.