1 3.0物理系统PhysicsWorld
T07PhysicsWorld.h |
#ifndef #define #include class { public: CREATE_FUNC(T07PhysicsWorld); bool Scene* }; #endif |
T07PhysicsWorld.cpp |
#include bool { Layer::init(); PhysicsBody* PhysicsBody* { //后面的三个参数值分别表示的是:密度,弹性值,摩擦力 PhysicsBody* bodyA = //创建精灵 Sprite* //设置 sprite->setPhysicsBody(body); addChild(sprite); //设置精灵的位置 sprite->setPosition(winSize.width //设置速度 body->setVelocity(Vect(100, } { PhysicsBody* bodyB = Sprite* addChild(sprite); sprite->setPhysicsBody(body); sprite->setPosition(winSize.width } { bodyA->setContactTestBitmask(0x1); bodyB->setContactTestBitmask(0x1); bodyA->setGroup(1); bodyB->setGroup(2); //设置精灵的 auto ev->onContactBegin CCLog("Began return }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } { auto bodyB->getShapes().at(0)); ev->onContactBegin CCLog("Shape return }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } { auto ev->onContactBegin CCLog("Group return }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } return } |
TMenu.h |
#ifndef #define #include class { public: CREATE_FUNC(TMenu); bool bool }; #endif |
TMenu.cpp |
#include #include #include #include #include #include #include #include #include static "T01CPP11", "T02Vector", "T04Label", "T05Touch", "T06Box2D", "T07PhysicsWorld" }; bool { Layer::init(); Menu* addChild(menu); for (int { MenuItemFont* MenuItem* int Layer* if (title[i] if (title[i] if (title[i] if (title[i] if (title[i] if (title[i] if (l) { TBack* Scene* PhysicsWorld* world->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); s->addChild(b); s->addChild(l); Director::getInstance()->pushScene(s); } }); menu->addChild(item); item->setTag(1000 } menu->alignItemsVertically(); // auto #if 0 ev->onTouchBegan return }; #endif //ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2); ev->onTouchBegan ev->onTouchMoved setPositionY(getPositionY() }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, return } bool { return } |
运行结果: |
射线的做法
T08RayCast.h |
||||||||||||||
#ifndef #define #include class { public: CREATE_FUNC(T08RayCast); bool void void Sprite* int int float PhysicsShape* DrawNode* }; #endif |
||||||||||||||
T08RayCast.cpp |
||||||||||||||
#include void { Layer::onEnter(); Scene* scene->getPhysicsWorld()->setGravity(Vec2(0, } bool { Layer::init(); // { Sprite* addChild(cat); cat->setPosition(winSize.width _cat = } // { auto ev->onTouchBegan //得到触摸点 Vec2 //创建一个圆形的PhysicsBody PhysicsBody* Sprite* sprite->setPhysicsBody(body); addChild(sprite); sprite->setPosition(pt); return }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } { _angle = 0; _distance = 100; _nearDis = _food = _drawNode = } scheduleUpdate(); return } void { Scene* PhysicsWorld* //获得猫的位置 Vec2 Vec2 //当前时刻扫描到的终点位置 end.x end.y // if (_drawNode) //下面的代码用于画线段 _drawNode = _drawNode->drawSegment(start, addChild(_drawNode); // //bool(PhysicsWorld& world, const PhysicsRayCastInfo& info, void* data) auto if (info.shape return //如果点包含猫的点 float if (dis { _nearDis = _food = } // return }; //通过rayCast画一条射线 world->rayCast(callback, //每次角度加2 _angle += 2; //如果角度为360 if (_angle { //如果存在食物 if (_food) { // Node* //将猫的的位置数值到新的位置 _cat->setPosition(node->getPosition()); node->removeFromParent(); _food = _nearDis = } _angle = 0; } } |
||||||||||||||
运行结果: 1 3.0物理系统PhysicsWorld
射线的做法
|