cocos2d-x 3.2 物理世界PhysicsWorld

本文介绍了cocos2d-x 3.2版本中的物理世界创建过程,包括如何创建物理场景、设置重力、添加边界盒以及处理物理碰撞事件。通过示例代码详细讲解了基本的步骤,强调了ContactTestBitmask和setTag在碰撞检测中的重要性,适合初学者参考。
摘要由CSDN通过智能技术生成

cocos2d-x-3.2alpha0的物理世界。


hello  大家好,小弟这厢有礼了,我又来写博客了,cocos2d-x果然越来越人性话了。


看一看创建物理世界:


//创建一个物理场景

auto scene =Scene::createWithPhysics(); 

//设置物理世界的重力
scene->getPhysicsWorld()->setGravity(Vect(0,-(Director::getInstance()->getVisibleSize().height/3)));

//边界盒
auto edge=PhysicsBody::createEdgeBox(visibleSize);


对照一下cocos2d-x 2.2.3的Box2D

b2Vec2 gravity;
    gravity.Set(0.0f, -10.0f);        //重力

    world = new b2World(gravity);            //创建世界对象  一切都是映射
	
	

    world->SetAllowSleeping(true);            //允许休眠
    world->SetContinuousPhysics(true);        //设置检测连续碰撞

	//定义地形钢体描述并设置坐标为左下角
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0, 0);

	//根据钢体的描述创建钢体对象
    b2Body* groundBody = world->CreateBody(&groundBodyDef);
    groundBody->SetType(b2_staticBody);

    b2EdgeShape groundBox;      //定义地面盒形状
	
  //2个点确定一条线,以下分别设置了屏幕的4个边
    // bottom
    groundBox.Set(b2Vec2(0, 0), b2Vec2(480/PTM_RATIO, 0));
    groundBody->CreateFixture(&groundBox,0);

    // top
    groundBox.Set(b2Vec2(0 ,320/PTM_RATIO), b2Vec2(480/PTM_RATIO,320/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);

    // left
    groundBox.Set(b2Vec2(0, 320/PTM_RATIO), b2Vec2(0 ,0));
    groundBody->CreateFixture(&groundBox,0);

    // right
    groundBox.Set(b2Vec2(480/PTM_RATIO, 320/PTM_RATIO), b2Vec2(480/PTM_RATIO, 0));
    groundBody->CreateFixture(&groundBox,0);
	
	CCSpriteBatchNode *blocks=CCSpriteBatchNode::create("blocks
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值