bool BaseDemo::onTouchBegan(Touch* touch, Event* event) { auto location = touch->getLocation(); auto shapeArr = _world->getShapes(location); PhysicsBody* body = nullptr; for(auto& obj : shapeArr) { if((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0) { body = obj->getBody(); break; } } if(body != nullptr) { Node* mouse = Node::create(); mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY)); mouse->getPhysicsBody()->setDynamic(false); mouse->setPosition(location); this->addChild(mouse); PhysicsJointPin* joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location); joint->setMaxForce(5000.0f * body->getMass()); _world->addJoint(joint); _mouses.insert(std::make_pair(touch->getID(), mouse)); return true; } return false; }
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) { CCLOG("%s", "touch"); auto location = touch->getLocation(); auto arr = _scene->getPhysicsWorld()->getShapes(location);//从物理世界得到多边形 PhysicsBody *body = nullptr; for (auto &obj : arr) { if ((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0) //得到刚体 { body = obj->getBody(); break; } } if (body != nullptr) { //创建一个刚体 Node *mouse = Node::create(); mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY)); mouse->getPhysicsBody()->setDynamic(false); mouse->setPosition(location); this->addChild(mouse); body->setLinearDamping(0.0f); //用图钉关节与点中刚体绑定 赋予力 可以拖动 PhysicsJointPin *joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location); joint->setMaxForce(5000.0f * body->getMass()); _scene->getPhysicsWorld()->addJoint(joint); _mouses.insert(std::make_pair(touch->getID(), mouse)); return true; } return false; }
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; }
void SinglePlayerScene::setupPhysics() { Size visibleSize = Director::getInstance()->getVisibleSize(); float visibleHeight = visibleSize.height; PhysicsWorld* world = this->getScene()->getPhysicsWorld(); world->setGravity(Vec2(0, (visibleHeight / 320.0f) * -98.0f)); world->setSpeed(GAME_SPEED); world->setDebugDrawMask(DEBUGDRAW_OPTIONS); Vec2 takyanPos = _takyan->getPosition(); PhysicsBody *takyanBody = PhysicsBody::createCircle(_takyan->getContentSize().width * BALL_RADIUS, PhysicsMaterial(0.01f, 1.0f, 1.0f)); //_takyan->getPhysicsBody(); float takyanMass = takyanBody->getMass(); float velocityLimit = takyanMass * 150.0f; takyanBody->setMass(takyanMass * 100.0f); takyanBody->setRotationEnable(false); takyanBody->setVelocityLimit(velocityLimit); _takyan->setPhysicsBody(takyanBody); PHYSICS_MASK(takyanBody, MASK_TAKYAN, MASK_BOUNDS | MASK_KICKER, MASK_BOUNDS | MASK_KICKER); Size takyanSize = _takyan->getContentSize(); takyanSize.height *= 0.8f; PhysicsMaterial material(0.0025f, 0.0f, 1.0f); PhysicsBody *takyanTailBody = PhysicsBody::createCircle(takyanSize.width, material); takyanTailBody->setMass(takyanMass * 0.01); takyanTailBody->setRotationEnable(false); takyanTailBody->setLinearDamping(1.0f); takyanTailBody->setVelocityLimit(velocityLimit * 0.5f); _takyanTail->setPhysicsBody(takyanTailBody); PHYSICS_MASK(takyanTailBody, MASK_TAKYAN_TAIL, 0, MASK_FLOOR); Vec2 diff = _takyanTail->getPosition() - _takyan->getPosition(); Vec2 anch1 = Vec2::ANCHOR_MIDDLE; Vec2 anch2 = Vec2::ANCHOR_MIDDLE; auto jointS = PhysicsJointLimit::construct(takyanBody, takyanTailBody, anch1, anch2, 0, diff.y); jointS->setCollisionEnable(false); world->addJoint(jointS); PhysicsBody *kickerBody = PhysicsBody::createCircle(_kicker->getContentSize().width * KICKER_RADIUS, PhysicsMaterial(1.0f, 1.0f, 1.0f)); //_kicker->getPhysicsBody(); kickerBody->setDynamic(false); kickerBody->setEnable(false); _kicker->setPhysicsBody(kickerBody); PHYSICS_MASK(kickerBody, MASK_KICKER, MASK_TAKYAN, MASK_TAKYAN); int takyans = MASK_TAKYAN | MASK_TAKYAN_TAIL; auto floorBody = PhysicsBody::createBox(_floorBounds->getContentSize(), PhysicsMaterial(1.0f, 0.3f, 0.7f)); //_floorBounds->getPhysicsBody(); floorBody->setDynamic(false); _floorBounds->setPhysicsBody(floorBody); PHYSICS_MASK(floorBody, MASK_BOUNDS, takyans, takyans); auto ceilingBody = PhysicsBody::createBox(_ceilingBounds->getContentSize(), PhysicsMaterial(1.0f, 0.3f, 0.7f)); ceilingBody->setDynamic(false); _ceilingBounds->setPhysicsBody(ceilingBody); PHYSICS_MASK(ceilingBody, MASK_BOUNDS, MASK_TAKYAN, MASK_TAKYAN); Size wallSize = _leftWallBounds->getContentSize(); wallSize.height *= 5.0f; auto leftWallBody = PhysicsBody::createBox(wallSize); //_leftWallBounds->getPhysicsBody(); leftWallBody->setDynamic(false); _leftWallBounds->setPhysicsBody(leftWallBody); PHYSICS_MASK(leftWallBody, MASK_WALLS, takyans, MASK_TAKYAN); auto rightWallBody = PhysicsBody::createBox(wallSize); //_rightWallBounds->getPhysicsBody(); rightWallBody->setDynamic(false); _rightWallBounds->setPhysicsBody(rightWallBody); PHYSICS_MASK(rightWallBody, MASK_WALLS, takyans, MASK_TAKYAN); }