void HitMeScene::hitMeFire(Ref* sender) { Point velocity_delta = Point(CCRANDOM_0_1(), CCRANDOM_0_1()) * 300; velocity_delta = (CCRANDOM_0_1() < 0.5f)? velocity_delta : -velocity_delta; PhysicsBody* body = _hitItem->getPhysicsBody(); body->setVelocity(body->getVelocity() + velocity_delta); body->setAngularVelocity(body->getAngularVelocity() + 5.0f * CCRANDOM_0_1()); }
void Ball::initPhysics(){ PhysicsBody *spriteBody = PhysicsBody::createCircle(this->getSprite()->getBoundingBox().size.width , PHYSICSBODY_MATERIAL_DEFAULT); spriteBody->setDynamic(true); spriteBody->getShape(0)->setRestitution(1.0f); spriteBody->getShape(0)->setFriction(0.0f); spriteBody->getShape(0)->setDensity(1.0f); spriteBody->getShape(0)->setMass(100); spriteBody->setGravityEnable(false); spriteBody->setVelocity(this->velocity); this->getSprite()->setPhysicsBody(spriteBody); }
Chopper* Chopper::createKnife(float v, float y) { Chopper* knife = Chopper::create(); knife->retain(); knife->chopper = Sprite::createWithSpriteFrameName("knife4.png"); knife->chopper->setScale(0.6); PhysicsBody *body = PhysicsBody::create(); body->setGravityEnable(true); //body->setGravityEnable(false); body->setVelocity(Vec2(0, -v)); knife->chopper->setPhysicsBody(body); knife->chopper->setPosition(Vec2(410, y)); return knife; }
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; }
bool GameSceneQuattro::onContactBegin(cocos2d::PhysicsContact &contact) { cocos2d::log("GameSceneQuattro::onContactBegin ....... inizio "); PhysicsBody *a = contact.getShapeA()->getBody(); PhysicsBody *b = contact.getShapeB()->getBody(); PhysicsBody *pallinaBody = nullptr ; if ( ! (3 == a->getCollisionBitmask() && 3 == b->getCollisionBitmask())){ if ( 3 == a->getCollisionBitmask() ) { // CCLOG( "GameSceneQuattro::onContactBegin COLLISION HAS OCCURED" ); pallinaBody = contact.getShapeA()->getBody(); } if ( 3 == b->getCollisionBitmask() ) { pallinaBody = contact.getShapeB()->getBody(); } if(pallinaBody){ _emitter->setPosition(pallinaBody->getNode()->getPosition()); _emitter->resetSystem(); _emitter->setDuration(0.5); //prendo tag if (12 == pallinaBody->getTag()){ cocos2d::log("GameSceneQuattro::onContactBegin colpita pallina 2 "); //deve variare la y // pallinaBody->applyForce( Vect(-50, -220) ); pallinaBody->setVelocity(Vec2(randomValueTra(900,900), randomValueTra(0,500))); } if (13 == pallinaBody->getTag()){ cocos2d::log("GameSceneQuattro::onContactBegin colpita pallina 3 "); pallinaBody->setVelocity(Vec2(randomValueTra(900,1900), randomValueTra(0,800))); } if (14 == pallinaBody->getTag()){ cocos2d::log("GameSceneQuattro::onContactBegin colpita pallina 4 "); pallinaBody->setVelocity(Vec2(randomValueTra(900,1900), randomValueTra(0,800))); } if (15 == pallinaBody->getTag()){ cocos2d::log("GameSceneQuattro::onContactBegin colpita pallina 5 "); pallinaBody->setVelocity(Vec2(randomValueTra(900,1900), randomValueTra(-400,800))); } } } if (PIETRA_TAG == a->getTag()){ // cocos2d::log("GameSceneQuattro::onContactBegin ....... 1 "); a-> getNode()->stopAllActions(); a-> getNode()->setVisible(false); a->removeFromWorld(); //a->setEnable(false); // a->applyForce( Vect(100, 78) ); } if (PIETRA_TAG == b->getTag() ){ // cocos2d::log("GameSceneQuattro::onContactBegin ....... 2 "); b-> getNode()->stopAllActions(); b-> getNode()->setVisible(false); b->removeFromWorld(); // b->setEnable(false); // b->applyForce( Vect(100, 78) ); } CCLOG("Forse megio cosi recupero i tag e poi faccio tutto" ); auto sp = (Sprite*)contact.getShapeA()->getBody()->getNode(); int tag = sp->getTag(); CCLOG("onContactBegin tag di A : %d", tag); auto spb = (Sprite*)contact.getShapeA()->getBody()->getNode(); int tagb = spb->getTag(); CCLOG("onContactBegin tag di B : %d", tagb); return true; }