• Advertisement
Sign in to follow this  

XNA- 2D rectangle collision + physics

This topic is 2926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

so im making a 2D platformer and i finally got the jumping somewhat right but now i cant get my rectangle collision correct so he stops on a platform.heres some of my code.
[source lang = "c#"]
class Player
    {
        private enum Movement
        {
            Walking,
            Jumping
        }
        private Movement movement = Movement.Walking;

        public Texture2D texture;
        public Vector2 position;
        public float speed = 3f;
        public float velocity = 0f;
        const float gravity = .9f;
        public Rectangle rectangle;

        public Texture2D ground;
        private Rectangle groundRectangle;
        public Vector2 groundPosition;

        public Player()
        {
            this.position = new Vector2(100f, 100f);
            this.groundPosition = new Vector2(0f, 400f);
        }

        public void Load(Game game, string textureName)
        {
            this.texture = game.Content.Load<Texture2D>(textureName);
            this.ground = game.Content.Load<Texture2D>("Sprites\\Ground");

            this.groundRectangle = new Rectangle(
                (int)this.groundPosition.X,
                (int)this.groundPosition.Y,
                this.ground.Width,
                this.ground.Height);
            this.rectangle = new Rectangle(
                (int)this.position.X,
                (int)this.position.Y,
                this.texture.Width,
                this.texture.Height);
        }

        public void Update(GameTime gametime)
        {
            this.rectangle.X = (int)this.position.X;
            this.rectangle.Y = (int)this.position.Y;

            float dTime = (float)gametime.ElapsedGameTime.Milliseconds * 0.01f;

            if (this.rectangle.Intersects(this.groundRectangle))
            {
                this.movement = Movement.Walking;
                if (Input.KeyJustPressed(Keys.Space) && this.movement == Movement.Walking)
                {
                    this.velocity += -10f;
                    this.movement = Movement.Jumping;
                }
            }
            else
                this.movement = Movement.Jumping;

            if (this.movement == Movement.Jumping)
            {
                this.velocity += gravity * dTime;
                this.position.Y += this.velocity * dTime;
            }

            //Walking
            if (Input.KeyIsHeld(Keys.Right))
            {
                this.position.X += this.speed;
            }
            if (Input.KeyIsHeld(Keys.Left))
            {
                this.position.X -= this.speed;
            }
        }
    }

so what happends with this code is my player falls to the ground like i want with good physics and then once he hits the ground, he stops, ok perfect. but then when i press space he goes down about 1 pixel 3 times before he finally comes up out of the ground and flys up. what am i doing wrong here? so he goes down first then jumps WAY to much once he finally gets out of the ground. any help would be great. and if you have a different way to detect 2D collision in XNA or anything that is better than this that would be great too. i heard of per pixel collision but i heard its REALLY BAD and SLOW!! thanks again

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement