菜鸟教程小白 发表于 2022-12-13 00:13:42

iphone - box2d 凹体


                                            <p><p>我正在为我的 iOS 游戏创建一个 box2d 主体,它由 4 个凸形形状构成。
问题是调用init方法时失败。
这是我的代码:</p>

<pre><code>@implementation Banan
-(void)createBodyAtLocation:(CGPoint)location
{
    int num;
    float density = 1.0f;

    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position = b2Vec2(location.x/PTM_RATIO/RETSIZE, location.y/PTM_RATIO/RETSIZE);
    body = world-&gt;CreateBody(&amp;bodyDef);
    body -&gt; SetUserData(self);

    //first shape
    b2FixtureDef fixtureDef1;
    b2PolygonShape shape1;
    num = 4;
    b2Vec2 verts1;
      verts1.Set(58.9f / PTM_RATIO, 46.3f / PTM_RATIO);
         verts1.Set(63.3f / PTM_RATIO, 41.5f / PTM_RATIO);
      verts1.Set(47.4f / PTM_RATIO, 15.6f / PTM_RATIO);
         verts1.Set(43.6f / PTM_RATIO, 24.3f / PTM_RATIO);
    shape1.Set(verts1, num);
    fixtureDef1.shape = &amp;shape1;
    fixtureDef1.density = density;


    //second
    b2FixtureDef fixtureDef2;
    b2PolygonShape shape2;
    num = 5;
    b2Vec2 verts2;
    verts2.Set(42.1f / PTM_RATIO, 21.7f / PTM_RATIO);
    verts2.Set(46.6f / PTM_RATIO, -0.1f / PTM_RATIO);
    verts2.Set(29.1f / PTM_RATIO, -32.2f / PTM_RATIO);
    verts2.Set(2.5f / PTM_RATIO, -45.2f / PTM_RATIO);
    verts2.Set(6.8f / PTM_RATIO, -10.4f / PTM_RATIO);
    shape2.Set(verts2, num);
    fixtureDef2.shape = &amp;shape2;
    fixtureDef2.density = density;

    //third
    b2FixtureDef fixtureDef3;
    b2PolygonShape shape3;
    num = 4;
    b2Vec2 verts3;
    verts3.Set(5.6f / PTM_RATIO, -9.7f / PTM_RATIO);
    verts3.Set(-0.3f / PTM_RATIO, -45.7f / PTM_RATIO);
    verts3.Set(-32.7f / PTM_RATIO, -41.2f / PTM_RATIO);
    verts3.Set(-28.2f / PTM_RATIO, -15.7f / PTM_RATIO);
    shape3.Set(verts3, num);
    fixtureDef3.shape = &amp;shape3;
    fixtureDef3.density = density;

    //fourth
    b2FixtureDef fixtureDef4;
    b2PolygonShape shape4;
    num = 4;
    b2Vec2 verts4;
    verts4.Set(-28.7f / PTM_RATIO, -14.8f / PTM_RATIO);
    verts4.Set(-40.8f / PTM_RATIO, -36.0f / PTM_RATIO);
    verts4.Set(-60.5f / PTM_RATIO, -2.4f / PTM_RATIO);
    verts4.Set(-58.0f / PTM_RATIO, 2.9f / PTM_RATIO);
    shape4.Set(verts3, num);
    fixtureDef4.shape = &amp;shape4;
    fixtureDef4.density = density;

    //attach to shape
    body-&gt;CreateFixture(&amp;fixtureDef1);
    body-&gt;CreateFixture(&amp;fixtureDef2);
    body-&gt;CreateFixture(&amp;fixtureDef3);
    body-&gt;CreateFixture(&amp;fixtureDef4);
}
- (id)initWithWorld:(b2World *)tworld atLocation:(CGPoint)location {
    if ((self = )) {
      world = tworld;
      ;
      ;
    }
    return self;
}


-(void)dealloc
{
    ;
}
@end
</code></pre>

<p>'-initWithWorld: atLocation:' 方法由 ccTouchBegan 方法调用,然后终止我的应用程序。
错误如下:</p>

<p>断言失败:(区域> 1.19209290e-7F),函数ComputeCentroid,...</p>

<pre><code>-(void)createBananaAtLocation:(CGPoint)location
{
    Banan *ban = [[ initWithWorld:world atLocation:location] autorelease];//fails at this line when is invoked
    ;

}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchLocation = ];
    touchLocation = [
                     convertToGL:touchLocation];
    touchLocation = ;
    b2Vec2 locationWorld =
    b2Vec2(touchLocation.x/PTM_RATIO, touchLocation.y/PTM_RATIO);

    ; //error at this line
    return YES;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在您的情况下,顶点是顺时针方向,而 b2Polygon 要求是逆时针方向。
它导致计算多边形的负面积。您必须更改顶点的顺序。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - box2d 凹体,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/14084255/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/14084255/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - box2d 凹体