Exemple #1
0
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 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) );
}
Exemple #3
0
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);
}
Exemple #4
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;
            }
        }
    }
PhysicsSprite* PhysicsSprite::createWithTexture(Texture2D *pTexture)
{
    PhysicsSprite* pRet = new PhysicsSprite();
    if (pRet && pRet->initWithTexture(pTexture))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }

    return pRet;
}
PhysicsSprite* PhysicsSprite::create()
{
    PhysicsSprite* pRet = new PhysicsSprite();
    if (pRet && pRet->init())
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    return pRet;
}
PhysicsSprite* PhysicsSprite::create(const char *pszFileName)
{
    PhysicsSprite* pRet = new PhysicsSprite();
    if (pRet && pRet->initWithFile(pszFileName))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }

    return pRet;
}
Exemple #8
0
void Prototype::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {
	PhysicsSprite* sprite;

    for (CCSetIterator it = pTouches->begin(); it != pTouches->end(); it++) {
        CCTouch* touch = dynamic_cast<CCTouch*>(*it);
		CCPoint touchPoint = touchToPoint(touch);
		sprite = getPhysicsSpriteAtXY(touchPoint);

		if(sprite != NULL) {
			testPlaceBomb(sprite->getPhysicsBody(), touchPoint, 1.75f );
		}

    }
}
Exemple #9
0
PhysicsSprite* PhysicsSprite::create(const char *pszFileName, const Rect& rect)
{
    PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
    if (pRet && pRet->initWithFile(pszFileName, rect))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }

    return pRet;
}
Exemple #10
0
PhysicsSprite* PhysicsSprite::createWithSpriteFrame(SpriteFrame *pSpriteFrame)
{
    PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
    if (pRet && pRet->initWithSpriteFrame(pSpriteFrame))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }

    return pRet;
}
Exemple #11
0
PhysicsSprite* PhysicsSprite::createWithTexture(Texture2D *pTexture, const Rect& rect)
{
    PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
    if (pRet && pRet->initWithTexture(pTexture, rect))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }

    return pRet;
}
Exemple #12
0
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;

}
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);
}
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
}
void GamePhysicsContactListener::BeginContact(b2Contact* contact)
{
    // 衝突した双方の物体を取得
    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();

    CCNode* nodeA = (CCNode*)bodyA->GetUserData();
    CCNode* nodeB = (CCNode*)bodyB->GetUserData();

    if( nodeA != NULL && nodeB != NULL ){
        //
        if( nodeA->getTag() == NODE_TAG_BALL ){
            Ball* pBall = (Ball*)nodeA;
            pBall->contactWith(nodeB);
        }
        else if( nodeB->getTag() == NODE_TAG_BALL ){
            Ball* pBall = (Ball*)nodeB;
            pBall->contactWith(nodeA);
        }
    }

#if 0
    // 物体にひもづくSpriteを取得
    PhysicsSprite* spriteA = (PhysicsSprite*)bodyA->GetUserData();
    PhysicsSprite* spriteB = (PhysicsSprite*)bodyB->GetUserData();

    // 地面との衝突は無視する
    if (spriteA->getTag() == Config::kTag_Ground ||
        spriteB->getTag() == Config::kTag_Ground)
    {
        return;
    }

    // 衝突時の加速度を取得
    b2Vec2 velocityA = bodyA->GetLinearVelocity();
    b2Vec2 velocityB = bodyB->GetLinearVelocity();
    CCLOG("[BeginContact] A(%f, %f) B(%f, %f)", velocityA.x, velocityA.y, velocityB.x, velocityB.y);

    // 加速度が一定上の大きさだったら、ぶつかられた方を削除する
    float threshold = 3;
    if (pow(velocityA.x, 2) + pow(velocityA.y, 2) > pow(threshold, 2)) {
        spriteB->setDeleteFlag(true);
    }
    if (pow(velocityB.x, 2) + pow(velocityB.y, 2) > pow(threshold, 2)) {
        spriteA->setDeleteFlag(true);
    }
#endif
}
Exemple #16
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);
}
Exemple #17
0
void HelloWorld::update(float dt)
{
    
    

    
    
    CCPoint velocity = joyStick->getVelocity(); // range  0-1
    if (velocity.x != 0 || velocity.y != 0) {
        
        if (speed < MAX_SPEED)
            speed += 50;
        
        
        // +y轴为起始轴 顺时针转  atan2范围是PI - 负PI  atan是 PI/2 - 负PI/2
        float c = atan2(velocity.x, velocity.y);
        
        c = c * 180/M_PI;
        
        // 度数为0 - 360.忘记了可以nslog下
        if (c <= 0)
            c += 360;
    
   
        float d = CC_RADIANS_TO_DEGREES(playerBody->GetAngle()),b;
        
        if (d <= 0) {
            d = -d;
            b =(int)d % 360;
        }else
            b =360 - abs((int)d) % 360;
        
        
        //CCLog("stickangle:%f,playerangle:%f   %f",c,b,CC_RADIANS_TO_DEGREES(playerBody->GetAngle()));
        float omega = 100.0f;
        if (abs(b - c) > 1.0f)
        {

            if (b > c) {
                if (b - c < 180)
                    omega = omega;
                else
                    omega = -omega;
            }else
            {
                if (c - b < 180)
                    omega = -omega;
                else
                    omega = omega;
            }
        
  
            
            playerBody->SetAngularVelocity(CC_DEGREES_TO_RADIANS(omega));
        }else
            playerBody->SetAngularVelocity(CC_DEGREES_TO_RADIANS(0.0f));
    }else
    {
        playerBody->SetAngularVelocity(CC_DEGREES_TO_RADIANS(0.0f));
    }
    
    if (speed > 0)
        speed -= 10;
    if (speed < 0) {
        speed = 0;
    }
    
    
    PhysicsSprite *player = (PhysicsSprite *)playerBody->GetUserData();
    float playerAngle = player->getRotation();
    
    b2Vec2 v2Linear;
    v2Linear.Set(sin(CC_DEGREES_TO_RADIANS(playerAngle)) * speed / PTM_RATIO,cos(CC_DEGREES_TO_RADIANS(playerAngle) )* speed / PTM_RATIO);
    playerBody->SetLinearVelocity(v2Linear);
}
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);

}
Exemple #19
0
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());*/
    }
    
}