Help with 2D player physics

Sorry for spamming the forums! ^^ Just exiting about coming back to XNA after several years in the drag and drop djungle.

Im after some shortcuts this time.

I wonder if someone would be so kind and share some good 2d Player physics. Including some basic jump logic, perhaps double-juming, wallclimbing, wall-jumping etc. So i could use it as reference. I got some of my own from back in the XNA days, but they’re very simple and to be frank, quite awfull ^^

I have a really awesome raycast player controller that i made in Unity, but I doubt it will be easily translated.

Obviously i could (and perhaps should) dig in to all the math myself - but as a fulltime working dad, time is precious! Also, im a big believer of “not reinventing the wheel”…

I’m sure there’s plenty of people here sitting on awesome 2d player classes.

Additionally - I know there are some external physic libraries such as Farseer etc - but for now I think I stick to pure XNA. Unless you guys know some awesome 2d specefic. easy to use library with good cross platform support?

Appriciate any help, links, code! Cheers.

1 Like

It sounds to me like you are not sure what you want.

If you want a 2D physics system, look for Box2D. I am sure someone has ported it to MonoGame

But it sounds to me like what you actually want is a platformer starter kit.

1 Like

Thanks! I thought I made myself pretty clear. As I stated I’m primarily after some raw generic 2d player logic. Secondary I wouldnt mind some simple physics library.

I’m not looking for a starter kit, since I’m well aware of how to create platformer games in xna. What I’m missing is some juicy player logic that’s all :slight_smile:

I’m guessing your wanting to make a platformer? Oddly just before seeing this post I was pulling down an old project and there was a random project I didn’t remember making, raw bones of a basic platformer. I can see a number of issues with it right off the bat, but might be good for a starting point if your really stuck.

Thing is I’d generally make this stuff on a case by case basis. This for instance only has block tiles / block collision which was suitable for me and also fast, but if you want slopes you’d have to use a totally different approach, and tbh its mathematically quite complex handling these types of collisions. If you want to do anything more advanced than my example (e.g. slopes, sliding down slopes etc) you’d be better off with something like box2d posted above, and just write a simple controller with it.

Heres my update loop anyway

 public void Update(float deltaTime)
    {
        player.Update(deltaTime);

        //Player physics
        player.vspeed += gravity * deltaTime;

        player.X += player.hspeed * deltaTime;
        player.Y += player.vspeed * deltaTime;

        //Player collision detection
        player.isGrounded = false;
        for (var x = 0; x < tiles.GetLength(0); x++)
        {
            for (var y = 0; y < tiles.GetLength(1); y++)
            {
                var tile = tiles[x, y];
                if (tile == null || tile == player)
                    continue;

                if (player.X+player.Width > tile.X && player.X < tile.X+tile.Width
                    && player.Y + player.Height > tile.Y && player.Y < tile.Y + tile.Height)
                {
                    var mx = (player.X - tile.X);
                    var my = (player.Y - tile.Y);

                    if (Math.Abs(mx) > Math.Abs(my))
                    {
                        if (mx > 0 && player.hspeed < 0)
                        {
                            player.hspeed = 0;
                            player.X = tile.X + tile.Width;
                        }
                        else if (mx < 0 && player.hspeed > 0)
                        {
                            player.X = tile.X - player.Width;
                            player.hspeed = 0;
                        }
                    }

                    else
                    {
                        if (my > 0 && player.vspeed < 0)
                        {
                            player.vspeed = 0;
                            player.Y = tile.Y + tile.Height;
                        }
                        else if (my < 0 && player.vspeed > 0)
                        {
                            player.vspeed = 0;
                            player.Y = tile.Y - player.Height;
                            player.isGrounded = true;
                        }
                    }
                }               
            }
        }
        
    }