void Prototype::testSeparator() { b2Separator sep; vector<b2Vec2>* vec = new vector<b2Vec2>(); vec->push_back(b2Vec2(-4, -4)); vec->push_back(b2Vec2(4, -4)); vec->push_back(b2Vec2(4, 0)); vec->push_back(b2Vec2(0, 0)); vec->push_back(b2Vec2(0, 4)); vec->push_back(b2Vec2(-4, 4)); m_bodyDef.position.Set((m_centerPoint.x-300)/PTM_RATIO, m_centerPoint.y/PTM_RATIO); b2Body* body = getWorld()->CreateBody(&m_bodyDef); try { sep.Separate(body, &m_fixtureDef, vec, PTM_RATIO); } catch(b2SeparatorException& e) { CCLog("b2Separator Exception: %s", e.what()); } // Only need below to attach box2d body to a cocos2d sprite... PhysicsSprite* ps = new PhysicsSprite(); ps->setTag(2); ps->setPosition( CCPointMake( m_centerPoint.x+300, m_centerPoint.y ) ); ps->setPhysicsBody(body); body->SetUserData(ps); m_sprites[ps->getTag()] = ps; }
void Prototype::testSimple() { b2PolygonShape shape; m_bodyDef.position.Set(m_centerPoint.x/PTM_RATIO, m_centerPoint.y/PTM_RATIO); b2Body* body = getWorld()->CreateBody(&m_bodyDef); shape.SetAsBox(4, 4); m_fixtureDef.shape = &shape; body->CreateFixture(&m_fixtureDef); // Only need below to attach box2d body to a cocos2d sprite... PhysicsSprite* ps = new PhysicsSprite(); ps->setTag(1); ps->setPosition( CCPointMake( m_centerPoint.x+300, m_centerPoint.y ) ); ps->setPhysicsBody(body); body->SetUserData(ps); m_sprites[ps->getTag()] = ps; }
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); }