展会信息港展会大全

Cocos2d-x3.3 Physics物理引擎模块解决了刚体穿透问题
来源:互联网   发布日期:2015-09-28 10:37:32   浏览:2281次  

导读:看代码: void PhysicsFixedUpdate::onEnter() { PhysicsDemo::onEnter(); _scene-getPhysicsWorld()-setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); _scene-getPhysicsWorld()-setGravity(Point::ZERO); // wall auto wall = Node::create(); wall-setPhy...

看代码:

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

{

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

}

}

赞助本站

人工智能实验室
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港