I am controlling a box2D application with my Iphone.
As the iphone is a multitouch screen, you may have several inputs, unlike the computer mouse.
I box2D multi touchable ?
My mouseJoints only work with one user Input, but all goes crazy when there are several inputs.
it doesn't work with this (contact is the position, I get from the iphone, which replaces mouseX, mouseY):
What am I doing wrong here ?
public function checkContact(contact, id:Number):void {
var body:b2Body=GetBodyAtMouse(contact);
if (body /* && (body.GetUserData() is PlayerActor */ ) {
if (PlayerActor(body.GetUserData()).waiting) {
PlayerActor(body.GetUserData()).waiting = false;
PlayerActor(body.GetUserData()).onClick(new MouseEvent(MouseEvent.CLICK));
}
var mouseJointDef:b2MouseJointDef=new b2MouseJointDef;
mouseJointDef.body1=PhysiVals.world.GetGroundBody();
mouseJointDef.body2=body;
mouseJointDef.target.Set(contact.x/PhysiVals.RATIO, contact.y/PhysiVals.RATIO);
mouseJointDef.maxForce=30000;
mouseJointDef.timeStep=PhysiVals.FRAME_RATE;
mouseJointArr[id]=PhysiVals.world.CreateJoint(mouseJointDef) as b2MouseJoint;
}
}
public function destroyContact(id:Number):void {
if (mouseJointArr[id]) {
PhysiVals.world.DestroyJoint( mouseJointArr[id] );
mouseJointArr[id] = null;
}
}
public function GetBodyAtMouse(contact,includeStatic:Boolean=false):b2Body {
var mouseXWorldPhys:Number = contact.x/PhysiVals.RATIO;
var mouseYWorldPhys:Number = contact.y/PhysiVals.RATIO;
trace ("mouseXWorldPhys",mouseXWorldPhys);
_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 k_maxCount:int=10;
var shapes:Array = new Array();
var count:int=PhysiVals.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;
}
As the iphone is a multitouch screen, you may have several inputs, unlike the computer mouse.
I box2D multi touchable ?
My mouseJoints only work with one user Input, but all goes crazy when there are several inputs.
it doesn't work with this (contact is the position, I get from the iphone, which replaces mouseX, mouseY):
What am I doing wrong here ?
public function checkContact(contact, id:Number):void {
var body:b2Body=GetBodyAtMouse(contact);
if (body /* && (body.GetUserData() is PlayerActor */ ) {
if (PlayerActor(body.GetUserData()).waiting) {
PlayerActor(body.GetUserData()).waiting = false;
PlayerActor(body.GetUserData()).onClick(new MouseEvent(MouseEvent.CLICK));
}
var mouseJointDef:b2MouseJointDef=new b2MouseJointDef;
mouseJointDef.body1=PhysiVals.world.GetGroundBody();
mouseJointDef.body2=body;
mouseJointDef.target.Set(contact.x/PhysiVals.RATIO, contact.y/PhysiVals.RATIO);
mouseJointDef.maxForce=30000;
mouseJointDef.timeStep=PhysiVals.FRAME_RATE;
mouseJointArr[id]=PhysiVals.world.CreateJoint(mouseJointDef) as b2MouseJoint;
}
}
public function destroyContact(id:Number):void {
if (mouseJointArr[id]) {
PhysiVals.world.DestroyJoint( mouseJointArr[id] );
mouseJointArr[id] = null;
}
}
public function GetBodyAtMouse(contact,includeStatic:Boolean=false):b2Body {
var mouseXWorldPhys:Number = contact.x/PhysiVals.RATIO;
var mouseYWorldPhys:Number = contact.y/PhysiVals.RATIO;
trace ("mouseXWorldPhys",mouseXWorldPhys);
_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 k_maxCount:int=10;
var shapes:Array = new Array();
var count:int=PhysiVals.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;
}