Uhm, If I remember correctly the meat and bones of the GetBodyAtMouse() function either came from the downloadable example, or from Emanuele Feronato's site..
It basically works by drawing a small (like 0.1 x 0.1) immaginary square and checking what's overlapping it...
It can be used to return multiple objects, but this one I'm pasting has obviously been tweaked to just
return the first.
Code:
public function GetBodyAtMouse(includeStatic:Boolean ):b2Body {
mouseXWorldPhys = mouseX / 30;
mouseYWorldPhys = mouseY / 30;
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.01, mouseYWorldPhys - 0.01);
aabb.upperBound.Set(mouseXWorldPhys + 0.01, mouseYWorldPhys + 0.01);
// Query the world for overlapping shapes.
var k_maxCount:int = 10;
var shapes:Array = new Array();
var count:int = m_world.Query(aabb, shapes, k_maxCount);
var body:b2Body = null;
for (var i:int = 0; i < count; ++i)
{
if (shapes[i].GetBody().IsStatic() == false || includeStatic)
{
var tShape:b2Shape = shapes[i] as b2Shape;
var inside:Boolean = tShape.TestPoint(tShape.GetBody().GetXForm(), mousePVec);
if (inside)
{
body = tShape.GetBody();
break;
}
}
}
return body;
}// GetBodyAtMouse
So you can obviously just do a little modification.. i.e.
Code:
public function getCloseBodies(targBody:b2Body){
if (targBody != null){
mouseXWorldPhys = targBody.GetPosition().x;
mouseYWorldPhys = targBody.GetPosition().y;
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.5, mouseYWorldPhys - 0.5);
aabb.upperBound.Set(mouseXWorldPhys + 0.5, mouseYWorldPhys + 0.5);
}
}
The advantage to doing it this way is that it can be slightly faster, with loads of objects
and only 2-3 important collisions, and you can increase the bounds slightly i.e. if the user
has to cick two objects that are *REALLY* close and appear touching, but aren't touching.
The disadvantage, I'm guessing it's less lightweight overall, so use sparingly, and the
collision detection doesnt take rotation into account.
Either way, have fun fiddling with it
🙂Here's a few links I found:
http://wonderfl.net/search?page=3&q=b2MouseJointDefhttp://wonderfl.net/search?q=body2&search_order=newhttp://www.emanueleferonato.com/2008/11 ... x2d-flash/