大家好我正在用cocos2d v1开发游戏,我正在使用box2d。我正在启用视网膜显示器。我正在创建一个桶。我想只在x轴上移动我的水桶,所以我做了一个b2PrismaticJoint,但问题是水桶不适合地面。这是我的代码:`CCSprite * tube = [CCSprite spriteWithFile:@“tube.png”]; tube.position = ccp(SCREEN_WIDTH / 2,SCREEN_HEIGHT / 2); [self addChild:tube];
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);`
当我取下关节时一切都很好..请看附图。谢谢!
答案 0 :(得分: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);