cocos2dx3.X项目重写(二)新的物理引擎

新的物理引擎叫physicsBody

创建物理scene

auto scene = Scene::createWithPhysics();
添加调试信息 这样可以让刚体附加方框显示出来
 scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

 
创建边界框  

void Stage::addEdgeBox()
{
	auto visibleSize = Director::getInstance()->getVisibleSize();
	//建立刚体
	auto edgeBox = PhysicsBody::createEdgeBox(visibleSize);
	//创建图形
	auto shape =Node::create();
	//刚体附加到图形
	shape->setPhysicsBody(edgeBox);
	this->addChild(shape);
}
</pre><span style="color: rgb(51, 51, 51); font-family: sans-serif; font-size: 24px;">shape的默认位置是(0,0),但是这个是node的中心位置,我们把它设置到屏幕中心 </span><br style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: sans-serif; font-size: 24px;" /><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding-top: 0.2rem; padding-bottom: 0.3rem; font-family: Arial, 'Microsoft YaHei'; font-size: 0.7rem; color: rgb(51, 51, 51); line-height: 0.875rem; word-break: break-all;"></p><pre name="code" class="cpp" style="box-sizing: border-box; font-family: Arial, 'Microsoft YaHei'; font-size: 13px; white-space: pre-wrap; padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; border: 1px solid rgb(204, 204, 204); border-radius: 4px; overflow: hidden; width: 1543px; background-color: rgb(245, 245, 245);">shape->setPosition(visibleSize.width/2,visibleSize.heigh

设置好了世界,现在把精灵player变成刚体,只需要一句话

player->setPhysicsBody(PhysicsBody::createBox(player->getContentSize()));
这时候如果把精灵的position放到屏幕中间,就可以看到精灵下落,因为有了重力,而且到地面还会有反弹。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值