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

来源:互联网 发布:考古学禁区 知乎 编辑:程序博客网 时间:2024/04/29 16:53


之前在写一个个人小项目的时候有问到过大海老师这个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().sizePhysicsMaterial(0.1f1,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(100100);

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

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

    ball->getPhysicsBody()->setVelocity(Point(100020));

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

    }

}

0 0
原创粉丝点击