歪斜或倾斜时如何碰撞固定装置?

时间:2013-12-26 11:26:58

标签: iphone objective-c cocoa-touch cocos2d-iphone box2d

我想碰撞vall并阻止但是当我通过两行代码时不能碰撞球和阻塞但是如果我取消注释两行代码,那么显示水平的块不会像斜率那样。

请检查我的系数:

    for(int i = 0; i < 4; i++) {



        static int padding=20;


        self.touchEnabled = YES;

        // Create block and add it to the layer
        CCSprite *block = [CCSprite spriteWithFile:@"block.png"];
        int xOffset = padding+block.contentSize.width/2 +  ((block.contentSize.width+padding)*i);
        block.position = ccp(xOffset, 250);
        block.tag = 2;
        [self addChild:block];

        // Create block body
        b2BodyDef blockBodyDef;
        blockBodyDef.type = b2_dynamicBody;   //b2_dynamicBody  ,b2_staticBody
        blockBodyDef.position.Set(xOffset/PTM_RATIO, 250/PTM_RATIO);
        blockBodyDef.userData = block;
        b2Body *blockBody = _world->CreateBody(&blockBodyDef);



        // Create block shape

//这两行产生问题如果我解锁两行,代码工作正常,但是所有4块只在一条线上显示水平,但球可以击中该块。我希望球击中阻挡并且阻挡将水平显示。

     //   b2PolygonShape blockShape;
    //    blockShape.SetAsBox(block.contentSize.width/PTM_RATIO/2,      block.contentSize.height/PTM_RATIO/2);




      //////////////////////////////  If I comment the these lines and uncomment above two line the block will show horizontally and ball can hit the block. But I want this code to show my block slope like. This is must. I want block will show slope and ball can hit the block.

        b2PolygonShape blockShape;
        int num = 3;
        b2Vec2 verts[] = {
            b2Vec2(27.5f / PTM_RATIO, 296.6f / PTM_RATIO),
            b2Vec2(14.3f / PTM_RATIO, 127.2f / PTM_RATIO),
            b2Vec2(187.0f / PTM_RATIO, 288.4f / PTM_RATIO),
            b2Vec2(400.0f / PTM_RATIO, 288.4f / PTM_RATIO)
        };
        blockShape.Set(verts, num);
    ///////////////////////////////////////////////////////////////////







        // Create shape definition and add to body
        b2FixtureDef blockShapeDef;
        blockShapeDef.shape = &blockShape;
        blockShapeDef.density = 10.0;
        blockShapeDef.friction = 0.0;
        blockShapeDef.restitution = 0.1f;
        blockBodyDef.angle = 45/PTM_RATIO;
         //blockShapeDef.angle = angle * 4.1;
        //blockShapeDef->angle = angle * 0.0174533f;
        //blockBody->SetTransform(b2Vec2(2, 45));
        //blockBodyDef->angle = Math.PI / 180 * 30;//30°
        //float32 angle2 = blockBody->GetAngle();
        blockBody->CreateFixture(&blockShapeDef);









    }

非常欢迎任何想法或建议。

1 个答案:

答案 0 :(得分:0)

我不确定您的确切问题是什么,但我发现您的代码存在一些问题:

  1. 您正在设置身体的角度不正确。你需要以弧度而不是度数来做。你需要在身体变换上设置它:

    //我不相信这有效......   //blockBodyDef.angle = CC_DEGREES_TO_RADIANS(45);

    ...

    // float32 angle2 = blockBody-&gt; GetAngle();   blockBody-&GT; CreateFixture(安培; blockShapeDef);   //我的修改后的行   blockBody-&gt; SetTransform(blockBody-&gt; GetPosition(),CC_DEGREES_TO_RADIANS(40));

  2. 我使用了你的SetAsBox(...)方法,但考虑了精灵的比例因子:

    block.scale = 0.25f; //我添加了这个用于测试

    b2PolygonShape blockShape; blockShape.SetAsBox(block.scale * block.contentSize.width / PTM_RATIO / 2,                   block.scale * block.contentSize.height / PTM_RATIO / 2);

  3. 当我做完所有这些时,我有四个盒子从屏幕中间开始(旋转40度),然后倒在地上并平放。这是你想要做的吗?