cocos2d-x box2d 世界刚体初始化流程

这篇博客介绍了在cocos2d-x中使用box2d物理引擎初始化世界和刚体的步骤。首先,设置世界重力为0.0f, -10.0f,并允许物体在静止时睡眠以优化性能。接着,创建边界,包括底部、顶部、左侧和右侧的边缘,以限制物体的运动范围。通过b2BodyDef和b2EdgeShape定义刚体和形状,并将其添加到世界中。" 110638035,9055224,递归解决取球组合问题,"['算法', '递归算法', '组合问题']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    b2Vec2 gravity;

    //设置世界重力加速度为10,-向下,+向上

    gravity.Set(0.0f, -10.0f);

   world = newb2World(gravity);

    

    //物体静止时,允许睡眠,不被纳入世界计算,这样提高性能

    world->SetAllowSleeping(true);

    world->SetContinuousPhysics(true);

    

    //Define the grounp body 世界是有边界的

    //保存构造刚体数据

   b2BodyDef groundBodyDef;

    //刚体(0,0)点坐标在左下角

    groundBodyDef.position.Set(0,0);

    

    //创建刚体

   b2Body *groundBody = world->CreateBody(&groundBodyDef);

    

    //define the ground box shape

    //一个线段(边缘)的形状.这些可以在链或环连接其他边缘形状。使用连接信息,以确保正确的接触法线。

   b2EdgeShape groundBox ;

    

    //bottom

    groundBox.Set(b2Vec2(0,0), b2Vec2(960 /PTM_RATIO,0));

    groundBody->CreateFixture(&groundBox,0);

    

    //top

    groundBox.Set(b2Vec2(0,640 / PTM_RATIO),b2Vec2(960 /PTM_RATIO,640/PTM_RATIO));

    groundBody->CreateFixture(&groundBox,0);

    

    //left

    groundBox.Set(b2Vec2(0,640 / PTM_RATIO),b2Vec2(0,0));

    groundBody->CreateFixture(&groundBox,0);

    

    //right

    groundBox.Set(b2Vec2(960 /PTM_RATIO, 640 /PTM_RATIO), b2Vec2(960 /PTM_RATIO,640/PTM_RATIO));

    groundBody->CreateFixture(&groundBox,0);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值