Cocos2d-x3.3 Physics物理引擎模块解决了刚体穿透问题


之前在写一个个人小项目的时候有问到过大海老师这个physics物理引擎的刚体穿透问题,然后大海老师就给我推荐了他自己写的这篇文博,很好很强大。

下面是大海老师的博客

代码

voidPhysicsFixedUpdate::onEnter()

{

PhysicsDemo::onEnter();

_scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

_scene->getPhysicsWorld()->setGravity(Point::ZERO);

// wall

autowall =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);

}


voidPhysicsFixedUpdate::addBall()

{

autoball =Sprite::create("Images/ball.png");

ball->setPosition(100,100);

ball->setPhysicsBody(PhysicsBody::createCircle(ball->getContentSize().width/2,0.0f)));

ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG);

ball->getPhysicsBody()->setVeLocity(Point(1000,20));

this->addChild(ball);


voidPhysicsFixedUpdate::updateStart(floatdelta)

//重点在这里

_scene->getPhysicsWorld()->setAutoStep(false);

scheduleUpdate();

voidPhysicsFixedUpdate::update(floatdelta)

{

// use fixed time and calculate 3 times per frame makes physics simulate more precisely.

//这里表示先走3步瞧瞧 如果fps是1/60 三个setp就是1/180

for(inti =0; i <3; ++i)

{

_scene->getPhysicsWorld()->step(1/180.0f);

}

}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...