- 绿色方块是 box2d 地面。
- 较大的圆圈通过旋转接头与地面相连,并且可以旋转。
- 所有的圆圈都是 b2_dynamicBody
我想在大圆圈的边缘或边界上连接较小的圆圈,如下所示。请告诉我如何实现它以及我必须使用什么样的关节?此外,当我旋转大圆圈时,小圆圈应该粘在它的位置。
我想在大圆圈的边缘或边界上连接较小的圆圈,如下所示。请告诉我如何实现它以及我必须使用什么样的关节?此外,当我旋转大圆圈时,小圆圈应该粘在它的位置。
通过这段代码....
// 在钩子大圆之间创建关节
wheelSprite = [[CCSprite alloc] initWithFile:@"heroWheel.png"];
wheelSprite.visible = FALSE;
wheelSprite.scale = 1.0*hero_size_factor;
wheelSprite.rotation = 0.0;
wheelSprite.anchorPoint = CGPointMake(0.5,0.5);
[layer addChild:wheelSprite z:5];
ballBodyDef.userData = wheelSprite;
//create hook between big circle
hook = world->CreateBody(&ballBodyDef);
b2PolygonShape hookShape;
hookShape.SetAsBox(0.2,0.1);
b2FixtureDef hookDef;
hookDef.shape=&hookShape;
hookDef.density=0.5f*hero_size_factor;
hookDef.friction = 0.0f;
hookDef.restitution = 0.0f;
hookDef.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&hookDef);
//create circle shape fixture
b2CircleShape wheel1,wheel2,wheel3,wheel4;
float wheelRadius = hero_width/4.0f;
//Create fixture with above shape
b2FixtureDef ballShapeDef1,ballShapeDef2,ballShapeDef3,ballShapeDef4;
wheel1.m_p = b2Vec2(wheelRadius, wheelRadius);
wheel1.m_radius = wheelRadius;
ballShapeDef1.shape = &wheel1;
ballShapeDef1.density = 0.0f;
ballShapeDef1.friction = 0.0f;
ballShapeDef1.restitution = 0.0f;
ballShapeDef1.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&ballShapeDef1);
wheel2.m_radius = wheelRadius;
wheel2.m_p = b2Vec2(-wheelRadius, -wheelRadius);
ballShapeDef2.shape = &wheel2;
ballShapeDef2.density = 0.0f;
ballShapeDef2.friction = 0.0f;
ballShapeDef2.restitution = 0.0f;
ballShapeDef2.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&ballShapeDef2);
wheel3.m_radius = wheelRadius;
wheel3.m_p = b2Vec2(wheelRadius,-wheelRadius);
ballShapeDef3.shape = &wheel3;
ballShapeDef3.density = 0.0f;
ballShapeDef3.friction = 0.0f;
ballShapeDef3.restitution = 0.0f;
ballShapeDef3.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&ballShapeDef3);
wheel4.m_radius = wheelRadius;
wheel4.m_p = b2Vec2(-wheelRadius,wheelRadius);
ballShapeDef4.shape = &wheel4;
ballShapeDef4.density = 0.0f;
ballShapeDef4.friction = 0.0f;
ballShapeDef4.restitution = 0.0f;
ballShapeDef4.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&ballShapeDef4);
//Create Revolute Joints between hook and big circle
b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.bodyA = hook;
revoluteJointDef.bodyB = Leg;
revoluteJointDef.collideConnected = false;
revoluteJointDef.localAnchorA.Set(0.0,0.0);
revoluteJointDef.localAnchorB.Set(0.0,-(hero_height - 0.1));
revoluteJointDef.referenceAngle = 0;
revoluteJointDef.maxMotorTorque = 5.0*hero_size_factor;
Hero_Motor = (b2RevoluteJoint*)world->CreateJoint(&revoluteJointDef);