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); } } }
// PhysicsShapeEdgeSegment PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(const Vec2& a, const Vec2& b, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { PhysicsShapeEdgeSegment* shape = new (std::nothrow) PhysicsShapeEdgeSegment(); if (shape && shape->init(a, b, material, border)) { shape->autorelease(); return shape; } CC_SAFE_DELETE(shape); return nullptr; }
// PhysicsShapeEdgeSegment PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(Point a, Point b, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/) { PhysicsShapeEdgeSegment* shape = new PhysicsShapeEdgeSegment(); if (shape && shape->init(a, b, material, border)) { shape->autorelease(); return shape; } CC_SAFE_DELETE(shape); return nullptr; }