0

大家好,我正在使用 cocos2d v1 开发游戏,并且正在使用 box2d。我正在启用视网膜显示。我正在创建一个桶。我只想在 x 轴上移动我的桶,所以我做了一个 b2PrismaticJoint 但问题是桶不适合地面。这是我的代码:`CCSprite *tube = [CCSprite spriteWithFile:@"tube.png"]; tube.position = ccp(SCREEN_WIDTH/2, SCREEN_HEIGHT/2); [自我添加孩子:管];

b2BodyDef bucketBodyDef;
bucketBodyDef.type = b2_dynamicBody;
bucketBodyDef.position.Set(tube.contentSize.width/PTM_RATIO, tube.contentSize.height/PTM_RATIO);
bucketBodyDef.userData = tube;
tubeBody = world->CreateBody(&bucketBodyDef);

b2PolygonShape bucketShape;
bucketShape.SetAsBox(tube.contentSize.width/2/PTM_RATIO, tube.contentSize.height/2/PTM_RATIO);


// Create shape definition and add to body
b2FixtureDef bucketShapeDef;
bucketShapeDef.shape = &bucketShape;
bucketShapeDef.density = 10.0f;
bucketShapeDef.friction = 0.4f;
tubeFixture = tubeBody->CreateFixture(&bucketShapeDef);


[self createTubeFixtures];


// Restrict bucket along the x axis
b2PrismaticJointDef jointDef;
b2Vec2 worldAxis(1.0f, 0.0f);
jointDef.collideConnected = true;
jointDef.Initialize(tubeBody, groundBody,
                    tubeBody->GetWorldCenter(), worldAxis); 
world->CreateJoint(&jointDef);`

当我卸下接头时,一切都很好……请参见附图。谢谢!

带接头

无接头

4

1 回答 1

0

我犯了一个小错误,花了我两天时间!!那是设置身体的坐标:b2BodyDef bucketBodyDef; bucketBodyDef.type = b2_dynamicBody; **bucketBodyDef.position.Set(tube.contentSize.width/2/PTM_RATIO, tube.contentSize.height/2/PTM_RATIO);** bucketBodyDef.userData = tube; tubeBody = world->CreateBody(&bucketBodyDef);

于 2013-04-03T20:33:50.170 回答