void WorldLayerSence::createBodyJointTo(
b2World* world,const char* name)
{
CCSize size =
CCDirector::sharedDirector()->getVisibleSize();
float x = rand()*100%(int)size.width/2;
float y = rand()*100%(int)size.height/2;
//1、创建精灵
CCSprite* spritTrang =
CCSprite::create(name);
spritTrang->setPosition(ccp(size.width/1.2
-spritTrang->getContentSize().width/2/PTM_RATIO,
160/PTM_RATIO));
spritTrang->setTag(2);
addChild(spritTrang);
//1创建刚体定义
b2BodyDef trangBodyDef;
trangBodyDef.userData =
spritTrang;//刚体关联精灵
//刚体定义位置
trangBodyDef.position.Set((size.width/1.2
-spritTrang->getContentSize().width/2)/PTM_RATIO,
160/PTM_RATIO);
trangBodyDef.type=
b2_dynamicBody;//刚体种类,动态刚体
//2根据刚体定义产生刚体
b2Body* trangBody = world->CreateBody(
&trangBodyDef);
//3、赋予形状
b2PolygonShape shape;
shape.SetAsBox(
spritTrang->getContentSize().width/2/PTM_RATIO,
spritTrang->getContentSize().height/2/PTM_RATIO);
//4、定义关联
b2FixtureDef def;
def.shape = &shape; //关联形状
def.density = 3.5; //关联密度
def.restitution = 1; //关联反弹系数
def.friction = 1; //关联摩擦力
//设置关联
trangBody->CreateFixture(&def);
//连接一个静态刚体
CCSprite* upSpr = CCSprite::create("up_bar.png");
upSpr->setPosition(ccp(size.width/2,0));
addChild(upSpr);
trangBodyDef.type = b2_staticBody;
trangBodyDef.position.Set(
size.width/2/PTM_RATIO,
0);//关联位置
//定义形状
b2PolygonShape upShape;
upShape.SetAsBox(
upSpr->getContentSize().width/2/PTM_RATIO,
upSpr->getContentSize().height/2/PTM_RATIO);
//定义关联
b2FixtureDef upFixDef;
upFixDef.shape=&upShape;
upFixDef.restitution = 1;//反弹系数
b2Body* upBody = m_world->CreateBody(
&trangBodyDef);
upBody->CreateFixture(&upFixDef);
//1、生成一个连接定义
//定义一个连接刚体
b2RevoluteJointDef jointDef;
//jointDef.Initialize(upBody, trangBody,
// b2Vec2(size.width/1.2/PTM_RATIO,
// 160/PTM_RATIO));
jointDef.bodyA = upBody; //静态刚体
//关联的刚体A
jointDef.bodyB = trangBody; //非静态刚体
//关联的刚体B
jointDef.collideConnected = false;
jointDef.localAnchorB.Set(0,0);
jointDef.localAnchorA.Set(0,4);
//不会发生碰撞
jointDef.enableLimit = false;
//关闭角度限制
jointDef.lowerAngle = CC_DEGREES_TO_RADIANS(
-360);
//角度最低限制(由于关闭了角度限制,所以不起作用)
jointDef.upperAngle = CC_DEGREES_TO_RADIANS(
360);
//角度最高限制(由于关闭了角度限制,所以不起作用)
jointDef.enableMotor = true;
//开启连接器马达
jointDef.motorSpeed = 100;
//连接器马达的目标速度
jointDef.maxMotorTorque = 100;
//马达的最大扭矩
//旋转速度
jointDef.maxMotorTorque = 10.0f;
//灵敏度
jointDef.motorSpeed = 40.0f;
//创建连接(关节)
b2Joint* joint =
trangBody->GetWorld()->CreateJoint(&jointDef);
//使用旋转连接器创建链条(下一条实现)
}
b2World* world,const char* name)
{
CCSize size =
CCDirector::sharedDirector()->getVisibleSize();
float x = rand()*100%(int)size.width/2;
float y = rand()*100%(int)size.height/2;
//1、创建精灵
CCSprite* spritTrang =
CCSprite::create(name);
spritTrang->setPosition(ccp(size.width/1.2
-spritTrang->getContentSize().width/2/PTM_RATIO,
160/PTM_RATIO));
spritTrang->setTag(2);
addChild(spritTrang);
//1创建刚体定义
b2BodyDef trangBodyDef;
trangBodyDef.userData =
spritTrang;//刚体关联精灵
//刚体定义位置
trangBodyDef.position.Set((size.width/1.2
-spritTrang->getContentSize().width/2)/PTM_RATIO,
160/PTM_RATIO);
trangBodyDef.type=
b2_dynamicBody;//刚体种类,动态刚体
//2根据刚体定义产生刚体
b2Body* trangBody = world->CreateBody(
&trangBodyDef);
//3、赋予形状
b2PolygonShape shape;
shape.SetAsBox(
spritTrang->getContentSize().width/2/PTM_RATIO,
spritTrang->getContentSize().height/2/PTM_RATIO);
//4、定义关联
b2FixtureDef def;
def.shape = &shape; //关联形状
def.density = 3.5; //关联密度
def.restitution = 1; //关联反弹系数
def.friction = 1; //关联摩擦力
//设置关联
trangBody->CreateFixture(&def);
//连接一个静态刚体
CCSprite* upSpr = CCSprite::create("up_bar.png");
upSpr->setPosition(ccp(size.width/2,0));
addChild(upSpr);
trangBodyDef.type = b2_staticBody;
trangBodyDef.position.Set(
size.width/2/PTM_RATIO,
0);//关联位置
//定义形状
b2PolygonShape upShape;
upShape.SetAsBox(
upSpr->getContentSize().width/2/PTM_RATIO,
upSpr->getContentSize().height/2/PTM_RATIO);
//定义关联
b2FixtureDef upFixDef;
upFixDef.shape=&upShape;
upFixDef.restitution = 1;//反弹系数
b2Body* upBody = m_world->CreateBody(
&trangBodyDef);
upBody->CreateFixture(&upFixDef);
//1、生成一个连接定义
//定义一个连接刚体
b2RevoluteJointDef jointDef;
//jointDef.Initialize(upBody, trangBody,
// b2Vec2(size.width/1.2/PTM_RATIO,
// 160/PTM_RATIO));
jointDef.bodyA = upBody; //静态刚体
//关联的刚体A
jointDef.bodyB = trangBody; //非静态刚体
//关联的刚体B
jointDef.collideConnected = false;
jointDef.localAnchorB.Set(0,0);
jointDef.localAnchorA.Set(0,4);
//不会发生碰撞
jointDef.enableLimit = false;
//关闭角度限制
jointDef.lowerAngle = CC_DEGREES_TO_RADIANS(
-360);
//角度最低限制(由于关闭了角度限制,所以不起作用)
jointDef.upperAngle = CC_DEGREES_TO_RADIANS(
360);
//角度最高限制(由于关闭了角度限制,所以不起作用)
jointDef.enableMotor = true;
//开启连接器马达
jointDef.motorSpeed = 100;
//连接器马达的目标速度
jointDef.maxMotorTorque = 100;
//马达的最大扭矩
//旋转速度
jointDef.maxMotorTorque = 10.0f;
//灵敏度
jointDef.motorSpeed = 40.0f;
//创建连接(关节)
b2Joint* joint =
trangBody->GetWorld()->CreateJoint(&jointDef);
//使用旋转连接器创建链条(下一条实现)
}