Hello!
I’ve started playing around with Box2D and I’ve encountered a problem I can’t figure out.
I got 4 walls and a point that’s floating around on the stage searching for collisions with the walls.
Here is the code that’s creating the walls:
public function add_map():void {
var wallSize:int = 5;
for (var k:uint = 0; k < 4; k++) {
var my_body:b2BodyDef = new b2BodyDef();
my_body.type = b2Body.b2_staticBody;
var my_box:b2PolygonShape = new b2PolygonShape();
switch(k) {
case 0:
[COLOR=#008000]// Top[/COLOR]
my_body.position.Set((stage.stageWidth/2) / world_scale, (wallSize/2) / world_scale);
my_box.SetAsBox((stage.stageWidth/2)/world_scale, wallSize/world_scale);
break;
case 1:
[COLOR=#008000]// Right[/COLOR]
my_body.position.Set((stage.stageWidth-wallSize) / world_scale, (wallSize/2) / world_scale);
my_box.SetAsBox(wallSize/world_scale, (stage.stageHeight - (wallSize/2))/world_scale);
break;
case 2:
[COLOR=#008000]// Bot[/COLOR]
my_body.position.Set((stage.stageWidth/2) / world_scale, (stage.stageHeight-wallSize) / world_scale);
my_box.SetAsBox((stage.stageWidth/2)/world_scale, wallSize/world_scale);
break;
case 3:
[COLOR=#008000]// Left[/COLOR]
my_body.position.Set((wallSize/2) / world_scale, (wallSize/2) / world_scale);
my_box.SetAsBox(wallSize/world_scale, (stage.stageHeight - (wallSize/2))/world_scale);
break;
}
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_fixture.shape = my_box;
my_fixture.density = 100.0;
my_fixture.friction = 10;
var body:b2Body = world.CreateBody(my_body);
body.CreateFixture(my_fixture);
[COLOR=#b22222]mapParts.push(my_body);[/COLOR]
}
}
And here’s the code I’m using to determine if there’s any collisions going on
var objects:Array = getObjectsUnderPoint(thePoint);
for (var k:uint = 0; k < objects.length; k++) {
for (var i:uint = 0; i < mapParts.length; i++) {
if (objects[k] == [COLOR=#b22222]mapParts*[/COLOR]) {
trace("Wall detected");
}
}
}
I think that the problem is that **my_body *from the addMap()-function isn’t the real object that’s visible on the stage(so I can use getObjectUnderPoint to check if it’s colliding)
(Marked with red color)
*
*Thanks in advance,
Tompa
(Title was meant to be named ‘HitTestPoint on a Box2D-object’)*