public void resolveCollision(Pinball pinball) throws Exception
    {
        int startPoint = -1, endPoint = -1;
        for (int i = 0; i < 361; i++)
        {
            if (getMapVal(Constants.toInt(pinball.position.getX() + pinball.getRadius() * Math.sin(Math.toRadians(i))),
                    Constants.toInt(pinball.position.getY() + pinball.getRadius() * Math.cos(Math.toRadians(i)))) && startPoint == -1)
            {
                startPoint = i;
            }

        }

        if (startPoint == -1) return;
        for (int i = startPoint; i < 361; i++)
        {
            if (!getMapVal(Constants.toInt(pinball.position.getX() + pinball.getRadius() * Math.sin(Math.toRadians(i))),
                    Constants.toInt(pinball.position.getY() + pinball.getRadius() * Math.cos(Math.toRadians(i)))) && endPoint == -1)
            {
                endPoint = i;
            }
        }

        int normalAngle;
        if (endPoint > -1)
        {
            normalAngle = (startPoint + endPoint) / 2;

        }
        else
        {

            normalAngle = startPoint;
        }

        Vector2D normalVector = new Vector2D(Constants.toInt(pinball.velocity.getX() + pinball.getRadius() * Math.sin(Math.toRadians(normalAngle))),
                Constants.toInt(pinball.velocity.getY() + pinball.getRadius() * Math.cos(Math.toRadians(normalAngle))));

        Vector2D newVector = new Vector2D(Constants.bounceFactor * (-2 * (pinball.velocity.dotProd(normalVector) * normalVector.getX() + pinball.velocity.getX())),
                Constants.bounceFactor * (-2 * (pinball.velocity.dotProd(normalVector) * normalVector.getY() + pinball.velocity.getY())));

        pinball.velocity = newVector;
        pinball.velocity.normalise();
        if (pinball.velocity.magSq() > Constants.MAX_SPEED * Constants.MAX_SPEED)
        {
            pinball.velocity.mult(Constants.MAX_SPEED);
        }
        pinball.update();
    }