[Solved] 2D sprites Shake

I’ve recently started to use MonoGame and I’m have troubles with sprites shaking

The game currently contains a few orbital bodies and a small spaceship. When the space ship is moving, the movement is smooth, but I’m trying to implement a mechanic that the ship will lock into the position of a planet.
img1

Currently I’m just setting the position of the ship to the position of the orbital body, in this example the Earth. But when setting the position something shakes, either the planet or the ship can’t really tell which one, its minor but definitely noticeable.

For moving the planet, I get its starting angle from the position with:

angle = (float)(Math.Atan2(vec.Y, vec.X) - Math.Atan2(-1, 0));

Then I update the angle each game tick and update the position.

Vector2 newPos = new Vector2(
                 (float)(Math.Sin(angle) * distance),
                 (float)(-Math.Cos(angle) * distance));

            Pos = newPos + parentBody.Pos;
            angle += orbitTime * (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (angle > 360f) {
                angle -= 360f;
            }

And for the ship its just setting it’s position of the calculated position of the Earth.

The camera I use I took from this topic.

I set the spriteBatch transformMatrix to the camera’s matrix.

transformMatrix: camera.Transform

Now I don’t know where the problem of the shaking lies. Could be with the camera, the orbital position or something else.
Any help here will be amazing!

More code or the project can be included if needed!

How are you drawing your sprites. Using a destination rectangle or a destination vector?

Thanks for the reply!

I am using a rectangle.

The code for drawing is as follows:

spriteBatch.Draw(texture, Rectangle(), null, Color.White, Rot, new Vector2(texture.Width / 2, texture.Height / 2), spriteEffects, 0);

And the rectangle function:

return new Rectangle((int)(Pos.X + offset.X), (int)(Pos.Y + offset.Y), (int)Scale.X, (int)Scale.Y);

I was following a tutorial by Batholith Entertainment on YouTube and not sure if this is the best way of doing it!

could also just be a sampling issue … do you use MipMapping?

If your using the monogame camera your movement will not be very smooth if your drawing using rectangles. All your vectors are rounded to int position.

Spritebatch.draw uses an overload that takes in a vector instead of a rectangle. The overload also can use a vector for scaling the x width or the height, as well as radians for rotating.

1 Like

Thank you very much for the replies boot!

So the problem was in the fact that I was using the rectangle to draw the sprites as the rectangles require an int as an input so I had to round the position values! Thank you very much

No worries. Right now your destination size will be the size of your source rectangle. If you need to resize it you can also pass in a vector as a scale (2,2 for twice size and width) as one of the parameters. If you need to rotate the sprite you can also pass in a float as radians as a parameter

If you use rotation the origin is a vector on where on the sprite the rotation will be. The method overload is as follows

Draw (Texture2D texture,
Vector2 position,
Rectangle? sourceRectangle,
Color color,
float rotation,
Vector2 origin,
float scale,
SpriteEffects effects,
float layerDepth)

1 Like

Thanks again for the help!
Managed to figure out the other overloads after a while :stuck_out_tongue:

Great! Also if your looking for some maths and ideas around gravitational pull from planets etc i highly recommend watching Dan’s teachings on the coding train. Its in Java but it’s the same idea. Theres also some other good videos regarding drag and friction in this series.

Watch “2.5 Gravitational Attraction - The Nature of Code” on YouTube