XNA, smoother animations

I am trying to make jumping functionality in my Movement test. My character jumps and returns, but he is very hectic and not quite smooth. What happens, he approaches his maximum height, then smoothly transitions.

I can identify the problem, the for loop doesn't want to play well with the code. However, I do not know how to get around this. Is there a way to keep the button pressed and make it jump well?

The code:

if (leftStick.Y > 0.2f && sprite.Position.Y == position.Y || isPressed(Keys.Up) == true && sprite.Position.Y == position.Y) { if (wasLeft == true) { sprite.CurrentAnimation = "JumpLeft"; } else if (wasLeft == false) { sprite.CurrentAnimation = "JumpRight"; } //This for loop is my issue, it works but it jumpy and not smooth. for (movement.PlayerHeight = 0; movement.PlayerHeight < movement.PlayerMaxHeight; movement.PlayerJump()) { sprite.Position.Y -= movement.PlayerJump(); } } sprite.StartAnimation(); } else { leftStick = NoInput(leftStick); } private Vector2 NoInput(Vector2 leftstick) { if (sprite.Position.Y < position.Y) //(movement.PlayerSpeed > 0) { sprite.Position.Y += movement.PlayerHeight; movement.PlayerHeight -= movement.Player_Gravity; //sprite.Position.Y += movement.PlayerSpeed; //movement.PlayerSpeed -= movement.Player_Decel; } else { sprite.Position.Y = position.Y; } } 

Movement Class:

 public float PlayerMaxHeight = 15f; public float PlayerHeight = 0; public float Player_Gravity = 0.01f; private const float Player_Jump = 0.35f; public float PlayerJump() { PlayerHeight += Player_Jump + Player_Gravity; if (PlayerHeight > PlayerMaxHeight) { PlayerHeight = PlayerMaxHeight; } return PlayerHeight; } 
+4
source share
2 answers

My general approach to jumping fast is to use the drop value to make a slightly smoother move. I can't look at any / xna code right now, but my first thought would be as shown below.

Define the variables:

 float bleedOff = 1.0f; bool jumping = false; 

Input update:

 if(input.JumpKey()) { jumping = true; } 

Transition Update:

 if(jumping) { //Modify our y value based on a bleedoff //Eventually this value will be minus so we will start falling. position.Y += bleedOff; bleedOff -= 0.03f; //We should probably stop falling at some point, preferably when we reach the ground. if(position.Y <= ground.Y) { jumping = false; } } bleedOff = MathHelper.Clamp(bleedOff, -1f, 1f); 

Obviously, the value of bleedOff should be calculated with a little more randomness, perhaps using the value of gravity to make it look right, but this will give the illusion of acceleration / deceleration with a jump as they rise and fall.

Ascent very quickly begins and slows down and eventually begins to fall again, and it will accelerate. The clamp below is your maximum vertical speed.

I just wrote it from my head at work, so I apologize if this is not quite what you are looking for, but I tried to keep it a little more general. Hope this helps.

+2
source

The best way to make the jump that I found is to implement a property that will deal with acceleration .

A short list of what to do:

  • Create a property that preserves the current speed Y.
  • Increase Y speed by a given amount of each step - usually this is the gravity property somewhere.
  • Increment 1 position Y in speed Y at each step.
  • When you jump, simply subtract 1 indicated amount from the speed Y - this will cause your player to accelerate in the weakening movement (start quickly and slowly when it reaches the maximum jump). Since you always increase the speed Y, you will ultimately reverse the direction and return to the surface.
  • When touching the surface, reset speed Y to zero.

1 Pretty sure that the Y axis is inverted to XNA (I work in Flash), so when I say increasing the speed of Y, you may need to decrease it instead of it - the same is for jumping to subtract from it.

+3
source

Source: https://habr.com/ru/post/1403377/


All Articles