I'm making a Flash game, and I've encountered a really weird problem while trying to draw a polygonal shape in Box2D.
Here's the code I use:
var fixtureDefs:Array = new Array();
...
var fDef:b2FixtureDef = new b2FixtureDef();
fDef.density = 0;
fDef.shape = new b2PolygonShape();
b2PolygonShape(fDef.shape).SetAsArray(vertexArray);
fixtureDefs.push(fDef);
//This gets repeated several times, so that at the end you get a body consisting of several convex shapes.
...
var bD:b2BodyDef = new b2BodyDef();
bD.type =开发者_如何学C b2Body.b2_staticBody;
bD.position.Set(300/Constants.RATIO,200/Constants.RATIO);
var body:b2Body = Constants.world.CreateBody(bD);
...
for each(var fD:b2FixtureDef in fixtureDefs) {
body.CreateFixture(fD);
}
Where vertexArray is a valid array containing 4 b2Vec2 vertices, making up a convex shape.
The problem is, when I test, collisions don't work right for that body. Most other objects -enemies, user-controlled characters - pass straight through, as if the body isn't there at all. Some raycasts pass through as well.
Infuriatingly enough, one kind of bodies I have (a custom enemy) somehow does detect the body and collides with it. The raycasts that particular kind of enemy attempts do work - when your character hides behind the polygon, it's like they can't see him.
The other weird thing: when I try the same code, only go for SetAsBox instead of SetAsArray, it works exactly as it should.
I'm using a custom ContactListener class, but I haven't done any contact filtering (unless it's possible to do that without realising).
I'm using the Flash 9 version of Box2D 2.1a.
Any suggestions? Am I missing something obvious or have I (God forbid!) discovered a bug? Thanks for your help!
Andrey
Heyo Andrey,
2.1a is more on the strict side so you have to declare almost everything for it to work proper.
AS3 OOP-
private var body:b2Body;
private var bodyDef:b2BodyDef = new b2BodyDef();
private var bodyPoly:b2PolygonShape = new b2PolygonShape();
private var bodyFix:b2FixtureDef = new b2FixtureDef();
private var vertexArray:Array = new Array();
public function Example()
{
//Setting up Vertices in an Array
var ver1:b2Vec2 = new b2Vec2( -1, -1);
var ver2:b2Vec2 = new b2Vec2( 1, -1);
var ver3:b2Vec2 = new b2Vec2( 1, 1);
var ver4:b2Vec2 = new b2Vec2( -1, 1);
//Push in order
vertexArray.push(ver1, ver2, ver3, ver4);
bodyDef.type = b2Body.b2_dynamicBody;
//b2Vec2 Array then Vertex Count
bodyPoly.SetAsArray(vertexArray, vertexArray.length);
bodyFix.shape = bodyPoly;
bodyFix.density = 0.5;
bodyFix.friction = 0.5;
bodyFix.restitution = 0.5;
bodyDef.position.Set(0, 0);
body = m_world.CreateBody(bodyDef);
body.CreateFixture(bodyFix);
}
Remember that each point must be to the right of the one before.
var ver1:b2Vec2 = new b2Vec2( -1, -1);//Top Left
var ver2:b2Vec2 = new b2Vec2( 1, -1);//Top Right
var ver3:b2Vec2 = new b2Vec2( 1, 1);//Bottom Right
var ver4:b2Vec2 = new b2Vec2( -1, 1);//Bottom Left
Placement from left to right
vertexArray.push(ver1, ver2, ver3, ver4);
I'm not very good at explaining thing's but i hope this help's! -Zero
Heyo again, As most of the version's of Box2D were written by different people the source change's with it, to better suit the programming language's, the documentation you were looking at probably referred to a different language then AS3 most likely C++.
Your source should have come with a folder with the documentation for that version in it, should be a folder named 'Docs'.
精彩评论