void CCutScene::createBackground() { Point points[4] = { Point(0, 0), Point(0, 2048), Point(2048, 2048), Point(2048, 0) }; Vector2dVector vcPoints; vcPoints.clear(); for (int i = 0; i < 4; i++) { vcPoints.push_back(Vector2d(points[i].x, points[i].y)); } PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, "daanban.png"); //filledPolygon->setPhysicsBody(PhysicsBody::createPolygon(points, 4)); filledPolygon->setPosition(Vec2::ZERO + Vec2(0, (1536 - 2048) / 2)); addChild(filledPolygon, 80); }
bool FruitCutNinjaScene::init() { if (!Layer::init()) { return false; } _visibleSize = Director::getInstance()->getVisibleSize(); _deltaRemainder = 0.0f; _sliceTag = 1; //createBackground(); //加入封闭的盒子,用作墙壁 auto body = PhysicsBody::createEdgeBox(_visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); auto wallNode = Node::create(); wallNode->setPosition(_visibleSize.width/2, _visibleSize.height/2); wallNode->setPhysicsBody(body); this->addChild(wallNode); auto touchListener = EventListenerTouchAllAtOnce::create(); touchListener->onTouchesBegan = CC_CALLBACK_2(FruitCutNinjaScene::onTouchesBegan, this); touchListener->onTouchesMoved = CC_CALLBACK_2(FruitCutNinjaScene::onTouchesMoved, this); touchListener->onTouchesEnded = CC_CALLBACK_2(FruitCutNinjaScene::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); auto texture = Director::getInstance()->getTextureCache()->addImage("streak.png"); for (int i = 0; i < 3; i++) { Blade* blade = Blade::createWithMaximumPoint(50); blade->setAutoDim(false); blade->setTexture(texture); addChild(blade, 100); _blades.pushBack(blade); } //PEShapeCache::getInstance()->addBodysWithFile("pineapple.plist"); //auto body = PEShapeCache::getInstance()->getPhysicsBodyByName("pineapple"); //Point points[4] = {Point(-128 + 128, -128 + 128), Point(-128 + 128, 128 + 128), Point(128 + 128, 128 + 128), Point(128 + 128, -128 + 128)}; /* Point points[9] = { Point(157,50), Point(119, 13), Point(54, 0), Point(7,36), Point(5, 78), Point(23,118), Point(170,252), Point(235, 244), Point(237,158) }; */ Point points[4] = { Point(0, 0), Point(0, 512), Point(512, 512), Point(512, 0) }; Vector2dVector vcPoints; vcPoints.clear(); for (int i = 0; i < 4; i++) { vcPoints.push_back(Vector2d(points[i].x, points[i].y)); } //texture = Director::getInstance()->getTextureCache()->addImage("boluo.png"); PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, "boluo.png"); filledPolygon->setPhysicsBody(PhysicsBody::createPolygon(points, 4)); filledPolygon->setPosition(Vec2(400, 700)); filledPolygon->getPhysicsBody()->setTag(_sliceTag); filledPolygon->getPhysicsBody()->setGravityEnable(false); addChild(filledPolygon, 80); filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, "xigua.png"); filledPolygon->setPhysicsBody(PhysicsBody::createPolygon(points, 4)); filledPolygon->setPosition(Vec2(1200, 700)); filledPolygon->getPhysicsBody()->setTag(_sliceTag); filledPolygon->getPhysicsBody()->setGravityEnable(false); addChild(filledPolygon, 80); for (int i = 0; i < PARTICLE_COUNT; i++) { m_pEmitter[i] = ParticleSystemQuad::create("pineapple_splurt.plist"); this->addChild(m_pEmitter[i], 100); m_pEmitter[i]->setAutoRemoveOnFinish(false); m_pEmitter[i]->setDuration(0.1f); m_pEmitter[i]->setScale(1.5f); } //本DEMO要做成一个切水果原型,尚未完成:) scheduleUpdate(); return true; }
void FruitCutNinjaScene::clipPoly(PhysicsShapePolygon* shape, Point normal, float distance) { PhysicsBody* body = shape->getBody(); int count = shape->getPointsCount(); int pointsCount = 0; Point* points = new Point[count + 1]; Vector2dVector vcPoints; vcPoints.clear(); Vector2d v2Point(0, 0); for (int i=0, j=count-1; i<count; j=i, ++i) { Point a = body->local2World(shape->getPoint(j)); float aDist = a.dot(normal) - distance; if (aDist < 0.0f) { points[pointsCount] = a; ++pointsCount; } Point b = body->local2World(shape->getPoint(i)); float bDist = b.dot(normal) - distance; if (aDist*bDist < 0.0f) { float t = std::fabs(aDist)/(std::fabs(aDist) + std::fabs(bDist)); Vec2 v2Tmp = a.lerp(b, t); points[pointsCount] = v2Tmp; ++pointsCount; } } Point center = PhysicsShape::getPolyonCenter(points, pointsCount); for (int i = 0; i < pointsCount; i++) { points[i] = body->world2Local(points[i]); vcPoints.push_back(Vector2d(points[i].x, points[i].y)); } PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount); PRFilledPolygon* pNode = (PRFilledPolygon*)(body->getNode()); std::string sName = pNode->getTextureName(); //auto texture = Director::getInstance()->getTextureCache()->addImage("pineapple.png"); PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, sName.c_str()); filledPolygon->setPhysicsBody(polyon); filledPolygon->setPosition(body->getPosition() + normal * -40); filledPolygon->getPhysicsBody()->setTag(_sliceTag); filledPolygon->getPhysicsBody()->setGravityEnable(false); polyon->setVelocity(body->getVelocityAtWorldPoint(center)); polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); //polyon->applyImpulse(normal * -100); addChild(filledPolygon, 80); /* CPolygonSprite* pSprite = CPolygonSprite::create(); pSprite->initWithFile("pineapple.png", polyon, false); pSprite->setPosition(body->getPosition()); polyon->setTag(_sliceTag); addChild(pSprite); */ /* Node* node = Node::create(); PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount, PHYSICSBODY_MATERIAL_DEFAULT, -center); node->setPosition(center); node->setPhysicsBody(polyon); polyon->setVelocity(body->getVelocityAtWorldPoint(center)); polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); addChild(node); */ delete[] points; }
void CCutScene::clipPoly(PhysicsShapePolygon* shape, Point normal, float distance) { PhysicsBody* body = shape->getBody(); int count = shape->getPointsCount(); int pointsCount = 0; Point* points = new Point[count + 1]; Vector2dVector vcPoints; vcPoints.clear(); Vector2d v2Point(0, 0); for (int i=0, j=count-1; i<count; j=i, ++i) { Point a = body->local2World(shape->getPoint(j)); float aDist = a.dot(normal) - distance; if (aDist < 0.0f) { points[pointsCount] = a; ++pointsCount; } Point b = body->local2World(shape->getPoint(i)); float bDist = b.dot(normal) - distance; if (aDist*bDist < 0.0f) { float t = std::fabs(aDist)/(std::fabs(aDist) + std::fabs(bDist)); Vec2 v2Tmp = a.lerp(b, t); points[pointsCount] = v2Tmp; ++pointsCount; } } Point center = PhysicsShape::getPolyonCenter(points, pointsCount); for (int i = 0; i < pointsCount; i++) { points[i] = body->world2Local(points[i]); vcPoints.push_back(Vector2d(points[i].x, points[i].y)); } PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount); CFoodCut* pNode = (CFoodCut*)(body->getNode()); std::vector<int> vMaterials; vMaterials.clear(); vMaterials = pNode->getMaterials(); MATERIAL_ID eId = MI_MAX; if (vMaterials.size() != 0) { eId = (MATERIAL_ID)vMaterials[0]; } CFoodCut *filledPolygon = CFoodCut::create(eId, vcPoints, pNode->getPanziIndex(), pNode->getTouchedIndex()); filledPolygon->setPhysicsBody(polyon); int nTmp = rand() % 50 + 50; int nTmpRotate = rand() % 30 - 60; filledPolygon->setPosition(body->getPosition()); //filledPolygon->setRotation(filledPolygon->getRotation() + nTmpRotate); filledPolygon->getPhysicsBody()->setTag(_sliceTag); filledPolygon->getPhysicsBody()->setGravityEnable(false); polyon->setVelocity(body->getVelocityAtWorldPoint(center)); //polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); float fMass = polyon->getMass(); float fV = 80; float fImpulse = fMass * fV; float fTmpX = (float)(Random() % 30) / 100.0f - 0.15f; float fTmpY = (float)(rand() % 30) / 100.0f - 0.15f; polyon->applyImpulse((normal + Vec2(fTmpX, fTmpY)) * -fImpulse); polyon->setLinearDamping(0.8f); addChild(filledPolygon, 80); filledPolygon->setBirthTime(getCurTime()); m_vCutFoods.push_back(filledPolygon); m_nSliceCount ++; delete[] points; }