Reputation: 1470
What is the best practice to find the closest body?
Iterating through the box2d world? This seems cpu intensive.
Try to find overlappings with a body that gets bigger until something is found?
Does Box2d offer a direct way to find the closest body?
Upvotes: 0
Views: 399
Reputation: 6057
As far as I know there is no native box2d way to find the closest body. If it were so, box2d also would have to iterate over all bodies internally. It depends on your specific setting if it is more effective to narrow your search on a specific area at first (by a QueryAABB or something) to limit the number of bodies you have to iterate over, but as you did not write something about your intentions and your setting I cannot give you a more specific advice here. Keep in mind that you only need to calculate and compare the square distance of the bodies to your position.
Upvotes: 1