本文整理汇总了C++中PhysicsBody::addShape方法的典型用法代码示例。如果您正苦于以下问题:C++ PhysicsBody::addShape方法的具体用法?C++ PhysicsBody::addShape怎么用?C++ PhysicsBody::addShape使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhysicsBody
的用法示例。
在下文中一共展示了PhysicsBody::addShape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
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;
}
示例2: createPhysicsBody
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);
}
}
}
示例3: setPhysical
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);
}
示例4: createPolygon
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;
}
示例5: createPolygon
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;
}
示例6: createCircle
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;
}
示例7: createBox
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;
}
示例8: createBox
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;
}
示例9: bodyFormJson
//从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;
}
示例10: createEdgeSegment
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;
}
示例11: createEdgeChain
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;
}
示例12: createEdgeBox
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;
}
示例13: createEdgeBox
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;
}
示例14: createEdgePolygon
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;
}
示例15: addBallObstacle
/*
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);
}