0

Here's the prolem

enter image description here

If I define the baseball bat with SetAsBox the no problem.

This kind of weird happens when I define it using SetAsVector

this.view = new createjs.Bitmap("hammer.png");
this.view.regX = 0;
this.view.regY = 0;

var fixDef = new box2d.b2FixtureDef();
fixDef.density = 5.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.8;
var bodyDef = new box2d.b2BodyDef();
bodyDef.type = box2d.b2Body.b2_kinematicBody;
fixDef.shape = new box2d.b2PolygonShape();
var vertices = []
vertices.push(new box2d.b2Vec2(30/SCALE, 0/SCALE));
vertices.push(new box2d.b2Vec2(14/SCALE, 0/SCALE));
vertices.push(new box2d.b2Vec2(17/SCALE, 10/SCALE));
vertices.push(new box2d.b2Vec2(17/SCALE, 100/SCALE));
vertices.push(new box2d.b2Vec2(0/SCALE, 290/SCALE));
vertices.push(new box2d.b2Vec2(12/SCALE, 300/SCALE));
vertices.push(new box2d.b2Vec2(32/SCALE, 300/SCALE));
vertices.push(new box2d.b2Vec2(44/SCALE, 290/SCALE));
vertices.push(new box2d.b2Vec2(27/SCALE, 100/SCALE));
vertices.push(new box2d.b2Vec2(27/SCALE, 10/SCALE));
fixDef.shape.SetAsVector(vertices, 10);
bodyDef.position.x = 600 / SCALE;
bodyDef.position.y = 300 / SCALE;
this.view.body =  world.CreateBody(bodyDef);
this.view.body.CreateFixture(fixDef);

Anyone know about this? Thanks.

jvperrin
  • 3,368
  • 1
  • 23
  • 33
Thịnh Phạm
  • 2,528
  • 5
  • 26
  • 39

1 Answers1

0

The method SetAsVector expects a convex polygon. But I think what you are trying to set here is a concave one.

Newer versions of Box2D support generating convex polygons out of a concave ones, but I don't think Box2DWeb supports this.

You can solve this by splitting your concave polygon into two convex polygons and creating two fixtures instead of one.

Wikipedia article about convex and concave polygons

Dennis Korbar
  • 500
  • 2
  • 7