如何计算Box2D/Farseer中一个点到一个点的距离?

How to calculate distance between a fixture and a point in Box2D/Farseer?

基本上,我有一个固定装置和一个点。我想知道它们之间的距离(不是它们中心之间的距离)。

我知道存在距离 API,但它只适用于 2 个灯具:/

更多的是你应该知道的数学知识。两个物体之间的距离是它们坐标之间的向量长度。 Farseer 使用 Xna 类型 Vector2 或 Vector3。只需减去两个所需的向量即可得到所需的向量,并通过相应向量类型上的方法得到 Length 。夹具的坐标在其Body.Position.

对于使用夹具形状到特定点的距离,只需从中创建假的 pointShape(根据需要使用最小半径的圆),然后使用距离 class。

float getDistance(Vector2 point, Fixture fixture)
{
    var proxyA = new DistanceProxy();
    proxyA.Set(fixture.Shape, 0);

    var proxyB = new DistanceProxy();
    // prepare point shape
    var pointShape = new CircleShape(0.0001, 1);
    proxyB.Set(pointShape, 0);

    Transform transformA;
    fixture.Body.GetTransform(out transformA);

    Transform transformB = new Transform();
    transformB.Set(point, 0);

    DistanceOutput distance;
    SimplexCache cache;

    FarseerPhysics.Collision.Distance.ComputeDistance(out distance, out cache,
                new FarseerPhysics.Collision.DistanceInput(){
                    UseRadii=true,
                    ProxyA=proxyA,
                    ProxyB=proxyB,
                    TransformA=transformA,
                    TransformB=transformB
                });
}