在 Box2D 中绘制自定义多边形

发布于 2024-11-01 13:39:00 字数 1177 浏览 7 评论 0原文

我正在制作一个 Flash 游戏,在尝试在 Box2D 中绘制多边形形状时遇到了一个非常奇怪的问题。

这是我使用的代码:

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 = 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);
}

其中 vertexArray 是一个包含 4 个 b2Vec2 顶点的有效数组,构成一个凸形状。

问题是,当我测试时,碰撞对该物体不起作用。大多数其他物体——敌人、用户控制的角色——直接穿过,就好像身体根本不存在一样。一些光线投射也会穿过。

令人气愤的是,我拥有的一种身体(自定义敌人)确实以某种方式检测到身体并与之碰撞。特定类型的敌人尝试进行的光线投射确实起作用 - 当你的角色隐藏在多边形后面时,就好像他们看不到他一样。

另一个奇怪的事情:当我尝试相同的代码时,只选择 SetAsBox 而不是 SetAsArray,它的工作原理完全正确。

我正在使用自定义 ContactListener 类,但我没有进行任何联系人过滤(除非可以在没有意识到的情况下做到这一点)。

我使用的是 Box2D 2.1a 的 Flash 9 版本。

有什么建议吗?我是否遗漏了一些明显的东西或者我(上帝禁止!)发现了一个错误?感谢您的帮助!

安德烈

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 = 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-11-08 13:39:00

Heyo Andrey,

2.1a 更加严格,因此您必须声明几乎所有内容才能正常工作。

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);

}

请记住每个点必须位于前一个点的右侧。

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

从左到右的放置

vertexArray.push(ver1, ver2, ver3, ver4);

我不太擅长解释事情,但我希望这有帮助! -零

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

你另情深 2024-11-08 13:39:00

又嗨了,
由于 Box2D 的大多数版本都是由不同的人编写的,因此源代码也随之变化,为了更好地适应编程语言,您正在查看的文档可能引用了与 AS3 不同的语言,最有可能是 C++。

您的源应该附带一个包含该版本文档的文件夹,应该是一个名为“Docs”的文件夹。

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文