0
\$\begingroup\$

I am following this paper in trying to create elastic 2D collisions among two circles. The problem is that my end velocities are the same as my start velocities. I've looked over my code and don't see anything off.

This is what I am hoping to achieve:

enter image description here

Maybe a second pair of eyes will notice something obviously wrong:

function resolveCircleCollision(playerOne, playerTwo) {
    // http://www.imada.sdu.dk/~rolf/Edu/DM815/E10/2dcollisions.pdf

    var xDiff = playerTwo.objectData.pos.x - playerOne.objectData.pos.x;
    var yDiff = playerTwo.objectData.pos.y - playerOne.objectData.pos.y;
    var normalVector = new Vector(xDiff, yDiff);

    if(normalVector.mag() <= playerOne.objectData.radius + playerTwo.objectData.radius) {
        // Normal/tangent unit vectors
        var normalUnitVector = normalVector.normalized();
        var tangentUnitVector = new Vector(-1 * normalUnitVector.y,     normalUnitVector.x);

        // Resolve velocity vectors into normal/tangential components
        var playerOneNormalComponent = normalUnitVector.dot(playerOne.velocity);
        var playerOneTangentComponent = tangentUnitVector.dot(playerOne.velocity); // Same post collision
        var playerTwoNormalComponent = normalUnitVector.dot(playerTwo.velocity);
        var playerTwoTangentComponent = tangentUnitVector.dot(playerTwo.velocity); // Same post collision

        // Find normal components post collision
        var playerOneNormalComponentPrime = (playerOneNormalComponent * (playerOne.mass - playerTwo.mass) + (2 * playerTwo.mass * playerTwoNormalComponent)) / (playerOne.mass + playerTwo.mass);
        var playerTwoNormalComponentPrime = (playerTwoNormalComponent * (playerTwo.mass - playerOne.mass) + (2 * playerOne.mass * playerOneNormalComponent)) / (playerOne.mass + playerTwo.mass);

        // Convert the components into vectors
        var playerOneNormalVector = normalUnitVector.multiply(playerOneNormalComponentPrime);
        var playerOneTangentVector = tangentUnitVector.multiply(playerOneTangentComponent);
        var playerTwoNormalVector = normalUnitVector.multiply(playerTwoNormalComponentPrime);
        var playerTwoTangentVector = tangentUnitVector.multiply(playerTwoTangentComponent);

       // Add the two components and set them equal to the player velocities
        playerOne.velocity = playerOneNormalVector.add(playerOneTangentVector);
        playerTwo.velocity = playerTwoNormalVector.add(playerTwoTangentVector);
    }
}
\$\endgroup\$
  • \$\begingroup\$ You mean pre velocity of player one is equal to post velocity? \$\endgroup\$ – Hamza Hasan Dec 26 '15 at 4:47
  • \$\begingroup\$ playerOne.velocity pre-collision == playerOne.velocity post-collision and playerTwo.velocity pre-collision == playerTwo.velocity post-collision Hope that clears it up. \$\endgroup\$ – Moother Dec 26 '15 at 5:13
  • \$\begingroup\$ Your code is working fine at least in unity \$\endgroup\$ – Hamza Hasan Dec 26 '15 at 9:42
  • \$\begingroup\$ You can check this first object has mass = 1 and second has mass = 0.5. pre velocity of obj1 = (0.3,0.0) and pre velocity of obj2 = (0,0) and post velocity obj1 = (0.1,-0.1) and post velocity of obj2 = (0.3,0.2) \$\endgroup\$ – Hamza Hasan Dec 26 '15 at 10:46
  • 1
    \$\begingroup\$ Mannn, you grabbed my day.. I did convert ti into c# :P But one thing, this all code was written in the gameloop. \$\endgroup\$ – Hamza Hasan Dec 26 '15 at 20:59

Your Answer

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

Browse other questions tagged or ask your own question.