handmade.network » Forums » ---Need help please --- Coding basic simple 2d platformer physics
msmshazan
Shazan Shums
162 posts

Some day I will make quality software.
Programming FTW.

#11658 ---Need help please --- Coding basic simple 2d platformer physics
6 months, 1 week ago

I was using farseer for an XNA project but I think its an overkill for simple 2d stuff and the setup and coding pattern is hard so I was trying to code the physics myself but I think I'm stuck.
The issue is resolving collision and doing wall grab ,jumping and stuff...(maybe all stuff in a platformer like sonic)
The thing is though people say that it is easy to use a prebuilt library it is not especially if it is others code.Any guidance would be helpful.


Here's my current progress...

Physics.cs gist

Just being curious on programming....
timothy.wright
Timothy Wright
75 posts
1 project
#11659 ---Need help please --- Coding basic simple 2d platformer physics
6 months, 1 week ago Edited by Timothy Wright on April 10, 2017, 4:05 p.m.

Personally I think if you are making a meat-boy like platformer, then it will better to just make your own equations and formulas. True physics will simulate everything correctly but it won't "feel" right.

I would start with just pushing colliding objects out to the collision point, and making sure you've got the time step correct.

Here are two good references for the time step.

https://gamedevelopment.tutsplus....engine--gamedev-7493#timestepping

http://gafferongames.com/game-physics/fix-your-timestep/
stevetranby
Steve Tranby
12 posts

Code Monkey

#11662 ---Need help please --- Coding basic simple 2d platformer physics
6 months, 1 week ago Edited by Steve Tranby on April 14, 2017, 1:44 a.m.

I alway enjoy this one as a nice overview of various aspects of 2d platformer implementation.
http://higherorderfun.com/blog/20...e-to-implementing-2d-platformers/

And the same blog on fixing timestep has a good reminder that Euler integration (the commonly known formula for pos/vel/accel) is "It is only 100% accurate if the rate of change is constant over the timestep."
http://gafferongames.com/game-physics/integration-basics/

Jon Blow and Sean Barret also discuss frame-rate independence.
https://www.youtube.com/watch?v=fdAOPHgW7qM
https://www.youtube.com/watch?v=jTzIDmjkLQo

I see 0xdeadc0de everywhere
msmshazan
Shazan Shums
162 posts

Some day I will make quality software.
Programming FTW.

#11683 ---Need help please --- Coding basic simple 2d platformer physics
6 months, 1 week ago Edited by Shazan Shums on April 13, 2017, 10:55 a.m.

Thank you for your helpful guidance.

I have implemented AABB collision but it seems buggy.


Here's the problem Physics Bug.gif

It collides before hitting the object.

Here's a part of source code in main..
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
if (Physics.AABBvsAABB(Crate.AABB, Floor.AABB))
            {
                Crate.Collided = true;
                Floor.Collided = true;
            }
            Crate.Update(gameTime);
            Floor.Update(gameTime);


.... some code 

  Renderer.Draw(Crate.GetRenderPos(), Crate.Texture,Camera.get_transformation());
            Renderer.Draw(Floor.GetRenderPos(), Floor.Texture,Camera.get_transformation());
   


in entity update
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 public enum FacingDirection
    {
        Up = 0,
        Down = 1,
        Left = 2,
        Right = 3
    }

    public enum EntityTag
    {
        Player = (0 << 1),
        Enemy = (1 << 1),
        Crate = (2 << 1),
        Floor = (3 << 1)

    }

    public  struct  Entity
    {

        public bool Active ;
        public bool Visible ;
        public bool Collided ;
        public bool Special;
        public FacingDirection Direction;
        public EntityTag Tag;
        public long Health;
        public long Stamina;
        public long MaxHealth;
        public long MaxStamina;
        public float HealthRegen;
        public float StaminaRegen;
        public Texture2D Texture;
        public AABB AABB;
        public Vector2 Dim;
        public Vector2 Pos;
        public Vector2 dPos;
        public Vector2 ddPos;
        public static Color _Color = Color.White;
        public static int depth = 0;
        public Color Mask{
            get{ return _Color; }
            set { _Color = value; }
            
        }

        public void SetDim()
        {
            Dim.X = Texture.Width;
            Dim.Y = Texture.Height;
        }
        public void SetAABB()
        {
            AABB.Center = Pos;
            AABB.HalfDim = Dim/2;
        }


        public Vector2 GetRenderPos()
        {
            return AABB.Center - AABB.HalfDim;
        }

        public void Update(GameTime gt)
        {
            float dt = (float)gt.ElapsedGameTime.TotalSeconds;
            AABB.Center = Pos;
            dPos = Vector2.One;
            if (Collided)
            {
                ddPos = -ddPos * 0.1f;
                Collided = false;
            }
            dPos *= ddPos;
            Pos += dPos * dt;
        }
    }


Here's physics
1
2
3
4
5
6
7
8
9
 public struct AABB
    {
        public Vector2 Center;
        public Vector2 HalfDim;
    }
 public static bool AABBvsAABB(AABB A, AABB B)
        {
            return (Math.Abs(A.Center.X - B.Center.X) > (A.HalfDim.X + B.HalfDim.X) || Math.Abs(A.Center.Y - B.Center.Y) > (A.HalfDim.Y + B.HalfDim.Y)) ? false : true;
        }

Just being curious on programming....
msmshazan
Shazan Shums
162 posts

Some day I will make quality software.
Programming FTW.

#11684 ---Need help please --- Coding basic simple 2d platformer physics
6 months, 1 week ago Edited by Shazan Shums on April 13, 2017, 12:59 p.m.

I guess it was a rendering bug.

Now the issue is about resolving velocities after collison and jump code.

Any help?

Just being curious on programming....
timothy.wright
Timothy Wright
75 posts
1 project
#11685 ---Need help please --- Coding basic simple 2d platformer physics
6 months, 1 week ago

msmshazan
Shazan Shums
162 posts

Some day I will make quality software.
Programming FTW.

#11693 ---Need help please --- Coding basic simple 2d platformer physics
6 months, 1 week ago Edited by Shazan Shums on April 14, 2017, 12:21 p.m.

I found another bug Box falling through.

Here's collision resolve code


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Crate.Update(gameTime);
            Floor.Update(gameTime);
            if (Physics.AABBvsAABB(Crate.AABB, Floor.AABB))
            {
                Crate.Collided = true;
                Floor.Collided = true;
                Crate.Pos = Crate.PrevPos;
                Crate.InAir = false;
                Crate.ddPos = Vector2.Zero;
            }


I can't figure it out.


And how do you post gifs in hmn.

Just being curious on programming....