用的Box2d,想做一个风车的效果,不吊东西在上面还好,看不出什么。在风车四个杆上分别吊了一个盒子之后,四根杆就像螺丝松了一样会扭动,不是我想要的固定很紧的纹丝不动的效果。
风车是在顺时针转动的。
362
思路如下:
1、创建横竖四根木条
2、创建中间的轮子及固定轮子的静态物体(即中间的小黑圆点),使轮子按一定的速度顺时针转动
pRJ->SetMotorSpeed(-1500.0f);//以一定的速度顺时针转动
pRJ->SetMaxMotorTorque(1000.0f);
3、把四根木条固定在轮子上(pRHV1是个旋转关节,用于固定一根木条)
pRJV1->SetLimits(0.0f, 0.08f);//设置角度限制转动,这里想要完全固定,不让它有一丝的转动,但是设置成两个0的话会有问题,所以后面一个写成了一个很小的数字
pRJV1->EnableLimit(true);//开启限制
4、创建四个盒子(Box)
5、把四个盒子分别固定在四根木条的顶端
6、end
代码有点多,如果有人好心看完的话,希望能指导下,如果没看完代码但是知道原因的,也麻烦指导下,谢谢。
具体代码如下:
这是个init函数
::initWorld(ccTime dt)
{
//创建竖直的两根木条
CCSprite *pWoodV1 = CCSprite::spriteWithFile("Public/woodV.png", CCRectMake(0, 0, 40, 150));
CCSprite *pWoodV2 = CCSprite::spriteWithFile("Public/woodV.png", CCRectMake(0, 0, 40, 150));
b2Body *woodV1Body = sWorld->createRectBody(pWoodV1, ccp(520, 165), b2_dynamicBody);
b2Body *woodV2Body = sWorld->createRectBody(pWoodV2, ccp(520, 355), b2_dynamicBody); //创建水平的两根木条
CCSprite *pWoodH1 = CCSprite::spriteWithFile("Public/woodH.png", CCRectMake(0, 0, 150, 40));
CCSprite *pWoodH2 = CCSprite::spriteWithFile("Public/woodH.png", CCRectMake(0, 0, 150, 40));
b2Body *woodH1Body = sWorld->createRectBody(pWoodH1, ccp(425, 260), b2_dynamicBody);
b2Body *woodH2Body = sWorld->createRectBody(pWoodH2, ccp(615, 260), b2_dynamicBody); //创建轮子及中间的小黑点
CCSprite *pWheel = CCSprite::spriteWithFile("Public/wheel.png");
b2Body *wheelBody = sWorld->createCircleBody(pWheel, ccp(520, 260), b2_dynamicBody, false);
CCSprite *pPoint = CCSprite::spriteWithFile("Public/point.png");
b2Body *pointBody = sWorld->createCircleBody(pPoint, ccp(520, 260), b2_staticBody, false); //创建旋转关节把轮子固定在静态物体小黑点上
b2RevoluteJoint *pRJ = sWorld->createRevoluteJoint(pointBody, wheelBody, pointBody->GetWorldCenter(), true);
pRJ->SetMotorSpeed(-1500.0f);//以一定的速度顺时针转动
pRJ->SetMaxMotorTorque(1000.0f); //把四根木条固定在轮子上,木条随轮子一起转动
b2Vec2 ve = woodV1Body->GetWorldCenter();
ve.y = ve.y + 65 / PTM_RATIO;
b2RevoluteJoint *pRJV1 = sWorld->createRevoluteJoint(wheelBody, woodV1Body, ve, true);
pRJV1->SetLimits(0.0f, 0.08f);
pRJV1->EnableLimit(true);
ve = woodV2Body->GetWorldCenter();
ve.y = ve.y - 65 / PTM_RATIO;
b2RevoluteJoint *pRJV2 = sWorld->createRevoluteJoint(wheelBody, woodV2Body, ve, true);
pRJV2->SetLimits(0.0f, 0.08f);
pRJV2->EnableLimit(true); ve = woodH1Body->GetWorldCenter();
ve.x = ve.x + 65 / PTM_RATIO;
b2RevoluteJoint *pRJH1 = sWorld->createRevoluteJoint(wheelBody, woodH1Body, ve, true);
pRJH1->SetLimits(0.0f, 0.08f);
pRJH1->EnableLimit(true); ve = woodH2Body->GetWorldCenter();
ve.x = ve.x - 65 / PTM_RATIO;
b2RevoluteJoint *pRJH2 = sWorld->createRevoluteJoint(wheelBody, woodH2Body, ve, true);
pRJH2->SetLimits(0.0f, 0.08f);
pRJH2->EnableLimit(true); //创建四个Box,上下两个为红色,左右两个为绿色
Box *boxRed1 = sWorld->createBox(RED, ccp(520, 115));
Box *boxRed2 = sWorld->createBox(RED, ccp(520, 405));
Box *boxGreen1 = sWorld->createBox(GREEN, ccp(375, 260));
Box *boxGreen2 = sWorld->createBox(GREEN, ccp(665, 260));
//把四个Box分别与四根木条固定在一起
sWorld->createRevoluteJoint(woodV1Body, boxRed1->getBoxBody(), boxRed1->getBoxBody()->GetWorldCenter());
sWorld->createRevoluteJoint(woodV2Body, boxRed2->getBoxBody(), boxRed2->getBoxBody()->GetWorldCenter());
sWorld->createRevoluteJoint(woodH1Body, boxGreen1->getBoxBody(), boxGreen1->getBoxBody()->GetWorldCenter());
sWorld->::initWorld(ccTime dt)
{
//创建竖直的两根木条
CCSprite *pWoodV1 = CCSprite::spriteWithFile("Public/woodV.png", CCRectMake(0, 0, 40, 150));
CCSprite *pWoodV2 = CCSprite::spriteWithFile("Public/woodV.png", CCRectMake(0, 0, 40, 150));
b2Body *woodV1Body = sWorld->createRectBody(pWoodV1, ccp(520, 165), b2_dynamicBody);
b2Body *woodV2Body = sWorld->createRectBody(pWoodV2, ccp(520, 355), b2_dynamicBody); //创建水平的两根木条
CCSprite *pWoodH1 = CCSprite::spriteWithFile("Public/woodH.png", CCRectMake(0, 0, 150, 40));
CCSprite *pWoodH2 = CCSprite::spriteWithFile("Public/woodH.png", CCRectMake(0, 0, 150, 40));
b2Body *woodH1Body = sWorld->createRectBody(pWoodH1, ccp(425, 260), b2_dynamicBody);
b2Body *woodH2Body = sWorld->createRectBody(pWoodH2, ccp(615, 260), b2_dynamicBody); //创建轮子及中间的小黑点
CCSprite *pWheel = CCSprite::spriteWithFile("Public/wheel.png");
b2Body *wheelBody = sWorld->createCircleBody(pWheel, ccp(520, 260), b2_dynamicBody, false);
CCSprite *pPoint = CCSprite::spriteWithFile("Public/point.png");
b2Body *pointBody = sWorld->createCircleBody(pPoint, ccp(520, 260), b2_staticBody, false); //创建旋转关节把轮子固定在静态物体小黑点上
b2RevoluteJoint *pRJ = sWorld->createRevoluteJoint(pointBody, wheelBody, pointBody->GetWorldCenter(), true);
pRJ->SetMotorSpeed(-1500.0f);//以一定的速度顺时针转动
pRJ->SetMaxMotorTorque(1000.0f); //把四根木条固定在轮子上,木条随轮子一起转动
b2Vec2 ve = woodV1Body->GetWorldCenter();
ve.y = ve.y + 65 / PTM_RATIO;
b2RevoluteJoint *pRJV1 = sWorld->createRevoluteJoint(wheelBody, woodV1Body, ve, true);
pRJV1->SetLimits(0.0f, 0.08f);
pRJV1->EnableLimit(true);
ve = woodV2Body->GetWorldCenter();
ve.y = ve.y - 65 / PTM_RATIO;
b2RevoluteJoint *pRJV2 = sWorld->createRevoluteJoint(wheelBody, woodV2Body, ve, true);
pRJV2->SetLimits(0.0f, 0.08f);
pRJV2->EnableLimit(true); ve = woodH1Body->GetWorldCenter();
ve.x = ve.x + 65 / PTM_RATIO;
b2RevoluteJoint *pRJH1 = sWorld->createRevoluteJoint(wheelBody, woodH1Body, ve, true);
pRJH1->SetLimits(0.0f, 0.08f);
pRJH1->EnableLimit(true); ve = woodH2Body->GetWorldCenter();
ve.x = ve.x - 65 / PTM_RATIO;
b2RevoluteJoint *pRJH2 = sWorld->createRevoluteJoint(wheelBody, woodH2Body, ve, true);
pRJH2->SetLimits(0.0f, 0.08f);
pRJH2->EnableLimit(true); //创建四个Box,上下两个为红色,左右两个为绿色
Box *boxRed1 = sWorld->createBox(RED, ccp(520, 115));
Box *boxRed2 = sWorld->createBox(RED, ccp(520, 405));
Box *boxGreen1 = sWorld->createBox(GREEN, ccp(375, 260));
Box *boxGreen2 = sWorld->createBox(GREEN, ccp(665, 260));
//把四个Box分别与四根木条固定在一起
sWorld->createRevoluteJoint(woodV1Body, boxRed1->getBoxBody(), boxRed1->getBoxBody()->GetWorldCenter());
sWorld->createRevoluteJoint(woodV2Body, boxRed2->getBoxBody(), boxRed2->getBoxBody()->GetWorldCenter());
sWorld->createRevoluteJoint(woodH1Body, boxGreen1->getBoxBody(), boxGreen1->getBoxBody()->GetWorldCenter());
sWorld->createRevoluteJoint(woodH2Body, boxGreen2->getBoxBody(), boxGreen2->getBoxBody()->GetWorldCenter());
unschedule(SEL_SCHEDULE(&Lv08::initWorld));
}(woodH2Body, boxGreen2->getBoxBody(), boxGreen2->getBoxBody()->GetWorldCenter());
unschedule(SEL_SCHEDULE(&Lv08::initWorld));
}
createRectBody()和CreateCircleBody()都是自己封装的函数,就是实现一般的创建一个刚体的功能。
createRevoluteJoint()用于创建旋转关节,代码如下
b2RevoluteJoint *World::createRevoluteJoint(b2Body *bA, b2Body *bB, b2Vec2 anchor, bool enableMotor){
b2RevoluteJointDef b2RevJointADef;
b2RevJointADef.motorSpeed = -10.0f;//设置旋转速度
b2RevJointADef.maxMotorTorque = 500;//设置最大力矩
b2RevJointADef.enableMotor = enableMotor;//是否开启马达
b2RevJointADef.Initialize(bA, bB, anchor);//初始化旋转关节
//在世界创建旋转关节
b2RevoluteJoint *b2RevJoint = (b2RevoluteJoint *)world->CreateJoint(&b2RevJointADef);
return b2RevJoint;
}