b2Circle形状不在b2Polygon形状上旋转Box2d Cocos2dx

时间:2014-05-20 18:00:16

标签: box2d cocos2d-x

好吧,我有一辆车,当一个矩形块旋转时,车轮没有向前移动。

轮子 - > b2CircleShape
矩形块 - > b2PolygonShape

我同时给出了密度和摩擦力。仍然没有!

具体来说,我在汽车底盘和车轮之间有一个车轮关节。 我希望通过给车轮关节电机提供正速来使汽车前进。

代码:

b2PolygonShape boxSahpe;
boxSahpe.SetAsBox(68/PTM_RATIO, 40/PTM_RATIO);

b2BodyDef bd;
bd.type = b2_dynamicBody;

bd.position.Set(8.85f, 10.0f);
bd.userData = this;
body = world->CreateBody(&bd);
body->CreateFixture(&boxSahpe, 1.0f);


b2BodyDef circleBD;
circleBD.type = b2_dynamicBody;

b2CircleShape circle;
circle.m_radius = 0.4f;

b2FixtureDef fd;
fd.shape = &circle;
fd.density = 1.0f;
fd.friction = 0.9f;

// left
circleBD.position.Set(7.70f, 9.0f);
m_wheel1 = world->CreateBody(&circleBD);
m_wheel1->CreateFixture(&fd);

// right
circleBD.position.Set(9.75f, 9.0f);
m_wheel2 = world->CreateBody(&circleBD);
m_wheel2->CreateFixture(&fd);

b2WheelJointDef jd;
b2Vec2 axis(0.0f, 1.10f);

jd.Initialize(body, m_wheel1, m_wheel1->GetWorldCenter(), axis);
jd.motorSpeed = 0.0f;
jd.maxMotorTorque = 20.0f;
jd.enableMotor = true;
jd.frequencyHz = m_hz;
jd.dampingRatio = m_zeta;
m_spring1 = (b2WheelJoint*)world->CreateJoint(&jd);

jd.Initialize( body,m_wheel2, m_wheel2->GetPosition(), axis);
jd.motorSpeed = 0.0f;
jd.maxMotorTorque = 10.0f;
jd.enableMotor = false;
jd.frequencyHz = m_hz;
jd.dampingRatio = m_zeta;
m_spring2 = (b2WheelJoint*)world->CreateJoint(&jd);

m_spring 1& 2 - > b2WheelJoint
m_wheel 1& 2 - > b2Body
身体 - > b2Body

1 个答案:

答案 0 :(得分:1)

确实建议您使用调试绘图显示,否则它就像闭着眼睛猜测一样。在这种情况下,我认为你会立即看到问题 - 这是我尝试时的情况:

enter image description here

对于车轮关节,频率值(例如2)的值较低,它们没有足够的刚度来保持主体离开地面。你可以使频率更高(大约4似乎足以让它移动),但我猜你可能想让主体更小。