Cube sphere intersection test?

后端 未结 3 545
鱼传尺愫
鱼传尺愫 2020-12-14 12:47

What\'s the easiest way of doing this? I fail at math, and i found pretty complicate formulaes over the internet... im hoping if theres some simpler one?

I just need

3条回答
  •  旧时难觅i
    2020-12-14 13:38

    // Assume clampTo is a new value. Obviously, don't move the sphere
    closestPointBox = sphere.center.clampTo(box)
    
    isIntersecting = sphere.center.distanceTo(closestPointBox) < sphere.radius
    

    Everything else is just optimization.

    Wow, -2. Tough crowd. Ok, here's the three.js implementation that basically says the same thing word for word. https://github.com/mrdoob/three.js/blob/dev/src/math/Box3.js

    intersectsSphere: ( function () {
    
        var closestPoint;
    
        return function intersectsSphere( sphere ) {
    
            if ( closestPoint === undefined ) closestPoint = new Vector3();
    
            // Find the point on the AABB closest to the sphere center.
            this.clampPoint( sphere.center, closestPoint );
    
            // If that point is inside the sphere, the AABB and sphere intersect.
            return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
    
        };
    
    } )(),
    

提交回复
热议问题