简体   繁体   中英

howto generate a smooth movement in xna for wp7?

i want to create a game and addes a image to my game, now i want it to move down smoothly. i have a code like this:

protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            pos.Y = pos.Y + 1;
            base.Update(gameTime);
        }

the movement works but it dont looks smooth, it looks like it jiggle. pos is a vector2 for the position in the image.

how to make it more smooth?

If you want movement to be smooth without adding a physics library you just have to factor in gameTime to your position update.

protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            pos.Y = pos.Y * 100 * (float)gameTime.ElapsedGameTime.TotalSeconds;
            base.Update(gameTime);
        }

I don't have access to XNA + visual studio right now, but the changes I made should give you an idea of what to try out. Keep in mind the Update call happens multiple times a second so the elapsed time will be a small number so then you have to multiply it by a larger "movement" value in this case I put 100. Tweak 100 until you see the movement speed you desire.

Beanish is right, you should multiply by GameTime if you want smoothness. Physics is an overkill if you only want your animation to look smooth.

The best way I've found to do animation is by using position interpolation, for this to work you have to know the initial (you already know this) and final position of the image.

If you want to move from A to B in, say, 2 seconds, you can use the following code.

Vector2 a = new Vector2(0, 0);
Vector2 b = new Vector2(0, 100);

float elapsedTime = 0;
float duration = 2.0;

public override void Update(GameTime gameTime)
{
    float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
    elapsedTime += dt;
    if (elapsedTime > 1)
        elapsedTime = 1;

    float param = elapsedTime / duration;
    pos = Vector2.Lerp(a, b, param);
}

The best thing about using this approach is that you can now use "easing" to make you animation look really really nice.

To do this just add a Power operation to the interpolator parameter:

pos = Vector2.Lerp(a, b, (float)Math.Pow(param /2.0, 0.5));

This will make you image slow down as it arrives to B. You can play with the exponent value (0.5) to get different results, try 2.0 for example.

Another important thing is that your image will always stop at B. If you use the Euler integration approach (your approach, adding a velocity each frame) you might have some trouble making the image stop at the right position (aka B) and it gets even worse when using 2 or 3 dimesions.

To know more about easing, check Robert Penner's Easing Equations .

First I can tell you what the problem isn't. You don't need a physics engine to have smooth movement. And changing the Update to include the ElapsedGameTime will not make a lick of difference for the smoothness (assuming you haven't changed the default of IsFixedTimestep to false). When there is a fixed timestep, ElapsedGameTime will always have the same value, it will not vary.

I don't how much you are doing in your code, but if it's too much, XNA will start skipping the Draw portion of your code, and this can definitely cause jerkiness. One way to check: in your Update method, test the value of IsRunningSlowly . Whenever it is true, XNA will skip some Draw calls.

If you are not doing anything complicated, then the culprit may be the refresh rate of your monitor. If it is set to anything other than 60Hz, you will have jerkiness. You could fix this by changing your monitor's refresh rate. Alternatively you can change the value of TargetElapsedTime to match your monitor's rate.

You should consider adding to your game a library for handling physics, as for example FarseerPhysics . By calculating the position in a per time base with physics rules applied your movements will be smooth and natural.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM