PhysicsBody *PEShapeCache::getPhysicsBodyByName(const std::string name) { BodyDef *bd = bodyDefs.at(name); CCASSERT(bd != nullptr, "Body not found"); PhysicsBody *body = PhysicsBody::create(bd->mass, bd->momentum); body->setPositionOffset(bd->anchorPoint); for (auto fd : bd->fixtures) { if (fd->fixtureType == SHAPE_CIRCLE) { auto shape = PhysicsShapeCircle::create(fd->radius, PhysicsMaterial(0.0f, fd->elasticity, fd->friction), fd->center); shape->setGroup(fd->group); // shape->setCategoryBitmask(fd->collisionType); body->addShape(shape); } else if (fd->fixtureType == SHAPE_POLYGON) { for (auto polygon : fd->polygons) { auto shape = PhysicsShapePolygon::create(polygon->vertices, polygon->numVertices, PhysicsMaterial(0.0f, fd->elasticity, fd->friction), fd->center); shape->setGroup(fd->group); // shape->setCategoryBitmask(fd->collisionType); body->addShape(shape); } } } return body; }
void Level::createPhysicsBody() { for (auto& object : _collisionObjectGroup->getObjects()) { float objectX = object.asValueMap().at("x").asFloat(), objectY = object.asValueMap().at("y").asFloat(); Size s = Size(object.asValueMap().at("width").asFloat(), object.asValueMap().at("height").asFloat()); if (object.asValueMap().find("polylinePoints") == object.asValueMap().end()) { PhysicsShapeBox *box = PhysicsShapeBox::create(s, PhysicsMaterial(0.0f, 0.0f, 1.0f), Vec2(objectX + s.width / 2, objectY + s.height / 2)); box->setTag(0); PhysicsBody *body = PhysicsBody::create(); body->setDynamic(false); body->setContactTestBitmask(0xFFFFFFFF); body->addShape(box); Node* node = Node::create(); node->setPhysicsBody(body); //addChild(node); _physicsNodes.pushBack(node); } else { Vec2 t[2]; int i = 0; for (auto& point : object.asValueMap().at("polylinePoints").asValueVector()) { // convert the points' local coordinates to the world coordinates // by doing a translation using the object's position vector // We invert the local y because it's based on the top-left space in Tiled t[i].x = point.asValueMap().at("x").asInt() + objectX; t[i].y = -point.asValueMap().at("y").asInt() + objectY; i++; } PhysicsShapeEdgeSegment *line = PhysicsShapeEdgeSegment::create(t[0], t[1], PhysicsMaterial(0.0f, 0.0f, 1.0f)); line->setTag(0); PhysicsBody *body = PhysicsBody::create(); body->setDynamic(false); body->setContactTestBitmask(0xFFFFFFFF); body->addShape(line); Node* node = Node::create(); node->setPhysicsBody(body); //addChild(node); _physicsNodes.pushBack(node); } } }
void BoySprite::setPhysical() { // add the physical body PhysicsBody *body = PhysicsBody::create(); body->addShape(NORMAL_SHAPE); body->setDynamic(true); body->setLinearDamping(0.0f); body->setRotationEnable(false); this->setPhysicsBody(body); }
PhysicsBody* PhysicsBody::createPolygon(Point* points, int count, PhysicsMaterial material) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapePolygon::create(points, count, material)); body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
PhysicsBody* PhysicsBody::createPolygon(const Vec2* points, int count, const PhysicsMaterial& material, const Vec2& offset) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapePolygon::create(points, count, material, offset)); body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
PhysicsBody* PhysicsBody::createCircle(float radius, const PhysicsMaterial& material, const Vec2& offset) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapeCircle::create(radius, material, offset)); body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
PhysicsBody* PhysicsBody::createBox(const Size& size, const PhysicsMaterial& material, const Vec2& offset) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapeBox::create(size, material, offset)); body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
PhysicsBody* PhysicsBody::createBox(Size size, PhysicsMaterial material) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapeBox::create(size, material)); body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
//从json文件加载正确的body PhysicsBody* MyBodyParser::bodyFormJson(cocos2d::Node *pNode, const std::string& name, PhysicsMaterial material) { PhysicsBody* body = nullptr; rapidjson::Value &bodies = doc["rigidBodies"]; if (bodies.IsArray()) { //遍历文件中的所有body for (int i=0; i<bodies.Size(); ++i) { //找到了请求的那一个 if (0 == strcmp(name.c_str(), bodies[i]["name"].GetString())) { rapidjson::Value &bd = bodies[i]; if (bd.IsObject()) { //创建一个PhysicsBody, 并且根据node的大小来设置 body = PhysicsBody::create(); float width = pNode->getContentSize().width; float offx = - pNode->getAnchorPoint().x*pNode->getContentSize().width; float offy = - pNode->getAnchorPoint().y*pNode->getContentSize().height; //CCLOG("%s :: w %f || offx %f || offy %f ||\n", bodies[i]["name"].GetString(), width, offx, offy); Point origin( bd["origin"]["x"].GetDouble(), bd["origin"]["y"].GetDouble()); rapidjson::Value &polygons = bd["polygons"]; for (int i = 0; i<polygons.Size(); ++i) { int pcount = polygons[i].Size(); //CCLOG("count %d", pcount); Point* points = new Point[pcount]; for (int pi = 0; pi<pcount; ++pi) { points[pi].x = offx + width * polygons[i][pcount-1-pi]["x"].GetDouble(); points[pi].y = offy + width * polygons[i][pcount-1-pi]["y"].GetDouble(); //CCLOG("x %d, y %d", points[pi].x, points[pi].y); } body->addShape(PhysicsShapePolygon::create(points, pcount, material)); delete [] points; } } else { CCLOG("body: %s not found!", name.c_str()); } break; } } } return body; }
PhysicsBody* PhysicsBody::createEdgeSegment(const Vec2& a, const Vec2& b, const PhysicsMaterial& material, float border/* = 1*/) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapeEdgeSegment::create(a, b, material, border)); body->_dynamic = false; body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
PhysicsBody* PhysicsBody::createEdgeChain(const Vec2* points, int count, const PhysicsMaterial& material, float border/* = 1*/) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapeEdgeChain::create(points, count, material, border)); body->_dynamic = false; body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
PhysicsBody* PhysicsBody::createEdgeBox(const Size& size, const PhysicsMaterial& material, float border/* = 1*/, const Vec2& offset) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapeEdgeBox::create(size, material, border, offset)); body->_dynamic = false; body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
PhysicsBody* PhysicsBody::createEdgeBox(Size size, PhysicsMaterial material, float border/* = 1*/) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapeEdgeBox::create(size, material, border)); body->_dynamic = false; body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
PhysicsBody* PhysicsBody::createEdgePolygon(const Vec2* points, int count, const PhysicsMaterial& material, float border/* = 1*/) { PhysicsBody* body = new (std::nothrow) PhysicsBody(); if (body && body->init()) { body->addShape(PhysicsShapeEdgePolygon::create(points, count, material, border)); body->setDynamic(false); body->autorelease(); return body; } CC_SAFE_DELETE(body); return nullptr; }
/* auto ball = Sprite::create("Ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBird); ballbody->setCollisionBitmask(ColliderTypeLand | ColliderTypePip); ballbody->setContactTestBitmask(1); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(true); ball->setPhysicsBody(ballbody); auto visibleSize = Director::getInstance()->getVisibleSize(); ball->setPosition(bird->getPositionX()+2*20,visibleSize.height); addChild(ball); */ void GameLayer::addBallObstacle(float dt) { auto ball = Sprite::create("Ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBall); ballbody->setCollisionBitmask(ColliderTypeLand | ColliderTypePip | ColliderTypeBird | ColliderTypeBall); ballbody->setContactTestBitmask(ColliderTypeBird); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(true); ball->setPhysicsBody(ballbody); auto visibleSize = Director::getInstance()->getVisibleSize(); ball->setPosition(bird->getPositionX()+2*20,visibleSize.height); addChild(ball); }
bool Witch::init() { mWitch = CCSprite::createWithSpriteFrameName(Tex::Player_0); this->addChild(mWitch); CCLOG("Witch Width:%f", mWitch->getContentSize().width); mWitch->setPosition(0, 0); PhysicsBody *body = PhysicsBody::create(); body->setTag(1); this->setPhysicsBody(body); body->addShape(PhysicsShapeBox::create(Size(mWitch->getContentSize().width, mWitch->getContentSize().height))); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); return true; }
bool GameLayer::init(){ if(Layer::init()) { //get the origin point of the X-Y axis, and the visiable size of the screen Size visiableSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); this->gameStatus = GAME_STATUS_READY; this->score = 0; // Add the bird this->bird = BirdSprite::getInstance(); this->bird->createBird(); PhysicsBody *body = PhysicsBody::create(); body->addShape(PhysicsShapeCircle::create(BIRD_RADIUS)); //ÅöײÉèÖà body->setCategoryBitmask(ColliderTypeBird); body->setCollisionBitmask(ColliderTypeLand | ColliderTypePip); body->setContactTestBitmask(ColliderTypeLand | ColliderTypePip); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); this->bird->setPhysicsBody(body); this->bird->setPosition(origin.x + visiableSize.width*1/3 - 5,origin.y + visiableSize.height/2 + 5); this->bird->idle(); this->addChild(this->bird); // Add the ground this->groundNode = Node::create(); float landHeight = BackgroundLayer::getLandHeight(); auto groundBody = PhysicsBody::create(); groundBody->addShape(PhysicsShapeBox::create(Size(288, landHeight))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); //ÅöײÉèÖà groundBody->setCategoryBitmask(ColliderTypeLand); groundBody->setCollisionBitmask(ColliderTypeBird); groundBody->setContactTestBitmask(ColliderTypeBird); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(144, landHeight/2); this->addChild(this->groundNode); // init land this->landSpite1 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite1->setAnchorPoint(Point::ZERO); this->landSpite1->setPosition(Point::ZERO); this->addChild(this->landSpite1, 30); this->landSpite2 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite2->setAnchorPoint(Point::ZERO); this->landSpite2->setPosition(this->landSpite1->getContentSize().width-2.0f,0); this->addChild(this->landSpite2, 30); shiftLand = schedule_selector(GameLayer::scrollLand); this->schedule(shiftLand, 0.01f); this->scheduleUpdate(); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); this->dialogLayer = nullptr; return true; }else { return false; } }
bool GameLayer::init() { if (!Layer::init()) return false; // Welcome Message Size visiableSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // screen background auto screenBackground = Sprite::create("screen_background.png"); screenBackground->setAnchorPoint(Point::ZERO); screenBackground->setPosition(Vec2(0, 0)); this->addChild(screenBackground); // sprite ⇨ mosquito mosquito = Sprite::create("mosquito.png"); mosquito->setPosition(Vec2(30, visiableSize.height - 300)); mosquito->setScale(0.1 * ( visiableSize.width / mosquito->getBoundingBox().size.width)); this->addChild(mosquito); //bug->runAction(this->create(6, bug->getPosition(), Vec2(visiableSize.width - 30, visiableSize.height - 300), 100)); mosquito->runAction(Spawn::create(RotateBy::create(5, 1080), this->create(5, mosquito->getPosition(),Vec2(visiableSize.width - 30, visiableSize.height - 300), 100), NULL)); PhysicsBody *body = PhysicsBody::createCircle(BUG_RADIUS); body->addShape(PhysicsShapeCircle::create(BUG_RADIUS)); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); body->setMass(1.0f); body->setContactTestBitmask(0xFFFFFFFF); this->mosquito->setPhysicsBody(body); // ************************************************************************ // // MotionStreakを作成 pStreak = MotionStreak::create(1.0, 1.0f, 3.0f, Color3B::GREEN, "suspend.png"); addChild(pStreak); this->groundNode = Node::create(); //イベントリスナー作成 auto listener = EventListenerTouchOneByOne::create(); //タッチ開始時 listener->onTouchBegan = [this](Touch* touch, Event* event) { Point pos = this->convertTouchToNodeSpace(touch); this->pStreak->setPosition(pos); auto groundBody = PhysicsBody::createBox(Size(4, pos.y)); groundBody->addShape(PhysicsShapeBox::create(Size(4, pos.y))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); groundBody->setMass(1.0f); groundBody->setContactTestBitmask(0xFFFFFFFF); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(pos.x, pos.y); this->addChild(this->groundNode); return true; }; //タッチ移動時 listener->onTouchMoved = [this](Touch* touch, Event* event) { Point pos = this->convertTouchToNodeSpace(touch); this->pStreak->setPosition(pos); }; //ディスパッチャに登録 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 100); // *************************************************************************************** // // sprite ⇨ suspend (init for invisible) suspendBackground = Sprite::create("suspend_background.png"); suspendBackground->setAnchorPoint(Point::ZERO); suspendBackground->setPosition(Vec2(0, 0 + 40)); suspendBackground->setScale(visiableSize.width / suspendBackground->getBoundingBox().size.width, (visiableSize.height - 40)/ suspendBackground->getBoundingBox().size.height); suspendBackground->setTag(2009); suspendBackground->setVisible(false); this->addChild(suspendBackground); //进入暂停 auto suspendBtn = Sprite::create("suspend.png"); auto menuSuspendItem = MenuItemSprite::create(suspendBtn, suspendBtn, NULL, CC_CALLBACK_1(GameLayer::menuSuspendCallback, this)); menuSuspendItem->setPosition(Vec2(visiableSize.width - suspendBtn->getBoundingBox().size.width - 20, visiableSize.height - suspendBtn->getBoundingBox().size.height - 20)); auto menu = Menu::create(menuSuspendItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 3); // suspend close button auto closeButton = Sprite::create("exit.png"); MenuItem *menuCloseItem = MenuItemSprite::create(closeButton, closeButton, NULL, CC_CALLBACK_1(GameLayer::menuSuspendCloseCallback, this)); menuCloseItem->setPosition(Vec2(245, 128)); pauseMenu = Menu::create( menuCloseItem, NULL); pauseMenu->setPosition(Point::ZERO); pauseMenu->setVisible(false); this->addChild(pauseMenu, 4); schedule(schedule_selector(GameLayer::update), 2.0f); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); return true; }
bool GameLayer::init(){ if(Layer::init()) { //get the origin point of the X-Y axis, and the visiable size of the screen Size visiableSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); this->gameStatus = GAME_STATUS_READY; this->score = 0; // Add the bird this->bird = BirdSprite::getInstance(); this->bird->createBird(); PhysicsBody *body = PhysicsBody::create(); body->addShape(PhysicsShapeCircle::create(BIRD_RADIUS)); body->setCategoryBitmask(ColliderTypeBird); body->setCollisionBitmask(ColliderTypeLand & ColliderTypePip | ColliderTypeBall); body->setContactTestBitmask(ColliderTypeLand | ColliderTypePip | ColliderTypeBall); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); this->bird->setPhysicsBody(body); this->bird->setPosition(origin.x + visiableSize.width*1/3 - 5,origin.y + visiableSize.height/2 + 5); this->bird->idle(); this->addChild(this->bird); /* //初始化加速粒子 accelerateParticle = ParticleSystemQuad::create("particleImpact.plist"); accelerateParticle->setScale(0.5f); accelerateParticle->setPosition(0,0); addChild(accelerateParticle); */ accelerateParticle = NULL; pipSpeed = 2; /* //闪亮 Blink *blink = Blink::create(5.0f, 10); bird->runAction(blink); */ //Ball this->ball = Sprite::create("ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBall); ballbody->setCollisionBitmask(ColliderTypePip | ColliderTypeLand); ballbody->setContactTestBitmask(ColliderTypePip | ColliderTypeLand); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(false); ball->setPhysicsBody(ballbody); // static Shaky3D* create(float duration, const Size& gridSize, int range, bool shakeZ); ball->setPosition(bird->getPositionX(),bird->getPositionY()+30); ball->setTag(100); // addChild(ball); BallisTouch = false; //BallName ballName = Sprite::create("BallWithHoney.png"); ballName->setPosition(ball->getPositionX(),ball->getPositionY()+40); //addChild(ballName); // Add the ground this->groundNode = Node::create(); float landHeight = BackgroundLayer::getLandHeight(); auto groundBody = PhysicsBody::create(); groundBody->addShape(PhysicsShapeBox::create(Size(288, landHeight))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); groundBody->setCategoryBitmask(ColliderTypeLand); groundBody->setCollisionBitmask(ColliderTypeBird | ColliderTypeBall); groundBody->setContactTestBitmask(ColliderTypeBird | ColliderTypeLand); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(144, landHeight/2); this->addChild(this->groundNode); // init land this->landSpite1 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite1->setAnchorPoint(Point::ZERO); this->landSpite1->setPosition(Point::ZERO); this->addChild(this->landSpite1, 30); this->landSpite2 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite2->setAnchorPoint(Point::ZERO); this->landSpite2->setPosition(this->landSpite1->getContentSize().width-2.0f,0); this->addChild(this->landSpite2, 30); shiftLand = schedule_selector(GameLayer::scrollLand); this->schedule(shiftLand, 0.01f); this->scheduleUpdate(); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); return true; }else { return false; } }