在iOS2的box2d中,如何找到两个圆形体之间的距离? [英] In box2d for iOS, how to find the distance between two circular bodies?
本文介绍了在iOS2的box2d中,如何找到两个圆形体之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码来获取两个不同半径的圆形体之间的距离:
I am using the following code to get the distance between two circular bodies of different radius:
distance = b2Distance(body1->GetPosition(), body2->GetPosition());
我已经意识到可变距离存储了两个身体中心之间的距离,但不是边界之间的距离。我想要的是当两个身体接触时距离= 0.
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.
我该怎么做?我一直在尝试这个代码,但它失败了:
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);
getShape方法在b2box代码中提供了错误的访问错误。
The getShape method is giving a bad access error within the b2box code.
任何想法?
谢谢,
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);
这篇关于在iOS2的box2d中,如何找到两个圆形体之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文