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); } } }
void PipeSprite::configPipeRandom() { Size visiableSize = Director::getInstance()->getVisibleSize(); Vec2 visiableOrigin = Director::getInstance()->getVisibleOrigin(); /**********************config upper pipe********************************/ mUpperPipe->setPosition(Vec2(0, mPipeAccessHeight + 60)); upperPipeExpand->setPosition(mUpperPipe->getPosition().x, mUpperPipe->getContentSize().height); upperPipeExpand->setScaleY((visiableSize.height - mUpperPipe->getPosition().y - mUpperPipe->getContentSize().height) / upperPipeExpand->getContentSize().height); /**********************config upper pipe********************************/ /**********************config bottom pipe********************************/ mBottomPipe->setPosition(Vec2(0, mPipeAccessHeight - 60)); bottomPipeExpand->setPosition(0, -mBottomPipe->getPosition().y + mLandHeight); bottomPipeExpand->setScaleY((mBottomPipe->getPosition().y - mLandHeight) / bottomPipeExpand->getContentSize().height); /**********************config bottom pipe********************************/ /****************add upper physics body*******************/ if (mUpperPipe->getPhysicsBody() != nullptr) { mUpperPipe->removeComponent(mUpperPipe->getPhysicsBody()); } PhysicsBody * newUpperPipeBody = PhysicsBody::createBox(Size(mUpperPipe->getContentSize().width, visiableSize.height - mUpperPipe->getPosition().y), PhysicsMaterial(1.0, 1.0, 0)); newUpperPipeBody->setPositionOffset(Vec2(0, (visiableSize.height - mUpperPipe->getPosition().y - mUpperPipe->getContentSize().height) / 2)); newUpperPipeBody->setGravityEnable(false); newUpperPipeBody->setDynamic(false); newUpperPipeBody->setContactTestBitmask(1); newUpperPipeBody->setCollisionBitmask(2); mUpperPipe->setPhysicsBody(newUpperPipeBody); /****************add upper physics body*******************/ /****************add bottom physics body*******************/ if (mBottomPipe->getPhysicsBody() != nullptr) { mBottomPipe->removeComponent(mBottomPipe->getPhysicsBody()); } PhysicsBody * newBottomPipeBody = PhysicsBody::createBox(Size(mBottomPipe->getContentSize().width, mBottomPipe->getPosition().y - mLandHeight + mBottomPipe->getContentSize().height), PhysicsMaterial(1.0, 1.0, 0)); newBottomPipeBody->setPositionOffset(Vec2(0, -(mBottomPipe->getPosition().y - mLandHeight) / 2)); newBottomPipeBody->setGravityEnable(false); newBottomPipeBody->setDynamic(false); newBottomPipeBody->setContactTestBitmask(1); newBottomPipeBody->setCollisionBitmask(2); mBottomPipe->setPhysicsBody(newBottomPipeBody); /****************add bottom physics body*******************/ }
Sprite* Stage::addPhysicsBody(cocos2d::TMXLayer *layer, cocos2d::Vec2 &coordinate) { // タイル1枚の大きさを取り出す auto tileSize = _tiledMap->getTileSize(); // タイルのスプライトを取り出す auto sprite = layer->getTileAt(coordinate); if (sprite) { // タイルのIDを取り出す auto gid = layer->getTileGIDAt(coordinate); // タイルのプロパティをmapで取り出す auto property = _tiledMap->getPropertiesForGID(gid); if (property.isNull() || property.getType() != Value::Type::MAP) { return nullptr; } auto properties = property.asValueMap(); // プロパティの中からcategoryの値をintとして取り出す auto category = properties.at("category").asInt(); auto material = PhysicsMaterial(); material.friction = 0; material.restitution = 0.1; // 剛体を設置する auto physicsBody = PhysicsBody::createBox(sprite->getContentSize(), material); // 剛体を固定する physicsBody->setDynamic(false); // 剛体にカテゴリをセットする physicsBody->setCategoryBitmask(category); // 剛体と接触判定を取るカテゴリを指定する physicsBody->setContactTestBitmask(static_cast<int>(TileType::PLAYER)); // 剛体と衝突を取るカテゴリを指定する physicsBody->setCollisionBitmask(static_cast<int>(TileType::PLAYER)); // アニメーションを付ける // プロパティにanimationの値があったら if (!properties["animation"].isNull()) { auto animationSprite = properties["animation"].asString(); auto animationCount = properties["animationCount"].asInt(); sprite->removeFromParent(); this->addChild(sprite); Vector<SpriteFrame *> frames; auto scale = 0.5; for (int i = 0; i < animationCount; ++i) { auto frame = SpriteFrame::create(animationSprite, Rect(tileSize.width * i * scale, 0, tileSize.width * scale, tileSize.height * scale)); frames.pushBack(frame); } auto animation = Animation::createWithSpriteFrames(frames); animation->setDelayPerUnit(0.15); sprite->runAction(RepeatForever::create(Animate::create(animation))); } sprite->setAnchorPoint(Vec2::ANCHOR_MIDDLE); sprite->setPhysicsBody(physicsBody); return sprite; } return nullptr; }
bool DamagePowerUp::init() { if ( !PowerUp::init() ){ return false; } std::ostringstream stream; stream << "powerups/DP" << ((int)_damageLevel) << ".png"; setTexture(stream.str()); setOpacity(0); auto fadeIn = FadeIn::create(1.0f); runAction(fadeIn); // [[-- sets the physic body auto physicBody = PhysicsBody::createBox( Size( getContentSize().width , getContentSize().height ) , PhysicsMaterial(0, 0.02, 0) ); physicBody->setCategoryBitmask(Type::POWERUP); physicBody->setCollisionBitmask( Entity::Type::SHIP ); physicBody->setContactTestBitmask(Type::SHIP); //physicBody->setTag(); setPhysicsBody( physicBody ); // --]] _stats.damage = 5; // Tells Cocos2d-x to call the Node's update function scheduleUpdate(); return true; }
void SimplePlatformerScene::drawCollisionTiles() { TMXLayer* collisionLayer = _tileMapNode->getLayer("edgeLayer"); auto mapSize = _tileMapNode->getMapSize(); auto tileSize = _tileMapNode->getTileSize(); Sprite* tile = nullptr; Point pos; for(int i = 0; i < mapSize.width; i++) { for(int j = 0; j < mapSize.height; j++) { tile = collisionLayer->getTileAt(Point(i, j)); if(tile != nullptr) { const ValueMap property = _tileMapNode->getPropertiesForGID(collisionLayer->getTileGIDAt(Point(i, j))).asValueMap(); bool collidable = property.at("collidable").asBool(); if(collidable) { pos = collisionLayer->getPositionAt(Point(i, j)); makeBoxObjAt(tile, tileSize, false, PhysicsMaterial(0.2f, 0.5f, 0.5f)); } } } } }
Pixel::Pixel(Layer* layer) { origin = Director::getInstance()->getVisibleOrigin(); visibleSize = Director::getInstance()->getVisibleSize(); pixelTexture = Sprite::create("Pixel.png"); pixelTexture->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2); pixelBody = PhysicsBody::createBox(pixelTexture->getContentSize(), PhysicsMaterial(0,0,0)); //I am a pixel pixelBody->setCategoryBitmask(eObjectBitmask::PIXEL); //I collide with ... //pixelBody->setCollisionBitmask(eObjectBitmask::PIPE); pixelBody->setContactTestBitmask(eObjectBitmask::PIPE | eObjectBitmask::LINE); ////////////////////// pixelTexture->setPhysicsBody(pixelBody); layer->addChild(pixelTexture, 100); isFalling = true; velocity = Vec2(0, 0); rotation = 0.0f; isDead = false; }
void Devil::InitSprite() { auto body = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(100, 1, 1)); body->setDynamic(true); body->setGravityEnable(false); body->setRotationEnable(false); body->setCollisionBitmask(MONSTER_BODY_COLLISION_BITMASK); body->setContactTestBitmask(true); this->setPhysicsBody(body); this->setUserData(this); hpBgSpr = Sprite::create("Monster/hpBg.png"); this->addChild(hpBgSpr, ZINDEX_MONSTER_HP); hpCurrentSpr = Sprite::create("Monster/hpCurrent.png"); this->addChild(hpCurrentSpr, ZINDEX_MONSTER_HP); auto origin = Director::getInstance()->getVisibleOrigin(); hpBgSpr->setPosition(Vec2(this->getContentSize().width / 2, this->getPosition().y + this->getContentSize().height + hpBgSpr->getContentSize().height / 2)); hpCurrentSpr->setPosition(hpBgSpr->getPosition()); hpBgSpr->setOpacity(0); hpCurrentSpr->setOpacity(0); fury = CSLoader::createNode("Effect/Rage/Rage.csb"); fury->setPosition(this->getContentSize().width * 0.5f, this->getContentSize().height * 0.5f); this->addChild(fury); fury->setVisible(false); auto visibleSize = Director::getInstance()->getVisibleSize(); d = Dragon::create(layer); d->setPosition(visibleSize.width*0.7f, visibleSize.height*0.7f); layer->addChild(d, ZINDEX_MONSTER_SPRITE); d->scheduleUpdate(); }
bool HeroSprite::init() { bool bRet = false; do { CC_BREAK_IF(!Sprite::initWithSpriteFrameName("heroStand_0001.png")); Size heroSize = this->getContentSize(); PhysicsBody *body = PhysicsBody::createBox(heroSize-Size(0,16), PhysicsMaterial(0, 0, 0)); body->setLinearDamping(0.0f); body->setDynamic(true); body->setGravityEnable(true); body->setTag(TAG_HERO_PHYS_BODY); body->setCategoryBitmask(1<<2); body->setContactTestBitmask(1<<0 | 1<<1); body->setMass(50); body->setRotationEnable(false); this->setPhysicsBody(body); mState = STATE_IDLE; mRunAnimate = NULL; mSmokeRunAnimate = NULL; bRet = true; } while(0); return bRet; }
void Person::addPhysics() { auto size = (this->getBoundingBox()).size; log("%lf %lf", size.width, size.height); auto material = PhysicsMaterial(100.0f, 0.01f, 1.0f); if(_type == TYPE::HERO) size.width /= 2; PhysicsBody *body = PhysicsBody::createBox(Size(size.width,size.height),material); // body->addShape(PhysicsShapeBox::create(Size(size.width,size.height),material)); body->setCategoryBitmask(_type); if(_type == TYPE::MONSTER) { body->setCollisionBitmask(TYPE::MONSTER | TYPE::BRICK | TYPE::GROUND | TYPE::TANGH | TYPE::BULLET | TYPE::BOSS | TYPE::PLANK); body->setContactTestBitmask(TYPE::MONSTER | TYPE::HERO | TYPE::BRICK | TYPE::TANGH | TYPE::BULLET | TYPE::BOSS); } else if(_type == TYPE::HERO) { body->setCollisionBitmask( TYPE::HERO | TYPE::GROUND | TYPE::TANGH | TYPE::PLANK); body->setContactTestBitmask(TYPE::MONSTER | TYPE::HERO | TYPE::GROUND | TYPE::TANGH | TYPE::BULLET | TYPE::TRAP | TYPE::BOSS | TYPE::BULLETENEMY | TYPE::PLANK | TYPE::BUFF); } else if(_type == TYPE::BOSS) { body->setCollisionBitmask( TYPE::HERO | TYPE::GROUND | TYPE::BOSS | PLANK); body->setContactTestBitmask( TYPE::HERO | TYPE::GROUND | TYPE::BOSS); } body->setDynamic(true); body->setLinearDamping(0.0f); body->setRotationEnable(false); body->setGravityEnable(true); this->setPhysicsBody(body); }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto edgeBody = PhysicsBody::createEdgeBox( visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3 ); auto edgeNode = Node::create(); edgeNode ->setPosition( Point( visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y ) ); edgeNode->setPhysicsBody( edgeBody ); this->addChild( edgeNode ); { auto sprite = Sprite::create( "CloseNormal.png" ); sprite->setPosition( Point( visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y ) ); auto spriteBody = PhysicsBody::createBox( sprite->getContentSize( ), PhysicsMaterial( 0, 1, 0 ) ); spriteBody->setDynamic( false ); sprite->setPhysicsBody( spriteBody ); this->addChild( sprite ); } { auto sprite = Sprite::create( "CloseNormal.png" ); sprite->setPosition( Point( visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y + 200 ) ); auto spriteBody = PhysicsBody::createBox( sprite->getContentSize( ), PhysicsMaterial( 0, 1, 0 ) ); sprite->setPhysicsBody( spriteBody ); this->addChild( sprite ); } return true; }
bool Bullet::init (int tag) { // if (!Sprite::initWithFile("faceball.png")){ if (!Sprite::initWithFile("ball.png")){ return false; } m_tag = tag; this->setColor(_tagColor[tag]); // 大きさをランダムにとりあえず bulletSize = _bulSize[tag]; // this->setContentSize(cocos2d::Size (bulletSize * 20, bulletSize * 20)); // もともと設定されてたもの // なんか、物理エンジンがおかしくなるような気がする // これを消すと落ち着く this->setScale(0.13f * bulletSize); //this->setScale(0.043f * bulletSize); // 密度、反発、摩擦 // まるいやつ。回転がわかりズラい // auto pBall = PhysicsBody::createCircle(bulletSize, // PhysicsMaterial(10.0f, 0.6f, 0.3f)); // しかくいやつ。これは四角すぎる // auto pBall = PhysicsBody::createBox(cocos2d::Size(bulletSize, bulletSize), PhysicsMaterial(1.0f, 0.6f, 0.3f)); Point spritePoints[12] = { Point(-1, 0.333) *= bulletSize, Point(-0.666, 0.5) *= bulletSize, Point(-0.333, 0.666) *= bulletSize, Point(0.333, 0.666) *= bulletSize, Point(0.666, 0.5) *= bulletSize, Point(1, 0.333) *= bulletSize, Point(1, -0.333) *= bulletSize, Point(0.666, -0.5) *= bulletSize, Point(0.333, -0.666) *= bulletSize, Point(-0.333, -0.666) *= bulletSize, Point(-0.666, -0.5) *= bulletSize, Point(-1, -0.333) *= bulletSize }; // for (Point target : spritePoints) { // target *= bulletSize; // } // 密度、反発、摩擦 auto pBall = PhysicsBody::createPolygon(spritePoints, 12, PhysicsMaterial(1.0f, 0.0f, 1.0f)); // auto pBall = PhysicsBody::createPolygon(spritePoints, 12, PhysicsMaterial(1.0f, 0.6f, 0.3f)); pBall->setDynamic(true); pBall->setRotationEnable(true); // 回転率?なのか?これを無限にすると回転しない // pBall->setMoment(PHYSICS_INFINITY); // pBall->setMoment(bulletSize); pBall->setGravityEnable(true); //log("%f",pBall->getMass()); // pBall->setMass(1.0); pBall->setVelocityLimit(800); this->setPhysicsBody(pBall); return true; }
// Creates an instance of Player // @param png - The path to the PNG representation of the sprite // @param plist - The path to the PLIST representation of the sprite Player* Player::create() { // Create an instance of Player Player* playerSprite = new Player(); // Reset travelled distance playerSprite->setDistanceTravelled(0); // Get resource loader instance ResourceLoader resLoader = ResourceLoader::getInstance(); // Get running animation Vector<SpriteFrame*> runningAnimation = resLoader.getAnimation(PLAYER_ANIMATION_RUNNING); // Generate player movement sprites if (playerSprite->initWithSpriteFrame(runningAnimation.at(0))) { auto animation = Animation::createWithSpriteFrames(runningAnimation); // Set delay between frames animation->setDelayPerUnit(PLAYER_SPRITE_DELAY_RUNNING); //Create running action auto runningAction = RepeatForever::create(Animate::create(animation)); //Set tag for action runningAction->setTag(PLAYER_ANIMATION_RUNNING); // Run animation forever playerSprite->runAction(runningAction); // Set properties playerSprite->autorelease(); playerSprite->initOptions(); playerSprite->setScale(Director::getInstance()->getWinSize().width * 1.8 / 800); playerSprite->scheduleUpdate(); // Add weapon Weapon* weapon = Weapon::create(); playerSprite->setWeapon(weapon); //Create physical body auto spriteBody = PhysicsBody::createBox(playerSprite->boundingBox().size, PhysicsMaterial(1.0f, 0.5f, 0.5f)); spriteBody->setAngularVelocityLimit(0.0f); spriteBody->setMass(1); spriteBody->setCollisionBitmask(PLAYER_COLLISION_BITMASK); spriteBody->setContactTestBitmask(true); playerSprite->setPhysicsBody(spriteBody); // Return return playerSprite; } CC_SAFE_DELETE(playerSprite); return NULL; }
void Player::resumePlayer() { auto spriteBody = PhysicsBody::createBox(this->boundingBox().size, PhysicsMaterial(1.0f, 0.5f, 0.5f)); spriteBody->setAngularVelocityLimit(0.0f); spriteBody->setMass(1); spriteBody->setCollisionBitmask(PLAYER_COLLISION_BITMASK); spriteBody->setContactTestBitmask(true); this->setPhysicsBody(spriteBody); this->getPhysicsBody()->setVelocity(velocityOnPause); }
// 添加敌人 void GameScene::addEnemy() { this->enemy.clear(); // 圆形 auto circle = Sprite::create("Circle.png"); auto circle_body = PhysicsBody::createCircle(20, PhysicsMaterial(0.0, 1.0, 0.0)); circle->setPhysicsBody(circle_body); circle->setPosition(100, WIN_SIZE.height - 100); this->enemy.pushBack(circle); // 正方形 auto square = Sprite::create("Square.png"); auto square_body = PhysicsBody::createBox(Size(50, 50), PhysicsMaterial(0.0, 1.0, 0.0)); square->setPhysicsBody(square_body); square->setPosition(WIN_SIZE.width - 100, WIN_SIZE.height - 100); this->enemy.pushBack(square); // 长方形:竖 auto rect_shu = Sprite::create("Rect_Shu.png"); auto rect_shu_body = PhysicsBody::createBox(Size(20, 50), PhysicsMaterial(0.0, 1.0, 0.0)); rect_shu->setPhysicsBody(rect_shu_body); rect_shu->setPosition(100, 100); this->enemy.pushBack(rect_shu); // 长方形:横 auto rect_heng = Sprite::create("Rect_Heng.png"); auto rect_heng_body = PhysicsBody::createBox(Size(60, 20), PhysicsMaterial(0.0, 1.0, 0.0)); rect_heng->setPhysicsBody(rect_heng_body); rect_heng->setPosition(WIN_SIZE.width - 100, 100); this->enemy.pushBack(rect_heng); for (int i = 0; i < this->enemy.size(); i++) { Sprite* e = this->enemy.at(i); e->setScale(0.2f); e->getPhysicsBody()->setCategoryBitmask(2); e->getPhysicsBody()->setCollisionBitmask(6); e->getPhysicsBody()->setContactTestBitmask(1); this->addChild(e, 0, i); } }
void Unit::SetDef(const UnitInfo& info) { Object::SetDef(info.m_DefInfo); getPhysicsBody()->getFirstShape()->setMaterial(PhysicsMaterial(0, 0, 0)); getPhysicsBody()->setCategoryBitmask(OBJ_UNIT); getPhysicsBody()->setCollisionBitmask(OBJ_ALL - OBJ_UNIT - OBJ_DEF - OBJ_LASER); getPhysicsBody()->setContactTestBitmask(OBJ_LASER + OBJ_MISSILE + OBJ_DEF); getPhysicsBody()->setVelocityLimit(m_DefInfo.m_MoveSpeed * 1.5f); SetHpBar("Image/Interface/hp_bar_small.png"); m_UnitInfo = info; }
// 添加玩家 void GameScene::addPlayer() { this->player = Sprite::create("Player.png"); auto body = PhysicsBody::createCircle(15.0f, PhysicsMaterial(0.0, 0.0, 0.0)); this->player->setPhysicsBody(body); this->player->setPosition(WIN_SIZE / 2); this->addChild(this->player); this->player->getPhysicsBody()->setCategoryBitmask(1); this->player->getPhysicsBody()->setCollisionBitmask(0); this->player->getPhysicsBody()->setContactTestBitmask(6); }
// 添加边界 void GameScene::addEdge() { this->edge = Sprite::create(); auto body = PhysicsBody::createEdgeBox(WIN_SIZE, PhysicsMaterial(100.0, 1.0, 0.0), 5.0f); this->edge->setPhysicsBody(body); this->edge->setPosition(WIN_SIZE / 2); this->addChild(this->edge); this->edge->getPhysicsBody()->setCategoryBitmask(4); this->edge->getPhysicsBody()->setCollisionBitmask(2); this->edge->getPhysicsBody()->setContactTestBitmask(1); }
bool Player::init() { if (!Sprite::initWithFile("player.png")) { return false; } // 1フレームの画像サイズを取得する auto frameSize = Size(this->getContentSize().width / FRAME_COUNT, this->getContentSize().height); // テクスチャの大きさを1フレーム分にする this->setTextureRect(Rect(0, 0, frameSize.width, frameSize.height)); // Vector<SpriteFrame *> frames; for (int i = 0; i < FRAME_COUNT; ++i) { // 1コマずつアニメーションを作成する auto frame = SpriteFrame::create("player.png", Rect(frameSize.width * i, 0, frameSize.width, frameSize.height)); // frames.pushBack(frame); } //アニメーションを作成して動かす auto animation = Animation::createWithSpriteFrames(frames); animation->setDelayPerUnit(0.05); this->runAction(RepeatForever::create(Animate::create(animation))); //剛体を作成 auto material = PhysicsMaterial(); auto body = PhysicsBody::createCircle(this->getContentSize().width / 2.0); //摩擦力 material.friction = 0; // 剛体の回転を無効にする body->setRotationEnable(false); // カテゴリをPLAYERにセットする body->setCategoryBitmask(static_cast<int>(Stage::TileType::PLAYER)); // 壁とのみ衝突する body->setCollisionBitmask(static_cast<int>(Stage::TileType::WALL)); // 全ての剛体と接触判定を行う body->setContactTestBitmask(INT_MAX); this->setPhysicsBody(body); // 初期加速度を設定する _acceleration = INITIAL_ACCELERATION; this->scheduleUpdate(); return true; }
BStar::BStar() { material = PhysicsMaterial(STAR_PLANK_DENSITY, STAR_PLANK_RESTITUTION, STAR_PALNK_FRICTION); bitmask = STAR_COLLISION_BITMASK; SonarSysBodyParser::getInstance()->parseJsonFile(STAR_JSON_FILE); sprite = Sprite::create(STAR_SPRITE); body = SonarSysBodyParser::getInstance()->bodyFormJson(sprite, STAR_NAME_OBJECT_IN_JSON, material); body->setDynamic(true); sprite->setPhysicsBody(body); }
bool BrokableWall::init( const ValueMap &valueMap , const ValueMap &gidProperties ) { if (!BaseEntity::initWithMap(valueMap )) return false; //std::string img = Utils::getWallByType(this->m_nType); std::string img = gidProperties.find("source")->second.asString(); m_durability = gidProperties.find("durability")->second.asInt(); if (!BaseEntity::initWithFile(img)) return false; setPositionAndRotation(); this->setPhysicsBody(PhysicsBody::createEdgeBox(this->getContentSize(), PhysicsMaterial(1.0f, 0.4f, 1.0f), 0));//ÃÜ¶È µ¯Á¦ Ħ²ÁÁ¦ setContactTestBitmask(0x0002); setEntityType(Type_Brokable); return true; }
void GameScene::addPlayer(TMXTiledMap *tmx){ auto physicsBody = PhysicsBody::createBox(Size(63, 155), PhysicsMaterial(0, 0, 0)); physicsBody->setGravityEnable(false); Sprite *player = Sprite::create("flying1.png"); player->setPhysicsBody(physicsBody); mPlayer = Player::create(); mPlayer->setPosition(Point(-33, 180)); mPlayer->bindSprite(player); this->addChild(mPlayer); mPlayer->addAnimationCache(); }
fireBullet * fireBullet::create(){ auto obj = new fireBullet(); if(obj->initWithFile("bullet.png")){ auto cirle = PhysicsBody::createCircle(obj->getContentSize().width,PhysicsMaterial(0,1,0)); obj->setPhysicsBody(cirle); cirle->setContactTestBitmask(true); obj->autorelease(); //obj->addChild(sp); } return obj; }
void gameLevel2::fireTheFuckinBullet() { if (isShipAlive) { if (soundState)CocosDenshion::SimpleAudioEngine::getInstance()->stopEffect(shipBulletSound); //BULLET FLAME bulletFlame = Sprite::create("FlameEffect/red.png"); bulletFlame->setPosition(Vec2(ship->getPositionX(), ship->getPositionY() + ship->getContentSize().height / 2 + 10)); bulletFlame->setScale(0.3); this->addChild(bulletFlame, 2); bulletFlame->runAction(Sequence::create(DelayTime::create(0.05), RemoveSelf::create(), NULL)); //BULLET CREATION auto bullet = Sprite::create("FlameEffect/redbullet.png"); bullet->setPosition(Vec2(ship->getPosition().x, ship->getPosition().y + ship->getContentSize().height / 2)); bullet->setScale(0.5); bullet->setTag(2); bulletPhysics = PhysicsBody::createBox(bullet->getContentSize(), PhysicsMaterial(0.1, 1.0, 0)); bulletPhysics->setDynamic(true); bulletPhysics->setCategoryBitmask(0x05); bulletPhysics->setCollisionBitmask(0); bulletPhysics->setContactTestBitmask(0x09); bullet->setPhysicsBody(bulletPhysics); this->addChild(bullet, 2); auto bulletMove = MoveTo::create(0.7, Vec3(bullet->getPosition().x, visibleSize.height + 30, 0)); auto easeMove = EaseIn::create(bulletMove, 0.7); auto seq = Sequence::create(easeMove, RemoveSelf::create(), NULL); bullet->runAction(seq); if (soundState) shipBulletSound = CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Sounds/wav/bullet1.wav"); } }
BoundWall::BoundWall(WallType type, Size screenSize) { PhysicsBody* body = nullptr; switch (type) { case UP: this->setAnchorPoint(Vec2(0.5f, 0)); this->setPosition(Vec2(screenSize.width / 2, screenSize.height)); body = PhysicsBody::createBox(Size(screenSize.width,5), PhysicsMaterial(1, 0, 0)); break; case DOWN: this->setAnchorPoint(Vec2(0.5f, 1)); this->setPosition(Vec2(screenSize.width / 2, 0)); body = PhysicsBody::createBox(Size(screenSize.width, 5), PhysicsMaterial(1, 0, 0)); break; case LEFT: this->setAnchorPoint(Vec2(1, 0.5f)); this->setPosition(Vec2(0, screenSize.height / 2)); body = PhysicsBody::createBox(Size(5, screenSize.height), PhysicsMaterial(1, 0, 0)); break; case RIGHT: this->setAnchorPoint(Vec2(0, 0.5f)); this->setPosition(Vec2(screenSize.width, screenSize.height / 2)); body = PhysicsBody::createBox(Size(5, screenSize.height), PhysicsMaterial(1, 0, 0)); break; default: break; } body->setGravityEnable(false); body->setDynamic(false); body->setTag(Tags::GROUND); body->setCollisionBitmask(true); body->setContactTestBitmask(true); this->setPhysicsBody(body); }
bool Rock::init() { if (!Sprite::initWithFile("rock.png")) { return false; } PhysicsBody* rockBody = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(DEFAULT_ROCK_MATERIAL)); rockBody->setDynamic(false); rockBody->setContactTestBitmask(ROCK_CONTACT_MASK); rockBody->setCategoryBitmask(ROCK_CATEGORY); rockBody->setCollisionBitmask(ROCK_COLLISION_MASK); rockBody->setGravityEnable(false); this->setPhysicsBody(rockBody); return true; }
void EnemyPlane::initOptions() { auto plane_body = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(0,0,0)); plane_body->setCollisionBitmask(2); plane_body->setContactTestBitmask(true); plane_body->setDynamic(true);// Физика this->setTag(2); this->setPhysicsBody(plane_body); this->setScale(0.15); _isEnemy = true; _baseHP = 100; _currentHP = _baseHP; _maximalSpeed = 50.0f; _movementDirection = Vec2(); this->setRotation(180); // this->setColor(Color3B(255,0,5)); }
Scene* TestScene::create() { TestScene* scene = TestScene::createWithPhysics(); scene->getPhysicsWorld()->setGravity(Vect(0.0f, -700.0f)); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); scene->scheduleUpdate(); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); PhysicsBody* edgeBody = PhysicsBody::createEdgeBox(visibleSize, PhysicsMaterial(0.1f, 0.0f, 0.5f), 3); Node* edgeNode = Node::create(); edgeNode->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); edgeNode->setPhysicsBody(edgeBody); scene->addChild(edgeNode); Player* sprite = Player::create(); sprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); /*Sprite* sprite = Sprite::create("testSprite.png"); sprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); MyBodyParser::getInstance()->parseJsonFile("test.json"); auto spriteBody = MyBodyParser::getInstance()->bodyFormJson(sprite, "Test", PhysicsMaterial(1, 1, 0)); if (spriteBody != nullptr) { spriteBody->setDynamic(true); sprite->setPhysicsBody(spriteBody); }*/ scene->addChild(sprite); return scene; }
Sprite* EnemyShip::create(char *filename) { ship = Sprite::create(filename); ship->setRotation(180); auto physicsBody = PhysicsBody::createBox(ship->getContentSize(), PhysicsMaterial(0.1, 1.0, 0)); physicsBody->setDynamic(true); physicsBody->setCategoryBitmask(0x09); physicsBody->setCollisionBitmask(0); physicsBody->setContactTestBitmask(0x05); ship->setPhysicsBody(physicsBody); return ship; }
Player * Player::create() { Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); Player * player = new Player(); if (player && player->initWithFile("GameScreen/player.png")) { player->autorelease(); player->initPlayer(); player->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height + origin.y)); auto player1Body = PhysicsBody::createBox(player->getContentSize(), PhysicsMaterial(0, 0, 0)); player1Body->setCollisionBitmask(0x000001); player1Body->setRotationEnable(false); //player1Body->setCategoryBitmask(0x02); player1Body->setContactTestBitmask(true); player1Body->setTag(10); player1Body->setDynamic(true); //player1Body->setVelocity(Vect(100, 247)); player1Body->setLinearDamping(0); //player1Body->applyForce(Vect(100, 245)); //Assign the body to sprite player->setPhysicsBody(player1Body); player->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 4)); player->setAnchorPoint(Point(0.5f, 0.5f)); player->setScale(.8); //player->pMovement(player); //this->addChild(sprite); return player; } CC_SAFE_DELETE(player); return NULL; }
void BallSprite::InitSprite() { auto body = PhysicsBody::createCircle(this->getContentSize().width / 2, PhysicsMaterial(1, 1, 0)); if (body != nullptr) { body->setDynamic(true); body->setRotationEnable(true); if (getTeam() == 1) { body->setCollisionBitmask(HERO_BALL_COLLISION_BITMASK); } else { body->setCollisionBitmask(MONSTER_BALL_COLLISION_BITMASK); } body->setContactTestBitmask(true); this->setPhysicsBody(body); } this->setScale(_ballSize); }