Example #1
0
////////////////////////////////////////////////////////////////////////////////
// マウスジョイント取得
b2MouseJoint* HelloWorld::getMouseJoint(cocos2d::CCPoint p) {
    b2Vec2 locationWorld = b2Vec2(p.x/PTM_RATIO, p.y/PTM_RATIO);
    CCNode* parent = getChildByTag(kTagParentNode);
    CCArray* children  = parent->getChildren();
    CCObject* data = NULL;
    CCARRAY_FOREACH(children, data)
    {
        PhysicsSprite* spr = (PhysicsSprite*)data;
        if (spr != NULL) {
            // スプライトに付与されているBodyを取得
            b2Body* b = spr->getBody();
            b2Fixture *f = b->GetFixtureList();
            if (f->TestPoint(locationWorld)) {
            
                b2MouseJointDef md;
                b2BodyDef groundBodyDef;
                b2MouseJoint* mouseJoint;
                b2Body* groundBody = world->CreateBody(&groundBodyDef);
                md.bodyA = groundBody;//ground Body
                md.bodyB = b;
                md.target = locationWorld;
                md.collideConnected = true;
                md.maxForce = 1000.0f * b->GetMass();
            
                b->SetAwake(true);
            
                // ジョイント取得
                mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);
                return mouseJoint;
            }
        }
    }
Example #2
0
HelloWorld::HelloWorld()
{
    hello = this;
    // Define the gravity vector.
    b2Vec2 gravity;
    gravity.Set(0.0f, 0.0f);
    // Do we want to let bodies sleep?
    bool doSleep = true;
    
    // Construct a world object, which will hold and simulate the rigid bodies.
    world = new b2World(gravity);
    world->SetAllowSleeping(doSleep);
    world->SetContinuousPhysics(true);
    
    joyStick = new SneakyJoystick();
    joyStick->autorelease();
    joyStick->initWithRect(CCRectZero);
    joyStick->setAutoCenter(true);
    joyStick->setHasDeadzone(true);
    joyStick->setDeadRadius(10);
    
    SneakyJoystickSkinnedBase *joystickSkin = new SneakyJoystickSkinnedBase();
    joystickSkin->autorelease();
    joystickSkin->init();
    joystickSkin->setBackgroundSprite(CCSprite::create("button-default.png"));
    joystickSkin->setThumbSprite(CCSprite::create("button-disabled.png"));
    joystickSkin->getThumbSprite()->setScale(0.5f);
    joystickSkin->setPosition(ccp(50, 50));
    joystickSkin->setJoystick(joyStick);
    


    // load shapes
    GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithFile("physicalTest.plist");
    
    setTouchEnabled(true);
    setAccelerometerEnabled( true );
    
    CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
    

    
//    m_debugDraw = new GLESDebugDraw( PTM_RATIO );
//    world->SetDebugDraw(m_debugDraw);
//    
//    uint32 flags = 0;
//    flags += b2Draw::e_shapeBit;
//    flags += b2Draw::e_jointBit;
//    //        flags += b2Draw::e_aabbBit;
//    //        flags += b2Draw::e_pairBit;
//    //        flags += b2Draw::e_centerOfMassBit;
//    m_debugDraw->SetFlags(flags);
//    
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(screenSize.width/2/PTM_RATIO, screenSize.height/2/PTM_RATIO);
    
    // Call the body factory which allocates memory for the ground body
	// from a pool and creates the ground box shape (also from a pool).
	// The body is also added to the world.
    b2Body *groundBody = world->CreateBody(&groundBodyDef);
    
    // Define the ground box shape.
    b2PolygonShape groundBox;
    
    /*
    // bottom
    groundBox.SetAsBox(screenSize.width/2/PTM_RATIO, 0, b2Vec2(0, -screenSize.height/2/PTM_RATIO), 0);
 	groundBody->CreateFixture(&groundBox, 0);
	
    // top
    groundBox.SetAsBox(screenSize.width/2/PTM_RATIO, 0, b2Vec2(0, screenSize.height/2/PTM_RATIO), 0);
    groundBody->CreateFixture(&groundBox, 0);
    
    // left
    groundBox.SetAsBox(0, screenSize.height/2/PTM_RATIO, b2Vec2(-screenSize.width/2/PTM_RATIO, 0), 0);
    groundBody->CreateFixture(&groundBox, 0);
    
    // right
    groundBox.SetAsBox(0, screenSize.height/2/PTM_RATIO, b2Vec2(screenSize.width/2/PTM_RATIO, 0), 0);
    groundBody->CreateFixture(&groundBox, 0);
    
*/
    //initBG
    initBG();
    
    PhysicsSprite *sprite = PhysicsSprite::create("missile", CCPointMake(screenSize.width/2/32, screenSize.height/2/32), world);
    sprite->setCollisionGroup(-10);
    
    sprite->setTag(1);
    playerBody = sprite->getBody();
    bgLayer->addChild(sprite);
    
    Weapon *gun = Weapon::create("SlienceBullet.png");
    sprite->addChild(gun);
    

    
    
    
    PhysicsSprite *sprite_2 = PhysicsSprite::create("missile", CCPointMake(screenSize.width/2/32, screenSize.height/2/32), world);
    sprite_2->setTag(2);
    bgLayer->addChild(sprite_2);
    

    //addNewSpriteWithCoords( CCPointMake(screenSize.width/2, screenSize.height/2) );
	//playerBody = addNewSpriteWithCoords( CCPointMake(screenSize.width/2, screenSize.height/2),"missile" ,1);
    //addNewSpriteWithCoords( CCPointMake(screenSize.width/2 + 100, screenSize.height/2),"missile",2 );

    CCFollow *follow = new CCFollow();
    follow->initWithTarget((CCSprite*)playerBody->GetUserData(),CCRectMake(0, 0, screenSize.width * 2, screenSize.height * 2));
    bgLayer->runAction(follow);
    bgLayer->setTag(100);
    follow->release();
   

    schedule(schedule_selector(HelloWorld::tick));
    scheduleUpdate();
    
    CCLayer *UI = CCLayer::create();
    this->addChild(UI);
    
    UI->addChild(joystickSkin);
    joystickSkin->setPosition(ccp(100, 100));
    
    speed = 0;
    
    
    // MyContactListener *_contactListener;
    _contactListener = new MyContactListener();
    world->SetContactListener(_contactListener);
}