0
\$\begingroup\$

I'm currently developing a physics engine and I'm not sure the best way to go about resolving my problem. I have a little box, that I can move around with in my scene.

When I'm resolving collisions, I take in the two bodies that are colliding, determine what both of their normal forces are and apply it to them (I also multiple it by a small number, if I don't I can't continue to walk around the scene).

Anyways, while I'm resolving the collision I do this:

a.Position -= a.Velocity;
b.Position -= b.Velocity;

I know this isn't a very good way to resolve the collision, but I'm just not sure how to do it correctly.

How should I go about resolve collision correctly?

Also, here is the whole method I use to resolve collision at the moment:

    private void ResolveCollision(Body a, Body b)
    {
        // Calculate normals forces (N = M * G)
        Vector2 normalForceA = this.CalculateNormalForce(a);
        Vector2 normalForceB = this.CalculateNormalForce(b);

        // Resolve collision, which is super buggy and obviously a bad idea.
        a.Position -= a.Velocity;
        b.Position -= b.Velocity;

        // Apply normal forces
        a.ApplyForce(-normalForceA * 11);
        b.ApplyForce(-normalForceB * 11);
    }

EDIT:

This is for a 2D game.

EDIT

Here is a video of what's going on when I applied TomTsagk's answer.

The video is here

\$\endgroup\$
0
\$\begingroup\$

It really depends on what result you would like to have. Here are some examples:

Stick together

When two objects collide, you can make them "stick" to each other. For example, based on your code, replace

a.Position -= a.Velocity;
b.Position -= b.Velocity;

With

a.Position -= normalForceA;
b.Position -= normalForceB;

This assumes that CalculateNormalForce returns the length of the penetration of the objects. You may need to change - to + depending on your calculations. It makes objects stick together and not penetrate each other. Using Velocity is not a bad idea, but if an object is moving too fast, it can have a big distance from another object and trigger a collision. With this, your objects will be next to each other when colliding.

Bounce

This is more realistic, but a little trickier to implement. Basically the idea is when an object is moving towards a direction, and it collides with another object, you calculate the distance that it travelled to reach that object. Most likely that distance is smaller than the object's velocity (otherwise there wouldn't be a collision) in which case the remaining velocity is applied in a "mirrored" direction.

enter image description here

Update:

If you are dealing with primarily objects that do not rotate (so they have fixed axis) one suggestion I can give would be (based on the video):

if (collide(a, b)) {

    b.position = a.top +b.height/2;

}

This assumes that a is a static object (in your case the ground) and b is the box falling on it. When collision happens, just move the box on top of the ground. I know this can't really apply to everything, but maybe it can help you find a better solution.

\$\endgroup\$
  • \$\begingroup\$ I gave it a shot, it seems that the objects penetrate and bounce for a little bit, then stabilise. \$\endgroup\$ – Mathew O'Dwyer Apr 13 '18 at 11:58
  • \$\begingroup\$ Could you share a gif or video perhaps? Maybe I missed something. Also I'm not sure how accurate CalculateNormalForce is, if it returns a number slightly bigger than the actual penetration distance, it would cause exactly the effect you described. \$\endgroup\$ – TomTsagk Apr 13 '18 at 12:00
  • \$\begingroup\$ CalculateNormalForce returns a Vector2 which is the bodies mass * gravity.xy. I'll upload a video now, Also if I just remove the two lines I replaced, it gives me the same result. \$\endgroup\$ – Mathew O'Dwyer Apr 13 '18 at 12:02
  • \$\begingroup\$ youtu.be/9TD2JtO4qms for some reason I'm not able to tag you, hoping you see this. \$\endgroup\$ – Mathew O'Dwyer Apr 13 '18 at 12:08
  • \$\begingroup\$ @MathewO'Dwyer Can you try removing the 2 lines of a.ApplyForce(-normalForceA * 11); and b.ApplyForce(-normalForceB * 11); ? I think that's what causing the bounciness \$\endgroup\$ – TomTsagk Apr 13 '18 at 12:16

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.