Saturday, August 2, 2025
HomeGame DevelopmentSprite stutters when shifting into wall/collide-able object in Monogame

Sprite stutters when shifting into wall/collide-able object in Monogame


What I’m attempting to attain is that when a sprite strikes right into a wall, it would keep on the right facet of the wall, with no actions even when the bottom line is held.

Nevertheless, with the strategy I’m utilizing (setting the sprite to the right location upon collision), I get micro stutters once I maintain the motion key into the wall.

That is in all probability attributable to the sprite being set again far sufficient that it’s not thought-about intersecting the textual content, so when velocity is utilized the subsequent body it would intersect, get pushed again, repeat.

How can I repair this?

(reworded query)

public Vector2 course = Vector2.Zero;
public float pace;
public Vector2 velocity;

public Court docket court docket;

    public bool MovePaddle(GameTime gameTime) 
    {
        if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
        {
            course = new Vector2(-1, 0);
            velocity.X = course.X * pace * (float)gameTime.ElapsedGameTime.TotalSeconds;
            place.X += velocity.X;
            return true;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.Proper))
        {
            course = new Vector2(1, 0);
            velocity.X = course.X * pace * (float)gameTime.ElapsedGameTime.TotalSeconds;
            place.X += velocity.X;
            return true;
        }
        return false;
    }

    public bool CheckCollisions() 
    {

        if (CollideBox.Intersects(court docket.borders[1]) || CollideBox.Intersects(court docket.borders[2])) 
        {
            if (CollideBox.Proper >= court docket.borders[2].Left)
            {
                velocity.X = -1;
                this.place.X = court docket.borders[2].Left - picture.Width;
                
                return true;
            }
            else if (CollideBox.Left <= court docket.borders[1].Proper) 
            {
                velocity.X = 1;
                this.place.X = court docket.borders[1].Proper;
                return true;
            }
        }
        return false;
    }

    public override void Replace(GameTime gameTime) 
    {
        CheckCollisions();
        MovePaddle(gameTime);
        
        base.Replace(gameTime);
    }
}

This is an instance of the issue:

Sprite stutters when shifting into wall/collide-able object in Monogame

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments