0
\$\begingroup\$

I'm trying to determine the collision-direction between two objects. I'll be using this for various things such as creating blood-entities in a specific direction proportional to where the other entity is hitting an entity. What's the best/most efficient way to do this? I've been doing this but it seems unstable/not to be working.

Assuming both entities are center-pivoted, I use trigonometry to find the angle like; atan(b/a). For some reason the output angle seems to be pretty random.

Here's the method I made:

protected void checkCollisionDirection(Collision2D target)
    {
        Vector3 positionDifference = target.gameObject.transform.position - gameObject.transform.position;
        int quadrant = 0;
        float x = positionDifference.x;
        float y = positionDifference.y;
        float degrees = Mathf.Atan (y / x) * Mathf.Rad2Deg;

        if (positionDifference.x > 0 && positionDifference.y > 0)
        {
            quadrant = 1;
        }
        else if (positionDifference.x < 0 && positionDifference.y > 0)
        {
            quadrant = 2;
            degrees += 180;
        }
        else if (positionDifference.x < 0 && positionDifference.y < 0)
        {
            quadrant = 3;
            degrees += 180;
        }
        else if (positionDifference.x > 0 && positionDifference.y < 0)
        {
            quadrant = 4;
            degrees += 360;
        }
        hitDirectionDegrees = degrees;
    }

I'm open to any suggestions that can find an angle between two entities.

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

You just need the angle of collision? How about:

Vector2 collisionAngleVectorToFirstEntity (secondEntityPosition.x - firstEntityPosition.x,
    secondEntityPosition.y - firstEntityPosition.y);

I'm not familiar with Unity but that Vector is pointing from the second point to the first point so running that on collision gives you a angle although in the form of a vector. You might need to run something like below to get a float with your degrees.

float degrees = collisionAngleVector.angle();
float degrees = Vector2.toDegrees(collisionAngleVector);

If there is no build in functionality to find a angle in degrees from a Vector2 then you have to calculate it yourself. Luckely Java has an atan2 function since normal tangents only go from -90 to 90 degrees and you have to find out yourself to which "wind direction" you have to add that number based on the position of x and y within the vector.

float degrees = Math.atan2(collisionV.y/collisionV.x);
\$\endgroup\$
1
\$\begingroup\$

Instead of using Mathf.Atan(y / x), which can only return angles between [-90..90] degrees, then compensating for angles outside that range by adding 180 degrees, simply use Mathf.Atan2(y, x).

Atan2 is specifically made for the task you are solving. From the Mathf.Atan2 docs:

public static float Atan2(float y, float x);
Returns the angle in radians whose Tan is y/x.
Return value is the angle between the x-axis and a 2D vector starting at zero and terminating at (x,y).

The logic seems otherwise correct.

\$\endgroup\$
1
  • \$\begingroup\$ Thats what I said, A inversed tangent can only calculate a single quadrant. \$\endgroup\$
    – Madmenyo
    Jan 10, 2015 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .