8

I'm trying to find other methods of registering collisions (other than OnCollisionEnter() and OnCollisionExit()). I'm currently using Physics.OverlapBox(), but I need more information about the collision; i.e., normal, point.

I can use Physics.BoxCast(), but the problem is that it moves a box a given distance, and using maxDistance = 0f won't work.

I need a method of checking for collisions similar to Physics.OverlapBox() except in that it would also return information about all collisions in the cast.

Any help is appreciated. Thanks.

blackhole
  • 137
  • 4
  • 15

2 Answers2

4

You can use OverlapBox and use Collider's ClosestPoint to select a single point of overlap, and use that to make your collision calculations.

Collider[] cols = Physics.OverlapBox(...);
Vector3 myPosition = transform.position; // for example

foreach (Collider col in cols) {
    Vector3 closestPoint = col.ClosestPoint(myPosition);
    Vector3 positionDifference = (closestPoint-myPosition);
    Vector3 overlapDirection = positionDifference.normalized; 
}

This overlapDirection will point in the direction away from the the position you use in ClosestPoint to the center of each colliding collider. If you want something based on the surface of your object, what you can do is use that overlap direction to place a raycast aimed at your object, to find the normal that way:

// ...

foreach (Collider col in cols) {
    Vector3 closestPoint = col.ClosestPoint(myPosition);
    Vector3 positionDifference = (closestPoint-myPosition);
    Vector3 overlapDirection = positionDifference.normalized; 

    RaycastHit hit;
    int layerMask = 1;  // Set to something that will only hit your object
    float raycastDistance = 10.0; // something greater than your object's largest radius, 
                                  // so that the ray doesn't start inside of your object
    Vector3 rayStart = myPosition + overlapDirection * raycastDistance;
    Vector3 rayDirection = -overlapDirection ;

    if (Physics.Raycast(rayStart, rayDirection, out hit, Mathf.Infinity, layerMask)) {
        Debug.Log(hit.normal);
        Debug.Log(hit.position);
    } else {
        // The ray missed your object, somehow. 
        // Most likely it started inside your object 
        // or there is a mistake in the layerMask
    }
}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 2
    Thank you. I understand your approach, but there is a flaw-- imagine if the object was hit by a point on the surface of a collider, but another point on the same collider was closer to `myPosition` than the actual collision point. We would get false data about the collision's normal, position, etc. The only fix to this would be if we could *somehow* get the closest distance and direction between 2 colliders. – blackhole Sep 16 '18 at 04:06
4

Your concern, expressed in the comment to the first answer is valid, but the bad news is that there is no simple trick to go around it. What you should be looking for is called continuous collision detection with a simplified version described in my answer on a somewhat similar matter:

Basically, for each moving object in your scene you have to calculate moment of next collision within the fraction of the frame 0<t0<1, then advance positions to this moment t0 within the frame, update velocities due to collision and proceed further to the next collision t0<t1<1, until you reach time of tn=1 (end of frame), making sure you don't get stuck in a the middle of the frame due to rounding of calculation or "cornered" objects. For spherical colliders, that is usually done by using capsule vs capsule (for pairs of objects) intersection and capsule vs box for the boundaries.

In oppose to the simple engine from the answer I'm referring to, Unity has continuous collision detection. So you can enable continuous collisions and continuous dynamic which computationally is very expensive.

You can also try using RigidBody.SweepTest which returns the closest collision information. Notice that even though there is also RigidBody.SweepTestAll, it doesn't help much. Not only because it returns only first 128 collisions, but also because it doesn't process them as there is no reflection. For physically correct behaviour you have to do what described above - advance time till the first collision and update velocities. Either with the physics engine or by yourself. This is very costly and not many games are doing that even cheating by using simplified objects (spheres are the cheapest ones as two swept spheres are two capsules and their intersection is a relatively cheap calculation), but amount of steps, especially in the "cornered" case when objects have nowhere to go and therefore are constantly colliding could be very large and such cases happen more than one can expect.

For complex objects you unlikely can do better than SweepTest, unless you trigger it based on the simpler primitives, such as Physics.BoxCast or Physics.SphereCast. Again, even though there are Physics.BoxCastAll and Physics.SphereCastAll they are not particularly useful as only first collision is guaranteed to occur. Those xxxCastAll are the functions you wrote you were looking for, so give it a try, they might work well enough for your use case.

isp-zax
  • 3,833
  • 13
  • 21