Just a friend asked me how to implement the jump. In fact, I don't know about xna either. In Zzk, there is no matching record for searching "XNA jump". I was a little impulsive when I was surprised. Do I have to do it myself? But xna really doesn't understand it. I found two or three source codes, and I was really stupid. I was so sorry to understand its algorithm. Simply implement it from the ground up by yourself. The reason to consider taking off from reality is the Take-off Speed (v1) weight (w) gravity g, which cannot be remembered according to the physical xxx, simple is perfect:
Global parameters:
private bool _isJumping; private bool _jumpKeyDown; private float _jumpHeight = 100f; private TimeSpan _jumpTimeSpan = TimeSpan.FromMilliseconds(500); private double _jumpStartTime; private float _jumpStartPosition;
Code in Update:
_jumpKeyDown = Keyboard.GetState().IsKeyDown(Keys.Space); squarePosition.Y = DoJump(squarePosition.Y, gameTime);
High Algorithm functions:
private float DoJump(float velocityY, GameTime gameTime) { if (_jumpKeyDown && !_isJumping) { _isJumping = true; _jumpStartTime = gameTime.TotalGameTime.TotalMilliseconds; _jumpStartPosition = velocityY; } if (_isJumping) { var jumpTime = gameTime.TotalGameTime.TotalMilliseconds - _jumpStartTime; var topTime = _jumpTimeSpan.TotalMilliseconds / 2; if (jumpTime < _jumpTimeSpan.TotalMilliseconds) { if (jumpTime > topTime) { velocityY = _jumpStartPosition-_jumpHeight - Convert(-(jumpTime - topTime) * _jumpHeight / (_jumpTimeSpan.TotalMilliseconds - topTime)); } else { velocityY =_jumpStartPosition+ Convert(-( jumpTime)*_jumpHeight/topTime); } } else { _isJumping = false; velocityY = _jumpStartPosition; } } return velocityY; }