3、b2AABB:

3、b2AABB:

b2AABB就是一个盒子,是由两个向量组成,一个为minVertex是最小顶点,另一个为maxVertex是最大顶点,通过这两个顶点来表示最为普通的AABB框。

结构体原型:

struct b2AABB
{
    /// Verify that the bounds are sorted.
    bool IsValid() const;

    b2Vec2 lowerBound;    ///< the lower vertex
    b2Vec2 upperBound;    ///< the upper vertex
};

AABB的全称是axis aligned bounding box,

大致概念首先是一个box,

也就是一个框框,

这个框框的4边与坐标轴平行,

也就是一个正方形或者长方形区域。

 

box2d 遍历世界中body列表的2种方法


第1种方法,在对 body 列表有删除操作的时候,

采用 while 的遍历方式比较方便(详见box2d白皮书)~

    /** Iterate over the bodies in the physics world */

    b2Body *node = _world->GetBodyList();  

    while(node) {  

        b2Body *b = node;

        node = node->GetNext();

        if(b->GetUserData() != NULL) {

           //Synchronize the AtlasSprites position and rotation with the corresponding(相应的) body

            CCSprite *myActor = (CCSprite*)b->GetUserData();

            CGPoint position = CGPointMake(b->GetPosition().x*PTM_RATIO, b->GetPosition().y*PTM_RATIO);  

            if(![MGameScene isPositionOutOfBounds:position]) { //如果没有越界的话,实时更新~

                myActor.position = position;

                myActor.rotation = -1 *CC_RADIANS_TO_DEGREES(b->GetAngle());

            } else {

                [_single.gameLayerremoveChild:myActor cleanup:YES];

                b->SetUserData(NULL);

                b->SetTransform(b2Vec2(7.5f,20.0f), 0.0f);   // 设置一个合理的位置储存这些用处已经不大的body~

                b->SetType(b2_staticBody);

            }

        } 

    }


第2种方法,适用于不对 body 列表作删除操作的情况

for (b2Body* b =_world->GetBodyList(); b; b = b->GetNext()) {

if (b->GetUserData() !=NULL) {

//Synchronize the AtlasSprites position and rotation with the corresponding body

CCSprite *myActor = (CCSprite*)b->GetUserData();

myActor.position =CGPointMake( b->GetPosition().x *PTM_RATIO, b->GetPosition().y *PTM_RATIO);

myActor.rotation = -1 *CC_RADIANS_TO_DEGREES(b->GetAngle());

}

}

更多 0



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值