【问题标题】:Box2d Bounding Box Without ForceBox2d 无力边界框
【发布时间】:2012-12-12 00:20:06
【问题描述】:

目前,Cocos2d-Box2d 项目正在使用 b2Vec2 为游戏边缘创建一个边界框。因此,bounding Box 不会影响运动学体,运动学体是不受力影响的体(这意味着体通常会飞出屏幕)。我想看看是否有办法让运动体与屏幕连接。如果没有,如果有人向我解释我应该如何在屏幕角落周围制作一个带有静态物体的边界框,我将不胜感激。

【问题讨论】:

    标签: static cocos2d-iphone box2d bounding-box kinematics


    【解决方案1】:

    这里有两种方法...尝试任何一种

    // 方法 - 1

    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0, 0);  
    b2Body              *mGroundBody ;
    
    
    mGroundBody = self.world->CreateBody(&groundBodyDef);
    NSString *strId = @"Ground Body";
    mGroundBody->SetUserData(strId);
    
    b2EdgeShape groundBox;      
    
        //bottom
        groundBox.Set(b2Vec2(0.0f,0.0f), b2Vec2(mS.width/PTM_RATIO,0.0f));
        mGroundBody->CreateFixture(&groundBox,0);
    
        // top
        groundBox.Set(b2Vec2(0,mS.height/PTM_RATIO), b2Vec2(mS.width/PTM_RATIO, mS.height/PTM_RATIO));
        mGroundBody->CreateFixture(&groundBox,0);
    
    
        // left
        groundBox.Set(b2Vec2(0,mS.height/PTM_RATIO), b2Vec2(0,0));
        mGroundBody->CreateFixture(&groundBox,0);
    
        // right
        groundBox.Set(b2Vec2(mS.width/PTM_RATIO,mS.height/PTM_RATIO), b2Vec2(mS.width/PTM_RATIO,0));
        mGroundBody->CreateFixture(&groundBox,0);
    

    // 方法 - 2

    //create 4 box2d walls...
    
        float bW = (IS_IPAD) ? (8) : 2 ;
        //top
        {
            b2BodyDef bodyDef;
            bodyDef.type = b2_staticBody;
            bodyDef.position.Set((mS.width*0.5f)/PTM_RATIO, (mS.height)/PTM_RATIO);
            bodyDef.linearDamping  = 0.0f;
            bodyDef.angularDamping = 0.0f;
    
            bodyDef.userData =  strId ;
    
            b2PolygonShape box;
            box.SetAsBox( ((mS.width*0.5f)/PTM_RATIO), (bW)/PTM_RATIO);
    
            b2FixtureDef fixDef;
            fixDef.shape = &box;
            fixDef.density = 1.0f;
            fixDef.friction = 0.1f;
            fixDef.restitution = 1.0f;
            fixDef.isSensor = false;
    
            b2Body *topBody = self.world->CreateBody(&bodyDef);
            topBody->CreateFixture(&fixDef);
    
        }
    
        //bottom
        {
            b2BodyDef bodyDef;
            bodyDef.type = b2_staticBody;
            bodyDef.position.Set((mS.width*0.5f)/PTM_RATIO, 0);
            bodyDef.linearDamping  = 0.0f;
            bodyDef.angularDamping = 0.0f;
    
            bodyDef.userData =  strId ;
    
            b2PolygonShape box;
            box.SetAsBox( ((mS.width*0.5f)/PTM_RATIO), (bW)/PTM_RATIO);
    
            b2FixtureDef fixDef;
            fixDef.shape = &box;
            fixDef.density = 1.0f;
            fixDef.friction = 0.1f;
            fixDef.restitution = 1.0f;
            fixDef.isSensor = false;
    
            b2Body *topBody = self.world->CreateBody(&bodyDef);
            topBody->CreateFixture(&fixDef);
    
        }
    
        //left
        {
            b2BodyDef bodyDef;
            bodyDef.type = b2_staticBody;
            bodyDef.position.Set(0, (mS.height*0.5f)/PTM_RATIO);
            bodyDef.linearDamping  = 0.0f;
            bodyDef.angularDamping = 0.0f;
    
            bodyDef.userData =  strId ;
    
            b2PolygonShape box;
            box.SetAsBox( ((bW)/PTM_RATIO), (mS.height*0.5f)/PTM_RATIO);
    
            b2FixtureDef fixDef;
            fixDef.shape = &box;
            fixDef.density = 1.0f;
            fixDef.friction = 0.1f;
            fixDef.restitution = 1.0f;
            fixDef.isSensor = false;
    
            b2Body *topBody = self.world->CreateBody(&bodyDef);
            topBody->CreateFixture(&fixDef);
    
        }
    
        //right
        {
            b2BodyDef bodyDef;
            bodyDef.type = b2_staticBody;
            bodyDef.position.Set((mS.width)/PTM_RATIO, (mS.height*0.5f)/PTM_RATIO);
            bodyDef.linearDamping  = 0.0f;
            bodyDef.angularDamping = 0.0f;
    
            bodyDef.userData =  strId ;
    
            b2PolygonShape box;
            box.SetAsBox( ((bW)/PTM_RATIO), (mS.height*0.5f)/PTM_RATIO);
    
            b2FixtureDef fixDef;
            fixDef.shape = &box;
            fixDef.density = 1.0f;
            fixDef.friction = 0.1f;
            fixDef.restitution = 1.0f;
            fixDef.isSensor = false;
    
            b2Body *topBody = self.world->CreateBody(&bodyDef);
            topBody->CreateFixture(&fixDef);
        }
    

    【讨论】:

    • 这两个选项似乎都不起作用。无论如何,我遇到了一个新问题。最初,我认为 Box2d Kinematic Body 不与 Box2d Walls 碰撞很奇怪,但后来我了解到 Kinematic Bodies 没有检测到碰撞......这很奇怪,因为 Kinematic Body 正在与 Dynamic我的身体...
    • 没关系,我想通了。运动学实体仍与动态实体发生碰撞。
    猜你喜欢
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多