コード例 #1
0
ファイル: Prototype.cpp プロジェクト: delorenj/Thermite
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;
}
コード例 #2
0
void MainGameLayer::addBox(Point p)
{
	// 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 = (Box2dDirectorLayer::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
	dynamicBox.SetAsBox(.5f, .8f);//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);    

	// body to sprite
	Texture2D *texture = TextureCache::getInstance()->addImage("gabriel.png");
	PhysicsSprite *sprite = PhysicsSprite::createWithTexture(texture);
    addChild(sprite);
    sprite->setB2Body(body);
    sprite->setPTMRatio(PTM_RATIO);
    sprite->setPosition( Point( p.x, p.y) );
}
コード例 #3
0
ファイル: LiquidScene.cpp プロジェクト: kosakasakas/2DPhys
void LiquidScene::drawBox2dSpriteAt(LiquidScene::Box2dSpriteData data, Point pos) {
    if (data.file == NULL) {
        return;
    }
    
    b2Body* body;
    b2BodyDef bodyDef;
    b2CircleShape shape;
    b2FixtureDef fixtureDef;
    
    PhysicsSprite* physicsSprite = PhysicsSprite::create(data.file);
    float scale = Director::getInstance()->getContentScaleFactor();
    physicsSprite->cocos2d::Node::setScale(scale);
    
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set((pos.x) / PT_RATIO, (pos.y) / PT_RATIO);
    bodyDef.userData = physicsSprite;
    
    body = world->CreateBody(&bodyDef);
    
    shape.m_radius = physicsSprite->getContentSize().width / PT_RATIO;
    fixtureDef.shape = &shape;
    fixtureDef.density = data.density;
    fixtureDef.friction = data.friction;
    
    body->CreateFixture(&fixtureDef);
    
    this->addChild(physicsSprite);
    physicsSprite->setB2Body(body);
    physicsSprite->setPTMRatio(PT_RATIO);
    physicsSprite->setPosition(pos);
}
コード例 #4
0
ファイル: Prototype.cpp プロジェクト: delorenj/Thermite
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;

}
コード例 #5
0
void HelloWorld::addNewSpriteAtPosition(CCPoint p)
{
    CCLOG("Add sprite %0.2f x %02.f",p.x,p.y);
    CCNode* parent = getChildByTag(kTagParentNode);
    
    //We have a 64x64 sprite sheet with 4 different 32x32 images.  The following code is
    //just randomly picking one of the images
    int idx = (CCRANDOM_0_1() > .5 ? 0:1);
    int idy = (CCRANDOM_0_1() > .5 ? 0:1);
    PhysicsSprite *sprite = new PhysicsSprite();
    sprite->initWithTexture(m_pSpriteTexture, CCRectMake(32 * idx,32 * idy,32,32));
    sprite->autorelease();
    
    parent->addChild(sprite);
    
    sprite->setPosition( CCPointMake( 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);
    
    sprite->setPhysicsBody(body);
}
コード例 #6
0
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);    
    
#if CC_ENABLE_BOX2D_INTEGRATION
    Node *parent = this->getChildByTag(kTagParentNode);
    
    //We have a 64x64 sprite sheet with 4 different 32x32 images.  The following code is
    //just randomly picking one of the images
    int idx = (CCRANDOM_0_1() > .5 ? 0:1);
    int idy = (CCRANDOM_0_1() > .5 ? 0:1);
    PhysicsSprite *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) );
#endif
}
コード例 #7
0
void HelloWorld::addNewSpriteAtPosition(CCPoint p)
{
   CCLOG("Add sprite %0.2f x %02.f",p.x,p.y);
   CCNode* parent = getChildByTag(kTagParentNode);
   
   //We have a 64x64 sprite sheet with 4 different 32x32 images.  The following code is
   //just randomly picking one of the images
   int idx = (CCRANDOM_0_1() > .5 ? 0:1);
   int idy = (CCRANDOM_0_1() > .5 ? 0:1);
   PhysicsSprite *sprite = new PhysicsSprite();
   sprite->initWithTexture(m_pSpriteTexture, CCRectMake(32 * idx,32 * idy,32,32));
   sprite->autorelease();
   
   parent->addChild(sprite);
   
   sprite->setPosition( CCPointMake( p.x, p.y) );
   sprite->setOpacity(32);
   
   // 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);
   
   sprite->setPhysicsBody(body);
   
   // Create a rope joint so that the
   // block will "swing" from the top of the
   // scene.
   
   // Calculate the local position of the
   // top of screen in the local space
   // of the ground box.
   CCSize scrSize = CCDirector::sharedDirector()->getWinSize();
   b2Vec2 groundWorldPos = b2Vec2((scrSize.width/2)/PTM_RATIO,(scrSize.height)/PTM_RATIO);
   b2Vec2 groundLocalPos = m_pGround->GetLocalPoint(groundWorldPos);
   
   // Now create the main swinging joint.
   b2RopeJointDef jointDef;
   jointDef.bodyA = m_pGround;
   jointDef.bodyB = body;
   jointDef.localAnchorA = groundLocalPos;
   jointDef.localAnchorB = b2Vec2(0.0f,0.0f);
   jointDef.maxLength = (groundWorldPos-body->GetWorldCenter()).Length();
   jointDef.collideConnected = true;
   world->CreateJoint(&jointDef);
   
   // Now create a second/third rope to "constrain" the swing.
   // These one will be attached to the side of the screen.
   b2Vec2 groundWorldPos2 = b2Vec2(0,body->GetWorldCenter().y);
   b2Vec2 groundLocalPos2 = m_pGround->GetLocalPoint(groundWorldPos2);

   jointDef.localAnchorA = groundLocalPos2;
   // Setting the length of the side rope...
   // What we want to do here is use the distance from the center as
   // the length of the rope.  Then add length to it so that the box
   // can have at least one full swing which ever side it is on (left or
   // right half of the scene).
   float32 distToCenter = (fabs(scrSize.width/2 - (body->GetWorldCenter().x)*PTM_RATIO))/PTM_RATIO;
   jointDef.maxLength = distToCenter + (scrSize.width/2)/PTM_RATIO;
   world->CreateJoint(&jointDef);
   
   // Now for the last rope, other side of the scene.
   // Now create a second/third rope to "constrain" the swing.
   // These one will be attached to the side of the screen.
   b2Vec2 groundWorldPos3 = b2Vec2((scrSize.width)/PTM_RATIO,body->GetWorldCenter().y);
   b2Vec2 groundLocalPos3 = m_pGround->GetLocalPoint(groundWorldPos3);
   jointDef.localAnchorA = groundLocalPos3;
   world->CreateJoint(&jointDef);

}
コード例 #8
0
ファイル: NormalEnemy.cpp プロジェクト: gouri34/heist
void NormalEnemy::setArmatureBody()
{
    //armature->getAnimation()->setMovementEventCallFunc(this, movementEvent_selector(NormalEnemy::animationEvent));
    
    armature->getAnimation()->playWithIndex(0);
    
    Vector<Node*> bonearr = armature->getChildren();
    //printf("bonearr size = %zd\n",bonearr.size());
    //Skin *dump = (Skin*)((Bone*)(bonearr.at(2)))->getDisplayRenderNode();
    //batch = SpriteBatchNode::createWithTexture(dump->getTexture());
    //gameScene->addChild(batch, armature->getZOrder());
    

    
    // int z = bonearr->count();
    for(int i = 0; i< bonearr.size();i++)
    {
        Bone *bone = (Bone*)bonearr.at(i);
        string boneName = bone->getName();
        Skin *skin = (Skin*)bone->getDisplayRenderNode();
        
        
        if (skin !=NULL) {
            skin->isphysicsObject = true;
            skin->parentScale = armature->getScale();
            Rect a = skin->getTextureRect();
            
            Point partpos = skin->getWorldPosition();
            float partrotation = skin->getWorldRotation();
            float bodyrotation = partrotation*M_PI/180.0;
            Size partSize = Size((a.getMaxX()-a.getMinX())/PTM_RATIO*armature->getScale(), (a.getMaxY()-a.getMinY())/PTM_RATIO*armature->getScale());
            
            b2BodyDef bodyDef;
            bodyDef.type = b2_staticBody;
            
            bodyDef.position.Set(partpos.x/PTM_RATIO, partpos.y/PTM_RATIO);
            b2Body *body_ = gameWorld->CreateBody(&bodyDef);
            
            b2PolygonShape dynamicBox;
            dynamicBox.SetAsBox(partSize.width/2.0, partSize.height/2.0);//These are mid points for our 1m box
            b2CircleShape circleShape;
            circleShape.m_radius = 0.45;
            
            b2FixtureDef fixtureDef;
            if (boneName.compare("headbone") == 0||boneName.compare("head")==0) {
                fixtureDef.shape = &circleShape;

            }
            else if (boneName.compare("left_feet") == 0||boneName.compare("foot_right")==0)
            {
                circleShape.m_radius = 0.2;
                fixtureDef.shape = &circleShape;

            }
            else {
                fixtureDef.shape = &dynamicBox;
            }
            fixtureDef.density = 0.2f;
            fixtureDef.restitution = 0.8;
            fixtureDef.friction = 0.2f;
            fixtureDef.filter.categoryBits = ZOMBIE;
            
           // printf("bonename = %s\n", boneName.c_str());
            
            if (boneName.compare("headbone") == 0||boneName.compare("head")==0) {
                fixtureDef.fixturetype = f_zbody_body;
                fixtureDef.filter.maskBits = BASE_GROUND | UPPER_GROUND | ARROW | BULLET;
            }
            else if (boneName.compare("bodybone") == 0||boneName.compare("body")==0) {
                fixtureDef.fixturetype = f_zbody_body;
                fixtureDef.filter.maskBits = BASE_GROUND | UPPER_GROUND | ARROW | BULLET;
            }
            else if (boneName.compare("leglbone") == 0 || boneName.compare("right_leg") == 0|| boneName.compare("right_foreleg")|| boneName.compare("foreleg_rbone") ||boneName.compare("left_leg")==0||boneName.compare("left_foreleg")==0) {
                fixtureDef.fixturetype = f_zbody_body;
                fixtureDef.filter.maskBits = BASE_GROUND | UPPER_GROUND | ARROW | BULLET;
            }
            else {
                fixtureDef.fixturetype = f_zbody_body;
                fixtureDef.filter.maskBits = BASE_GROUND | UPPER_GROUND;
            }
            
            
            
            body_->CreateFixture(&fixtureDef);
            body_->SetUserData(this);
            body_->SetTransform(b2Vec2(partpos.x/PTM_RATIO, partpos.y/PTM_RATIO), bodyrotation);
            body_->SetAngularDamping(1.2);
            
            skin->body = body_;
            
            
            PhysicsSprite *dumpSprite = (PhysicsSprite*)PhysicsSprite::createWithTexture(skin->getTexture(), skin->getTextureRect());
            dumpSprite->setPosition(skin->getWorldPosition());
            dumpSprite->setScale(armature->getScale());
            dumpSprite->setVisible(false);
            dumpSprite->body = body_;
            // printf("zorder = %i\n", bone->getZOrder());
           // dumpSprite->setZOrder(bone->getZOrder()+3);
            gameScene->addChild(dumpSprite,bone->getZOrder()+20);
            //deadSpriteArray->addObject((Ref*)dumpSprite);
            deadSpriteArray.pushBack(dumpSprite);
            
            
            bodies.push_back(body_);
        }
        /*Bone *bone = armature->getBone(key->getCString());
         printf("boneX = %f\n", bone->getPositionX());
         printf("boneY = %f\n", bone->getPositionY());*/
    }
    
}