I am struggling with a prototype method that needs to be properly invoked by a callback from the Box2D API. The issue I am facing is that the callback can access the function, but it does not refer to the correct instance of the function. I need help with finding the right syntax to ensure that the callback correctly calls the this.prototype class.
Here is the code snippet that calls the Box2D API:
Box2d.prototype.getBodyAtMouse = function() {
this.mousePVec = new this.b2Vec2(this.mouseX/this.SCALE, this.mouseY/this.SCALE);
var aabb = new this.b2AABB();
aabb.lowerBound.Set(this.mouseX/this.SCALE - 0.001, this.mouseY/this.SCALE - 0.001);
aabb.upperBound.Set(this.mouseX/this.SCALE + 0.001, this.mouseY/this.SCALE + 0.001);
this.selectedBody = null;
var _this = this;
this.world.QueryAABB(_this.getBodyCB, aabb);
return this.selectedBody;
}
Next, we have the Box2D API method that is being called:
b2World.prototype.QueryAABB = function (callback, aabb) {
var __this = this;
var broadPhase = __this.m_contactManager.m_broadPhase;
function WorldQueryWrapper(proxy) {
return callback(broadPhase.GetUserData(proxy));
};
broadPhase.Query(WorldQueryWrapper, aabb);
}
Finally, we have the callback function that I need the API to reference correctly:
Box2d.prototype.getBodyCB = function(fixture)
{
if(fixture.GetBody().GetType() != this.b2Body.b2_staticBody) {
if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), this.mousePVec)) {
selectedBody = fixture.GetBody();
return false;
}
}
return true;
}