b2shape 椭圆?

发布于 2024-12-05 03:13:56 字数 171 浏览 1 评论 0原文

我正在尝试在 box2D (Cocos2D) 中创建一个椭圆形对象。到目前为止,我已经使用 b2CircleShape 来实现这一点,但我意识到它不会再切割它了,我必须有椭圆形的身体。有可能吗?我尝试过 b2PolygonShape 但我的边缘是线性的,我需要它们是弯曲的。

有人遇到同样的问题吗?有什么建议吗?

I'm trying to create a elliptical object in box2D (Cocos2D). So far I've used b2CircleShape for that but I've realized that it ain't gonna cut it no more, I have to have elliptically shaped body. Is it posible? I've tried with b2PolygonShape but than my edges are linear and I need them to be curved.

Anybody had a same problem? Any suggestions?

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

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

发布评论

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

评论(2

蓝礼 2024-12-12 03:13:56

您可以尝试使用多边形形状并从线段创建“椭圆”:椭圆肯定会是凸多边形,并且您可以添加合理数量的线段。这只是一个近似值,但您可以稍后微调分段数量,以便在性能和原始形状的近似值之间获得最佳比例。

You can try with a polygon shape and create the "ellipse" from segments: the ellipse will be a convex polygon for sure, and you can add a reasonable number of segments. This will be just an approximation, but you can fine tune later the number of segments to give you the best ration between performance and approximation to the original shape.

揽月 2024-12-12 03:13:56

我也用过近似法。这有一些性能缺陷,但我想没什么大不了的。代码(Flash ActionScript 3,但您应该能够轻松移植):

var vertices:Vector.<b2Vec2> = new Vector.<b2Vec2>();
var a:Number = _image.width / 2 / PhysicsVals.RATIO;
var b:Number = _image.height / 2 / PhysicsVals.RATIO;
var segments:int = ellipse_approximation_vertices_count; (the more the more precise shape is, but the more time it takes to do collision detection)

var segment:Number = 2 * Math.PI / segments;

for (var i:int = 0; i < segments; i++)
{
    vertices.push(new b2Vec2(a * Math.cos(segment * i), b * Math.sin(segment * i)));
}

var shape:b2PolygonShape = new b2PolygonShape();
shape.SetAsVector(vertices, vertices.length);

var fixtureDef:b2FixtureDef = new b2FixtureDef();       
fixtureDef.shape = shape;

I have used approximation as well. This has some performance drawbacks, but nothing major I guess. Code (Flash ActionScript 3, but you should be able to port that easily):

var vertices:Vector.<b2Vec2> = new Vector.<b2Vec2>();
var a:Number = _image.width / 2 / PhysicsVals.RATIO;
var b:Number = _image.height / 2 / PhysicsVals.RATIO;
var segments:int = ellipse_approximation_vertices_count; (the more the more precise shape is, but the more time it takes to do collision detection)

var segment:Number = 2 * Math.PI / segments;

for (var i:int = 0; i < segments; i++)
{
    vertices.push(new b2Vec2(a * Math.cos(segment * i), b * Math.sin(segment * i)));
}

var shape:b2PolygonShape = new b2PolygonShape();
shape.SetAsVector(vertices, vertices.length);

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