I am hopeing someone could explain the code below. First of all, I never seen a function nested within another function so I was a little surprise there. But I just want to interpret the code below and if someone could correct me or add on then that will be great.
First of all this code is meant to get a body located at the mouse position. So you can drag and drop or do what you like with the body. Any how, it first creates a new b2AABB method which I assume is a miniature version of the b2world except it has boundaries.
They then set a bounding box around the mouse position
I am assuming by the comments that QueryAABB() finds any or possibly just 1 body that intersects with the bounding box area. Once it finds it, it calls on the callback function GetBodyCallback.
GetBodyCallback then evaluates this body and makes sure its not a static body such as a ground or wall. I am not sure what TestPoint does but if all goes well then it sets the reference of the body into a variable called body. and that is how they selected the body.
I already got mouse interaction with the body working with my code. As of right now I am just skimming the code and trying to interpret how they actually did it.
Code:
public function GetBodyAtMouse(includeStatic:Boolean = false):b2Body {
// Make a small box.
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
var body:b2Body = null;
var fixture:b2Fixture;
// Query the world for overlapping shapes.
function GetBodyCallback(fixture:b2Fixture):Boolean
{
var shape:b2Shape = fixture.GetShape();
if (fixture.GetBody().GetType() != b2Body.b2_staticBody || includeStatic)
{
var inside:Boolean = shape.TestPoint(fixture.GetBody().GetTransform(), mousePVec);
if (inside)
{
body = fixture.GetBody();
return false;
}
}
return true;
}
world.QueryAABB(GetBodyCallback, aabb);
return body;
}
// Make a small box.
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
var body:b2Body = null;
var fixture:b2Fixture;
// Query the world for overlapping shapes.
function GetBodyCallback(fixture:b2Fixture):Boolean
{
var shape:b2Shape = fixture.GetShape();
if (fixture.GetBody().GetType() != b2Body.b2_staticBody || includeStatic)
{
var inside:Boolean = shape.TestPoint(fixture.GetBody().GetTransform(), mousePVec);
if (inside)
{
body = fixture.GetBody();
return false;
}
}
return true;
}
world.QueryAABB(GetBodyCallback, aabb);
return body;
}
First of all this code is meant to get a body located at the mouse position. So you can drag and drop or do what you like with the body. Any how, it first creates a new b2AABB method which I assume is a miniature version of the b2world except it has boundaries.
Quote:
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
They then set a bounding box around the mouse position
Code:
world.QueryAABB(GetBodyCallback, aabb);
I am assuming by the comments that QueryAABB() finds any or possibly just 1 body that intersects with the bounding box area. Once it finds it, it calls on the callback function GetBodyCallback.
GetBodyCallback then evaluates this body and makes sure its not a static body such as a ground or wall. I am not sure what TestPoint does but if all goes well then it sets the reference of the body into a variable called body. and that is how they selected the body.
I already got mouse interaction with the body working with my code. As of right now I am just skimming the code and trying to interpret how they actually did it.