Physics封装,碰撞检测,课堂随笔
1. 定义GameScene.h中定义PhysicsWorld*nowWorld;//定义一个物理世界
2. 在创建场景对象时就要这样写了
Scene*GameScene::createScene()
{
auto scene=Scene::createWithPhysics();//创建一个带有物理世界的场景对象
auto layer=GameScene::create();
scene->addChild(layer);
layer->nowWorld=scene->getPhysicsWorld;//把物理世界的场景对象添加到图层
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);//定义物理世界的调试模式—开启模式 DEBUGDRAW_NONE;关闭
return true;
}
3. 创建一个静态刚体
auto groung=Sprite::create();//创建一个精灵
ground->setPosition(Vec(Director::getInstance()->getWinSize().width/2,20));
this->addChild(ground);
//定义一个Box夹具
autobody=PhysicsBody::creaetBox(Size(Vec2(Director::getInstance()->getWinSize().width,10)));
body->setGravityEnable(false);
body->setDynamic(false);
ground->setPhysicsBody(body);//设置精灵的物理刚体
4. 添加一个pig的刚体
void GameScene::addPig(int x,inty)
{
Sprite*pig=Sprite::create(“pig1.png”);
Pig->setPosition(x,y);
this->addChild(pig);
//定义一个物理刚体夹具圆形
autobody=PhysicsBody::creaetCircle(50);
body->setDynamic(true);//动态刚体
body->setMass(1.0f);//设置阻尼
body->setContactTestBitmask(0xFFFFFFFF);//掩码—主要是稍后我们碰撞检测时候用的
pig->setPhysicsBody(Body);//设置精灵的物理刚体
}
我们要实现在屏幕上点击产生一个猪
在
bool GameScene::onTouchBegan(Touch *touch, Event*unused_event)
{
addPig(touch->getLocation().x, touch->getLocation().y);
return true;
}
我们想添加一组约束刚体
Scene*GameScene::creaetScene()
{
auto sp1=Sprite::create("pig_3.png");
auto sp2=Sprite::create("bird2.png");
auto pbody1=PhysicsBody::createCircle(50);
auto pbody2=PhysicsBody::createCircle(50);
pbody1->setDynamic(true);
pbody2->setDynamic(true);
pbody1->setMass(1);
pbody2->setMass(1);
sp1->setPhysicsBody(pbody1);
sp2->setPhysicsBody(pbody2);
sp1->setPosition(Vec2(300,300));
sp2->setPosition(Vec2(300,300));
//两个物体之间的约束
PhysicsJointPin* joint = PhysicsJointPin::construct(
sp1->getPhysicsBody(),
sp2->getPhysicsBody(), Vec2(300,300));
layer->nowWorld->addJoint(joint);
layer->addChild(sp1);
layer->addChild(sp2);
}
添加碰撞检测---实现可以选择刚体,移动刚体
Scene*GameScene::createScene()
{
//添加刚体之间的碰撞检测--碰撞完之后的回调
auto listener02=EventListenerPhysicsContact::create();
listener02->onContactPostSolve=CC_CALLBACK_2(GameScene::onContact,layer);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener02, layer);
}
bool GameScene::init(){
if (!Layer::init()) {
return false;
}
auto listener=EventListenerTouchOneByOne::create();
listener->onTouchBegan=CC_CALLBACK_2(GameScene::onTouchBegan, this);
listener->onTouchMoved=CC_CALLBACK_2(GameScene::onTouchMoved, this);
listener->onTouchEnded=CC_CALLBACK_2(GameScene::onTouchEnded, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
bool GameScene::onTouchBegan(Touch *touch, Event*unused_event){
auto location=touch->getLocation();
auto arr=nowWorld->getShapes(location);//得到某个坐标点位置的所有刚体
PhysicsBody *body=nullptr;
for (int i=0; i<arr.size(); i++)
{
auto obj=arr.at(i);
body=obj->getBody();//得到选中对象的第一个刚体
break;
}
if (body!=nullptr)
{
Node*mouse=Node::create();
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY,PHYSICS_INFINITY));
mouse->setPosition(location);//新的刚体的坐标是触摸点的做标
mouse->getPhysicsBody()->setDynamic(false);//静态刚体
this->addChild(mouse);
PhysicsJointPin*joint=PhysicsJointPin::construct(mouse->getPhysicsBody(), body,location);
joint->setMaxForce(5000*body->getMass());//添加静态的刚体
nowWorld->addJoint(joint);
_mouses.insert(touch->getID(), mouse);//map mouse中存得都是刚体的编号
return true;
}else
{
addPig(touch->getLocation().x,touch->getLocation().y);
}
return true;
}
void GameScene::onTouchMoved(Touch *touch, Event *unused_event)
{
auto it=_mouses.find(touch->getID());
if (it!=_mouses.end())
{ //得到集合中的静态刚体
it->second->setPosition(touch->getLocation());
}
}
void GameScene::onTouchEnded(Touch *touch, Event *unused_event)
{
auto it=_mouses.find(touch->getID());
if (it!=_mouses.end())
{
this->removeChild(it->second);
_mouses.erase(it);//删除静态刚体
}
}
void GameScene::onContact(PhysicsContact& contact, const PhysicsContactPostSolve& solve)
{
CCLOG("碰撞检测%f",solve.getRestitution());//这个力
if (solve.getRestitution()>2)
{
}
}