我想在BOX-2D中制作一个可以轻松弯曲的柔性杆(如钢尺)。杆的一端是固定的,通过弯曲和释放另一端,我想击球。为此,我使用了许多小体来制作杆,这些小体体通过旋转接头相互连接。然后我用鼠标关节弯曲它。但问题是,在释放它以击晕球之后,我没有获得足够的力量。即使我增加了关节的扭矩,构成杆的物体也会破裂。有没有办法增加力量,还是我应该采用另一种方法制造杆?这是我用来制作杆的代码。
////creating base body......
b2BodyDef bodydef_base_2;
bodydef_base_2.type = b2_staticBody;
bodydef_base_2.position.Set(400.0f/PTM_RATIO,(FLOOR_HEIGHT-50)/PTM_RATIO);
b2Body * body_stat_base_2 = world->CreateBody(& bodydef_base_2);
b2FixtureDef bodyboxDef_base_2;
bodydef_base_2.bullet=true;
b2PolygonShape polygon_body_base_2;
polygon_body_base_2.SetAsBox(.4, .1);
bodyboxDef_base_2.shape = &polygon_body_base_2;
bodyboxDef_base_2.density = 1.0f;
b2Fixture * jointfixture_base_2 = body_stat_base_2->CreateFixture(&bodyboxDef_base_2);
////////creating the rod with bodies and joints...
for (int i = 0; i<body_number; i++) {
sp_2[i] = [CCSprite spriteWithFile:@"1.png"];
[self addChild:sp_2[i]];
sp_2[i].position =ccp(180.0f/PTM_RATIO,(FLOOR_HEIGHT+50+4*i)/PTM_RATIO);
bodydef_2.type = b2_dynamicBody;
bodydef_2.position.Set(400.0f/PTM_RATIO,(FLOOR_HEIGHT+50+4*i-50)/PTM_RATIO);
// bodydef.userData = sp[i];
body_2[i] = world->CreateBody(& bodydef_2);
bodyboxDef_2.shape = &polygon_body_2;
polygon_body_2.SetAsBox(.04/factor, .2/factor);
bodyboxDef_2.density = 15.0F;
bodyboxDef_2.filter.groupIndex = -1;
jointfixture_2 = body_2[i]->CreateFixture(&bodyboxDef_2);
bodydef_2.bullet = true;
if (i==0) {
revolutejointdef_2.Initialize(body_stat_base_2, body_2[i], b2Vec2(233.0f/PTM_RATIO, 100/PTM_RATIO));
} else {
revolutejointdef_2.Initialize(body_2[i-1], body_2[i], b2Vec2(233.0f/PTM_RATIO, 100/PTM_RATIO));
}
revolutejointdef_2.collideConnected = true;
revolutejointdef_2.enableMotor = true;
revolutejointdef_2.enableLimit = true;
revolutejointdef_2.motorSpeed =-5-(body_number-i)*5*0/factor;
revolutejointdef_2.localAnchorA.Set(.02/factor,.2/factor);
revolutejointdef_2.localAnchorB.Set(.02/factor,-.2/factor);
revolutejointdef_2.lowerAngle = CC_DEGREES_TO_RADIANS(0);
revolutejointdef_2.upperAngle = CC_DEGREES_TO_RADIANS(10+i);
revolutejointdef_2.maxMotorTorque = 0;//+i*.03;
revolutjoint_2[i] = (b2RevoluteJoint*)world->CreateJoint(&revolutejointdef_2);
positionarray_2[i] = body_2[i]->GetPosition();
b2MouseJointDef md;
md.bodyA = groundBody;
md.bodyB = body_2[i];
md.target = posarray_2[i];
md.maxForce = 50*i;
mouseJoint_2[i] = (b2MouseJoint *)world->CreateJoint(&md);
}