void Player::buildPlayerBox2DBody(b2World* world) { CCPoint position = CCPointZero; //body definition b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.fixedRotation = true; b2Vec2 worldPosition = ViewPort::getInstance()->screenToWorldCoordinate(position); bodyDef.position.Set(worldPosition.x, worldPosition.y); //shape definition for main fixture b2PolygonShape dynamicBox = _profile->spriteBox2DShape(); //fixture definition b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 1; fixtureDef.friction = 0.0f; // So the player will be able to slide next to obstacles fixtureDef.restitution = 0.0f; // So the player will not bounce again from the ground fixtureDef.filter.groupIndex = -1; //negative so the different players will not collide //create dynamic body b2Body *body = world->CreateBody(&bodyDef); //add main fixture body->CreateFixture(&fixtureDef); setB2Body(body); setPTMRatio(ViewPort::getInstance()->getPTMRatio()); setPosition(position); }
void Box2DTestLayer::addNewSpriteAtPosition(Point p) { CCLOG("Add sprite %0.2f x %02.f",p.x,p.y); // Define the dynamic body. //Set up a 1m squared box in the physics world b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); b2Body *body = world->CreateBody(&bodyDef); // Define another box shape for our dynamic body. b2PolygonShape dynamicBox; dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef); auto parent = this->getChildByTag(kTagParentNode); //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is //just kdRandomly picking one of the images int idx = (CCRANDOM_0_1() > .5 ? 0:1); int idy = (CCRANDOM_0_1() > .5 ? 0:1); auto sprite = PhysicsSprite::createWithTexture(_spriteTexture,Rect(32 * idx,32 * idy,32,32)); parent->addChild(sprite); sprite->setB2Body(body); sprite->setPTMRatio(PTM_RATIO); sprite->setPosition( Point( p.x, p.y) ); }