问题
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.
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.
Any ideas?
Thanks,
GA
回答1:
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);
来源:https://stackoverflow.com/questions/20993527/in-box2d-for-ios-how-to-find-the-distance-between-two-circular-bodies