void WorldLayerSence::createLineRouteJoint(
b2World* world,b2Body* body )
{
CCSprite* sprit = CCSprite::create("link.png");
sprit->setPosition(ccp(160,230));
addChild(sprit);
//链条节点
b2BodyDef def;
def.userData = sprit;
def.type = b2_staticBody;
//第一个节点做成静态刚体用以固定他的位置
def.position.Set(160/PTM_RATIO,230/PTM_RATIO);
b2FixtureDef fixdef;
fixdef.density = 1;//密度
fixdef.restitution = 0.9;//反弹系数
fixdef.friction = 1;//摩擦力
b2PolygonShape shap;
shap.SetAsBox(
sprit->getContentSize().width/2/PTM_RATIO,
sprit->getContentSize().height/2/PTM_RATIO);
fixdef.shape= &shap;
b2Body* linkNodeBody = world->CreateBody(&def);
linkNodeBody->CreateFixture(&fixdef);
//使用循环创建15个链条节点
for(int i = 0; i<15; i++)
{
def.type = b2_dynamicBody;
CCSprite* sp = CCSprite::create("link.png");
sprit->setPosition(ccp(160,230));
addChild(sp);
def.userData = sp;
b2Body* newLinkNodeBody = world->CreateBody(
&def);
newLinkNodeBody->CreateFixture(&fixdef);
//针对两节节点创建关节
createNodeRouteJoint(world,linkNodeBody,
newLinkNodeBody);
linkNodeBody = newLinkNodeBody;
}
};
void WorldLayerSence::createNodeRouteJoint(
b2World* world,b2Body* node,b2Body* newNode)
{
//创建旋转关节
b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.localAnchorA.Set( 0,-0.5);
revoluteJointDef.localAnchorB.Set(0,0.5);
//inside the loop, only need to change the bodies to be joined
revoluteJointDef.bodyA = node;
revoluteJointDef.bodyB = newNode;
revoluteJointDef.collideConnected = false;
revoluteJointDef.enableLimit = false;
m_world->CreateJoint( &revoluteJointDef );
}
b2World* world,b2Body* body )
{
CCSprite* sprit = CCSprite::create("link.png");
sprit->setPosition(ccp(160,230));
addChild(sprit);
//链条节点
b2BodyDef def;
def.userData = sprit;
def.type = b2_staticBody;
//第一个节点做成静态刚体用以固定他的位置
def.position.Set(160/PTM_RATIO,230/PTM_RATIO);
b2FixtureDef fixdef;
fixdef.density = 1;//密度
fixdef.restitution = 0.9;//反弹系数
fixdef.friction = 1;//摩擦力
b2PolygonShape shap;
shap.SetAsBox(
sprit->getContentSize().width/2/PTM_RATIO,
sprit->getContentSize().height/2/PTM_RATIO);
fixdef.shape= &shap;
b2Body* linkNodeBody = world->CreateBody(&def);
linkNodeBody->CreateFixture(&fixdef);
//使用循环创建15个链条节点
for(int i = 0; i<15; i++)
{
def.type = b2_dynamicBody;
CCSprite* sp = CCSprite::create("link.png");
sprit->setPosition(ccp(160,230));
addChild(sp);
def.userData = sp;
b2Body* newLinkNodeBody = world->CreateBody(
&def);
newLinkNodeBody->CreateFixture(&fixdef);
//针对两节节点创建关节
createNodeRouteJoint(world,linkNodeBody,
newLinkNodeBody);
linkNodeBody = newLinkNodeBody;
}
};
void WorldLayerSence::createNodeRouteJoint(
b2World* world,b2Body* node,b2Body* newNode)
{
//创建旋转关节
b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.localAnchorA.Set( 0,-0.5);
revoluteJointDef.localAnchorB.Set(0,0.5);
//inside the loop, only need to change the bodies to be joined
revoluteJointDef.bodyA = node;
revoluteJointDef.bodyB = newNode;
revoluteJointDef.collideConnected = false;
revoluteJointDef.enableLimit = false;
m_world->CreateJoint( &revoluteJointDef );
}