Jump to content

  • Log In with Google      Sign In   
  • Create Account


XNA- 2D rectangle collision + physics


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 nugums   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 January 2010 - 11:51 AM

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

Sponsor:

#2 nugums   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 January 2010 - 12:42 PM

nevermind im just gonna use farseer for this garbage :P




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS