本文整理汇总了C++中PhysicsBody::applyImpulse方法的典型用法代码示例。如果您正苦于以下问题:C++ PhysicsBody::applyImpulse方法的具体用法?C++ PhysicsBody::applyImpulse怎么用?C++ PhysicsBody::applyImpulse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhysicsBody
的用法示例。
在下文中一共展示了PhysicsBody::applyImpulse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: callbackC
void LevelFifteen::callbackC(Node* sender)
{
auto visibleSize = Director::getInstance()->getVisibleSize();
auto pictureName = 1 + rand() % 27;
auto str = String::createWithFormat("%d.png", pictureName)->getCString();
auto ballTwo2 = Sprite::createWithSpriteFrameName(str);
ballTwo2->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height - 90));
PhysicsBody* ballBodyTwo = nullptr;
if (pictureName <= 9)
{
ballBodyTwo = PhysicsBody::createBox(ballTwo2->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT);
}
else if (pictureName > 9 && pictureName <= 18)
{
ballBodyTwo = PhysicsBody::createCircle(ballTwo2->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT);
}
else if (pictureName > 18 && pictureName <= 21)
{
Vec2 vec[] = {
Vec2(-22.50000, 22.00000),
Vec2(24.00000, 10.00000),
Vec2(-10.00000, -24.00000)
};
ballBodyTwo = PhysicsBody::createPolygon(vec, 3);
}
else if (pictureName > 21 && pictureName <= 24)
{
Vec2 vec[] = {
Vec2(-15.50000, 15.00000),
Vec2(15.50000, 6.00000),
Vec2(-7.00000, -15.50000)
};
ballBodyTwo = PhysicsBody::createPolygon(vec, 3);
}
else if (pictureName > 24 && pictureName <= 27)
{
Vec2 vec[] = {
Vec2(-8.00000, 8.00000),
Vec2(7.50000, 3.00000),
Vec2(-4.00000, -7.50000)
};
ballBodyTwo = PhysicsBody::createPolygon(vec, 3);
}
//是否设置物体为静态
//ballBodyTwo->setDynamic(false);
ballBodyTwo->getShape(0)->setRestitution(1.0f);
ballBodyTwo->getShape(0)->setFriction(0.0f);
ballBodyTwo->getShape(0)->setDensity(1.0f);
ballBodyTwo->setGravityEnable(false);
ballBodyTwo->setCategoryBitmask(2);// 分类掩码
ballBodyTwo->setCollisionBitmask(1 | 2 | 4 | 8);// 碰撞掩码
ballBodyTwo->setContactTestBitmask(1 | 8);// 接触测试掩码
ballBodyTwo->addMass(1000.0f);
int flag = CCRANDOM_0_1() * 2;
auto num = 0;
switch (flag)
{
case 0:
num = -1;
break;
case 1:
num = 1;
break;
default:
break;
}
auto force1 = Vec2(CCRANDOM_0_1()*10000.0f*num, -300000.0f);
ballBodyTwo->applyImpulse(force1);
ballTwo2->setPhysicsBody(ballBodyTwo);
this->addChild(ballTwo2);
}
示例2: clipPoly
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;
}