本文主要是介绍Cocos2d-x3.3 Physics物理引擎模块解决了刚体穿透问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
看代码:
void PhysicsFixedUpdate::onEnter()
{
PhysicsDemo::onEnter();
_scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
_scene->getPhysicsWorld()->setGravity(Point::ZERO);
// wall
auto wall = Node::create();
wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1, 0.0f)));
wall->setPosition(VisibleRect::center());
this->addChild(wall);
addBall();
scheduleOnce(schedule_selector(PhysicsFixedUpdate::updateStart), 2);
}
void PhysicsFixedUpdate::addBall()
{
auto ball = Sprite::create("Images/ball.png");
ball->setPosition(100, 100);
ball->setPhysicsBody(PhysicsBody::createCircle(ball->getContentSize().width/2, PhysicsMaterial(0.1f, 1, 0.0f)));
ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
ball->getPhysicsBody()->setVelocity(Point(1000, 20));
this->addChild(ball);
}
void PhysicsFixedUpdate::updateStart(float delta)
{
addBall();
//重点在这里
_scene->getPhysicsWorld()->setAutoStep(false);
scheduleUpdate();
}
void PhysicsFixedUpdate::update(float delta)
{
// use fixed time and calculate 3 times per frame makes physics simulate more precisely.
//这里表示先走3步瞧瞧 如果fps是1/60 三个setp就是1/180
for (int i = 0; i < 3; ++i)
{
_scene->getPhysicsWorld()->step(1/180.0f);
}
}
这篇关于Cocos2d-x3.3 Physics物理引擎模块解决了刚体穿透问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!