Exemplo n.º 1
0
void LiquidScene::createParticle() {
    if (world != NULL) {
        particleColor = b2ParticleColor(arc4random()%255, arc4random()%255, arc4random()%255, 255);
        b2ParticleSystemDef psd;
        psd.radius = 4.0/PT_RATIO;
        particleSystem = world->CreateParticleSystem(&psd);
    }
}
void LiquidFunScene::CreateBlockOfParticles() {
  b2PolygonShape shape;
  const b2Vec2 center = ConvertRelativePositionToWorld(b2Vec2(0.5f, 0.5f));
  const b2Vec2 halfSize = ConvertRelativeSizeToWorld(
      b2Vec2(0.4f, 0.02f));
  shape.SetAsBox(halfSize.x, halfSize.y, b2Vec2(0.0f, 0.0f), 0.0f);

  b2ParticleGroupDef pd;
  pd.flags = b2_viscousParticle;
  pd.position = center;
  pd.shape = &shape;
  pd.color = b2ParticleColor(255, 128, 128, 255);
  m_world->CreateParticleGroup(pd);
}
BallSprite *BallSprite::createParticleSprite(Node *parent ,b2World *world, b2ParticleSystem *particleSystem, b2ParticleGroup *particleGroup,const std::string& filename) {
    
    BallSprite* pInstance = (BallSprite *)Sprite::create();
    //BallSprite* pInstance = BallSprite::createBallSprite(parent,world,filename);
    pInstance->setTexture(filename);
    parent->addChild(pInstance);
    
    // 描画用ノードの作成
    DrawNode* draw = DrawNode::create();
    draw->setPosition(Vec2(0, 0));
    pInstance->addChild(draw);
    
    /* 円の描画 */
    draw->drawDot(Vec2(pInstance->getBallRadius()/2, pInstance->getBallRadius()/2), // 中心
                  pInstance->getBallRadius(),                        // 半径
                  Color4F(1, 0.5f, 0, 200)                  // 色
                  );
    
    
    particleSystem->SetRadius(getBallRadius()*1.2/PTM_RATIO);
    
    b2ParticleDef particleDef;
    
    //パーティクルの寿命
    //particleDef.lifetime = 2.0f;
    
    //particleDef.flags = b2_dynamicBody;
    particleDef.flags = b2_waterParticle;
    particleDef.flags |= b2_destructionListenerParticle;
    particleDef.color = b2ParticleColor(100,150,255,255);
    //particleDef.velocity.y = -100;
    
    //グループへのポインタを渡しておく事でそのグループ内で管理する事ができる。
    particleDef.group = particleGroup;
    particleDef.flags = b2_waterParticle;
    
   
    //void** userdata = particleSystem->GetUserDataBuffer();
    particleDef.userData = pInstance;
    particleSystem->CreateParticle(particleDef);
    
    return pInstance;

}
void GameScene_Box2D::addParticle(){
    
    //発生起点
    b2Vec2 origin(300.0f/PTM_RATIO,300.0f/PTM_RATIO);
    
    //パーティクル加速度
    b2Vec2 velocity(0,100.0f);
    
    b2ParticleDef particleDef;
    particleDef.flags = b2_waterParticle;
    particleDef.flags |= b2_destructionListenerParticle;
    particleDef.color = b2ParticleColor(100,150,255,200);
    
    //パーティクルの寿命
   // particleDef.lifetime = 2.0f;
    
    //グループへのポインタを渡しておく事でそのグループ内で管理する事ができる。
    particleDef.group = _particleGroup;
    
    //設定した数値の間で発射方向をランダムに設定
    float ratep = (float)rand() / (float)RAND_MAX;
    particleDef.position.x = origin.x + (100 * ratep)/PTM_RATIO - 100/PTM_RATIO;
    particleDef.position.y = origin.y + (100 * ratep)/PTM_RATIO - 100/PTM_RATIO;
    
    //設定した数値の間でランダムな位置にパーティクルを生成
    float ratev = (float)rand() / (float)RAND_MAX;
    particleDef.velocity.x = velocity.x + (5 * ratev) - (5 / 2);
    
    //画像設定
    Sprite * image = Sprite::create("ball.png");
    
    image->setPositionX(particleDef.position.x * PTM_RATIO);
    image->setPositionY(particleDef.position.y * PTM_RATIO);
    
    //描画担当レイヤに渡す
    addChild(image);
    
    particleDef.userData = image;
    _particleSystem->CreateParticle(particleDef);
    
}
// Creates a physics world and lays outt he
void LiquidFunScene::LayoutLevel(int level) {
  // Scale the dimensions of the world to fit the view.
  const CCSize viewSize = CCDirector::sharedDirector()->getVisibleSize();
  const float aspectRatio = viewSize.width / viewSize.height;
  m_worldExtents.x = k_worldWidth;
  m_worldExtents.y = k_worldWidth / aspectRatio;
  m_worldExtentMin = b2Vec2Min(m_worldExtents);
  m_worldExtentMax = b2Vec2Max(m_worldExtents);
  m_worldExtentsInWalls = m_worldExtents;
  m_worldOffsetInWalls.Set(0.0f, 0.0f);

  // Create a world.
  m_world = new b2World(k_gravity);
  m_world->SetParticleRadius(m_worldExtentMin * k_particleSize);
  m_world->SetParticleDamping(0.2f);

  // Enable the Box2D rendering layer.
  Box2DGLESDebugDrawLayer* box2dLayer = new Box2DGLESDebugDrawLayer(
    m_world, b2Vec2Min(b2Vec2(viewSize.width / m_worldExtents.x,
                              viewSize.height / m_worldExtents.y)));
  box2dLayer->init();
  addChild(box2dLayer);
  GLESDebugDraw* const debugDraw = box2dLayer->GetGLESDebugDraw();
  // Turn off AABB drawing.
  debugDraw->SetFlags(debugDraw->GetFlags() & ~b2Draw::e_aabbBit);
  // Render particles as solid circles.
  debugDraw->SetSolidParticlesEnable(true);

  // Create boundaries
  CreateWalls();
  CreateBalls(5, 0.075f);
  CreateBlockOfParticles();

  // Define map-specific stuff
  switch (level) {
    case 0: {
      // Add an emitter in the middle
      static const float k_colorCycleRate = 5.0f;
      std::vector<b2ParticleColor> colors;
      colors.push_back(b2ParticleColor(0xff, 0x00, 0x00, 0xff));
      colors.push_back(b2ParticleColor(0xff, 0xff, 0xff, 0xff));
      colors.push_back(b2ParticleColor(0x00, 0x00, 0xff, 0xff));
      colors.push_back(b2ParticleColor(0x00, 0xff, 0x00, 0xff));
      colors.push_back(b2ParticleColor(0x22, 0xff, 0x44, 0xff));
      AddEmitter(ConvertRelativePositionToWorld(b2Vec2(0.5f, 0.1f)),
                 colors, k_colorCycleRate);

      // Add a kill field to the bottom right of the screen.
      b2CircleShape circle;
      circle.m_radius = m_worldExtentMax * 0.025f;
      b2Transform circleLocationSize;
      circleLocationSize.SetIdentity();
      circleLocationSize.Set(ConvertRelativePositionToWorld(
          b2Vec2(0.8f, 0.9f)), 0.0f);
      AddParticleKillField(circleLocationSize, circle, sizeof(circle));
      break;
    }
    default:
      CCLOG("ERROR: Invalid level %d defined.", level);
      break;
  }
}
Exemplo n.º 6
0
#include <stdio.h>

void DestructionListener::SayGoodbye(b2Joint* joint){
	if (test->m_mouseJoint == joint){
		test->m_mouseJoint = NULL;
	}else{
		test->JointDestroyed(joint);
	}
}

void DestructionListener::SayGoodbye(b2ParticleGroup* group){
	test->ParticleGroupDestroyed(group);
}

const b2ParticleColor Test::k_ParticleColors[] = {
	b2ParticleColor(0xff, 0x00, 0x00, 0xff), // red
	b2ParticleColor(0x00, 0xff, 0x00, 0xff), // green
	b2ParticleColor(0x00, 0x00, 0xff, 0xff), // blue
	b2ParticleColor(0xff, 0x8c, 0x00, 0xff), // orange
	b2ParticleColor(0x00, 0xce, 0xd1, 0xff), // turquoise
	b2ParticleColor(0xff, 0x00, 0xff, 0xff), // magenta
	b2ParticleColor(0xff, 0xd7, 0x00, 0xff), // gold
	b2ParticleColor(0x00, 0xff, 0xff, 0xff), // cyan
};
const uint32 Test::k_ParticleColorsCount = B2_ARRAY_SIZE(Test::k_ParticleColors);

Test::Test(){
	const b2ParticleSystemDef particleSystemDef;
	b2Vec2 gravity;
	gravity.Set(0.0f, -10.0f);
	m_world = new b2World(gravity);