I have been having some problems with my coding lately. I am trying to create a platform-based game and I have been having some troubles.

A friend of mine said that some update functions are out of order, so my collision is causing a jittery effect. It looks kind of weird so I am hoping that you guys could help me.

Here is the code for the update in game1.cs and player.cs

game1.cs -

protected override void Update(GameTime gameTime)
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    if (f.rectangle.Intersects(player.rectangle))
        player.isOnSurface = true;
        player.position.Y = f.rectangle.Top - player.texture.Height;  
    else player.isOnSurface = false;

player.cs -

public void update(GameTime gameTime)
    position = position + velocity;
    // walking
    if (Keyboard.GetState().IsKeyDown(Keys.A) | Keyboard.GetState().IsKeyDown(Keys.Left)) velocity.X = -4f;
    else if (Keyboard.GetState().IsKeyDown(Keys.D) | Keyboard.GetState().IsKeyDown(Keys.Right)) velocity.X = 4f;
    else velocity.X = 0;
    // jumping

    if (Keyboard.GetState().IsKeyDown(Keys.W) | Keyboard.GetState().IsKeyDown(Keys.Up) | Keyboard.GetState().IsKeyDown(Keys.Space) && isOnSurface == true)
        isOnSurface = false;
        position.Y -= 10;
        velocity.Y = -5;
    // gravity
    rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
    if (!isOnSurface)
        float i = 1;
        velocity.Y += 0.15f * i;

        velocity.Y = 0f;

I would just like to know if my functions are in order, or how to get rid of the jittery effect (and therefore a good method for collision).


I'm not 100% sure on this but it seems that the problem may be in the fact that in each frame for gravity you are adding 0.15 to Y velocity, but then casting it back down to an integer when you do your rectangles. So if technically you've adjusted your position by a fraction of a pixel, then when you do collision checks it will say you are not colliding (because you are casting that 0.15 down to 0) and then (I assume) drawing the sprite to the fractional pixels. You can try casting the positions in your draw method to integers before you draw it. This could prevent the jittery between-pixel drawing.

  • \$\begingroup\$ what do you mean casting my positions in my draw method? \$\endgroup\$ – cheesecake Mar 25 '13 at 9:53
  • \$\begingroup\$ When you draw your sprite use (int)position.X and (int)position.Y instead of just position \$\endgroup\$ – ssb Mar 25 '13 at 10:19
  • \$\begingroup\$ like "spriteBatch.draw(texture, (int)position.X, (int)position.Y, color.White)"? \$\endgroup\$ – cheesecake Mar 25 '13 at 10:54
  • \$\begingroup\$ spriteBatch.draw(texture, new Vector2((int) position.X, (int) position.Y), Color.White) \$\endgroup\$ – ssb Mar 25 '13 at 11:07

There seems to be a slight chance that after you detect collision with the object f, that the players velocity is not zero. The next time player.Update is called you adjust the players position by the velocity, then later do the check on isOnSurface and set the velocity to zero. This can cause a jitter effect like you are seeing.


Not the answer you're looking for? Browse other questions tagged or ask your own question.