Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)
        this.Exit();
    player.update(gameTime);
    if (f.rectangle.Intersects(player.rectangle))
    {
        player.isOnSurface = true;
        player.position.Y = f.rectangle.Top - player.texture.Height;  
    }       
    else player.isOnSurface = false;
    f.update(gameTime);
    base.Update(gameTime);
}

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;

    }
    else
    {
        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).

share|improve this question
    
Resetting the position and the velocity of the player is OK, but I would do it before drawing the sprite. –  Marton Mar 25 '13 at 7:59
    
well i believe the jitter happens when the bounding boxes intersect, when player position gets moved up, collision says that it is no longer colliding and so falls back down and does a endless loop of jitters –  cheesecake Mar 25 '13 at 8:15
    
Yes, it happens because of that. But if you do the collision detection (and the position reset) before drawing the sprite, this won't happen. –  Marton Mar 25 '13 at 9:16
    
add comment

closed as too localized by Byte56, Noctrine Mar 25 '13 at 18:22

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

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.

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

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.

share|improve this answer
add comment

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