19.6 C
New York
Tuesday, May 20, 2025

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


What I’m making an attempt to realize is that when a sprite strikes right into a wall, it is going to keep on the correct facet of the wall, with no actions even when the hot button is held.

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

That is in all probability brought on by the sprite being set again far sufficient that it is not thought of intersecting the textual content, so when velocity is utilized the subsequent body it is going to intersect, get pushed again, repeat.

How can I repair this?

(reworded query)

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

public Courtroom court docket;

    public bool MovePaddle(GameTime gameTime) 
    {
        if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
        {
            route = new Vector2(-1, 0);
            velocity.X = route.X * pace * (float)gameTime.ElapsedGameTime.TotalSeconds;
            place.X += velocity.X;
            return true;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.Proper))
        {
            route = new Vector2(1, 0);
            velocity.X = route.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:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles