1
\$\begingroup\$

I'm making a 2D fighter, and I have gotten a basic engine, but right now I need to add the collisions, but I'm sure a basic AABB check will not be enough.

I'm sure it needs some basic 2D physics equations, but every resource I see on the web is extremelly complex or try to teach you everything you need to know to make a 2D physics engine.

So far, I'm sure it's related to momentum and forces, but don't know if those are enough for a 2D collision in a fighting game and what are the simplest ways to implement them.

\$\endgroup\$
  • 1
    \$\begingroup\$ Depending on your game, a different level would be considered "basic". Could you be more specific about what you want to know? \$\endgroup\$ – Anko Oct 12 '15 at 18:44
  • \$\begingroup\$ The question is very broad and includes elements of "what does a physics engine do" on top of "how do I implement a custom physics engine." You may not need any of that. I recommend reading up on 2D Fighting Game Hitboxes (and Hurtboxes) plus spending a little time with the Box2D faq and examples, and other physics engines to get an idea for what things they handle to guide your design. \$\endgroup\$ – Patrick Hughes Oct 12 '15 at 19:48
0
\$\begingroup\$

You are asking for two subjects: the detection and the resolution of the collisions.

For detection in a fighting game, simple intersection tests for AABB are sufficient. Every animation can have several bounding boxes, and each box has a type (attack or body, for example). In every game frame you test intersection of all attack boxes of player1 against all body boxes of player2. And then the reverse.

After the detection phase, the resolution phase begins. In it, you apply the forces of the attacks that was registered in the previous phase (as well as damage and transition to hurt animations). You may storage all this data (damage, forces, target hurt animation) into the attack boxes.

See my animation editor below. It's in portuguese, but you can see the attack properties in the left panel: "ForçaX" and "ForçaY" (forces), Dano (damage), etc.

enter image description here

And for the physics simulation you can use Euler integration. Here's how I do it:

public void UpdatePhysics(Entity entity) {
    entity.Position = new Vector2(entity.Position.X + entity.Velocity.X, entity.Position.Y + entity.Velocity.Y);

    var horizontalVelocity = entity.Velocity.X;

    if (entity.Velocity.X != 0) {
        if (entity.Velocity.X > 0) {
            horizontalVelocity -= entity.Friction.X;

            if (horizontalVelocity < 0) {
                horizontalVelocity = 0;
            }
        } else if (entity.Velocity.X < 0) {
            horizontalVelocity += entity.Friction.X;

            if (horizontalVelocity > 0) {
                horizontalVelocity = 0;
            }
        }
    }

    var verticalVelocity = entity.Velocity.Y;

    if (entity.Velocity.Y != 0) {
        if (entity.Velocity.Y > 0) {
            verticalVelocity -= entity.Friction.Y;

            if (verticalVelocity < 0) {
                verticalVelocity = 0;
            }
        } else if (entity.Velocity.Y < 0) {
            verticalVelocity += entity.Friction.Y;

            if (verticalVelocity > 0) {
                verticalVelocity = 0;
            }
        }
    }

    entity.Velocity = new Vector2(horizontalVelocity + entity.Acceleration.X, verticalVelocity + entity.Acceleration.Y);
}
\$\endgroup\$
1
\$\begingroup\$

Ian Parberry - Introduction to Game Physics with Box2D first half of this book explains what you need. This is a broad topic and I would recommend you to read it to get all answers.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.