XNA skip simple algorithms and code

Source: Internet
Author: User

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;        }

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.