我正在创建一个应用程序,我试图在其中加入两个身体,这样当我移动一个身体时,第二个身体应该在第一个身体内移动。
这是我创建正文的代码:
- (b2Body *)addBoxBodyForDynamicSprite:(CCSprite *)sprite {
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
//spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO, sprite.position.y/PTM_RATIO);
CGPoint asolutePoint = [sprite.parent convertToWorldSpace:sprite.position];
spriteBodyDef.position.Set(asolutePoint.x/PTM_RATIO, asolutePoint.y/PTM_RATIO);
spriteBodyDef.userData = (__bridge void*)sprite;
b2Body *spriteBody = world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,
sprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 0.3;
spriteShapeDef.isSensor = true;
spriteBody->CreateFixture(&spriteShapeDef);
return spriteBody;
}
一个物体是运动的,另一个物体是动态的。我正在使用以下方法移动这些机构:
theBody->SetTransform(locationWorld, theBody->GetAngle());
如果我在这里施加线性力,身体不会移动,而我用来固定它们的关节是b2WeldJoint
。
b2JointDef jointDef;
jointDef.bodyA = another;
jointDef.bodyB = leftHandFixBody;
aJoint = (b2Joint *)world->CreateJoint(&jointDef);
它移动动态体,但运动体保持在其位置上。我想把两个身体一起移动。任何帮助将不胜感激。谢谢!