cocos2d-x 物理引擎Box2D 连接器之旋转连接器

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);
    //使用旋转连接器创建链条(下一条实现)
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值