简体   繁体   English

在iOS版box2d中,如何找到两个圆形物体之间的距离?

[英]In box2d for iOS, how to find the distance between two circular bodies?

I am using the following code to get the distance between two circular bodies of different radius: 我正在使用以下代码来获取不同半径的两个圆形物体之间的距离:

distance = b2Distance(body1->GetPosition(), body2->GetPosition());

I have realized that variable distance is storing the distance between the two centers of the bodies, but not the distance between the borders. 我已经意识到,可变距离存储的是物体两个中心之间的距离,而不是边界之间的距离。 What I want is distance=0 when the two bodies are touching. 我想要的是两个物体接触时,距离= 0。

How can I do that? 我怎样才能做到这一点? I've been trying this code but it fails: 我一直在尝试这段代码,但失败了:

b2DistanceInput *distanceInput;
distanceInput->transformA = body1->GetTransform();
distanceInput->transformB = body2->GetTransform();
b2DistanceProxy *proxyA;
proxyA->Set(fixtureBody1->GetShape(), 1);
b2DistanceProxy *proxyB;
proxyB->Set(fixtureBody2->GetShape(), 1);
distanceInput->proxyA = *proxyA;
distanceInput->proxyB = *proxyB;
b2DistanceOutput *theDistance;
b2SimplexCache *cache;
cache->count = 0;
b2Distance(theDistance, cache, distanceInput);

The getShape method is giving a bad access error within the b2box code. getShape方法在b2box代码内提供了错误的访问错误。

Any ideas? 有任何想法吗?

Thanks, 谢谢,

GA GA

Try use this code - it work for me: 尝试使用此代码-适用于我:

b2DistanceInput *distanceInput = new b2DistanceInput();
b2DistanceProxy *proxyA = new b2DistanceProxy();
b2DistanceProxy *proxyB = new b2DistanceProxy();
b2SimplexCache *cache = new b2SimplexCache();
b2DistanceOutput *theDistance = new b2DistanceOutput();

proxyA->Set(fixtureBody1->GetShape(),1);
proxyB->Set(fixtureBody2->GetShape(),1);

distanceInput->transformA = body1->GetTransform();
distanceInput->transformB = body2->GetTransform();
distanceInput->proxyA = *proxyA;
distanceInput->proxyB = *proxyB;
distanceInput->useRadii = true;

cache->count = 0;

b2Distance(theDistance, cache, distanceInput);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM