b2shape椭圆?

时间:2011-09-19 14:30:02

标签: objective-c cocos2d-iphone box2d

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

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我也使用了近似值。这有一些性能上的缺点,但我猜没什么大不了的。代码(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;