简体   繁体   English

车身角度不变box2D C ++

[英]Car body's angle not changing box2D C++

i'm making a side-scrolling car game with box2D. 我正在用box2D制作侧滚动汽车游戏。 I'm currently working on the car and it seems that i'm stuck. 我目前正在汽车上工作,似乎卡住了。 The chassis of my car isn't rotating when for example the car is trying to climb a hill. 例如,当汽车试图爬坡时,我的汽车底盘没有旋转。 I don't know if its normal or if i should set the angle of the body. 我不知道它是正常的还是应该设置身体的角度。 Here's a quick video that shows the problem : https://streamable.com/d802n 这是显示问题的快速视频: https : //streamable.com/d802n

This is my code : 这是我的代码:

b2BodyDef carBox = b2BodyDef();
carBox.position = b2Vec2(bodyCenterPosition.x, bodyCenterPosition.y);
carBox.type = b2_dynamicBody;
car = game->getWorld()->CreateBody(&carBox);

b2PolygonShape carPolygon = b2PolygonShape();
carPolygon.SetAsBox(bodySize.x, bodySize.y);
b2FixtureDef carFix = b2FixtureDef();
carFix.density = 0.0f;
carFix.shape = &carPolygon;
car->CreateFixture(&carFix);

b2PolygonShape headPolygon = b2PolygonShape();
headPolygon.SetAsBox(headSize.x, headSize.y);
for (int i = 0; i < 8; i++) {
    headPolygon.m_vertices[i].x -= 8.0f / RATIO;
    headPolygon.m_vertices[i].y -= 24.0f / RATIO;
}
b2FixtureDef headFix = b2FixtureDef();
headFix.density = 0.0f;
headFix.shape = &headPolygon;
car->CreateFixture(&headFix);


b2CircleShape circleShape;
circleShape.m_radius = 0.35f;
circleShape.m_p.SetZero();

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

b2BodyDef wheel1Def;
wheel1Def.type = b2_dynamicBody;
wheel1Def.position = b2Vec2(backWheelCenterPosition.x, backWheelCenterPosition.y);
backWheel = game->getWorld()->CreateBody(&wheel1Def);
backWheel->CreateFixture(&fd);

b2BodyDef wheel2Def;
wheel2Def.type = b2_dynamicBody;
wheel2Def.position = b2Vec2(frontWheelCenterPosition.x, frontWheelCenterPosition.y);
frontWheel = game->getWorld()->CreateBody(&wheel2Def);
frontWheel->CreateFixture(&fd);

b2WheelJointDef springDef1;
springDef1.dampingRatio = 50.0f;
springDef1.maxMotorTorque = 1.0f;
springDef1.frequencyHz = 15.0f;
springDef1.motorSpeed = 0.0f;
springDef1.enableMotor = true;
springDef1.Initialize(car, backWheel, backWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
backSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);

springDef1.Initialize(car, frontWheel, frontWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
frontSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);

It is normal for a body with a fixture having a zero density to behave like an infinite mass. 具有固定密度为零的固定装置的物体的行为就像无限质量一样,这是正常的。 Sounds like that's not what you intended however. 听起来那不是您想要的。

Looking at the code presented, we see that the carFix.density and headFix.density are being set to 0.0f . 查看提供的代码,我们看到carFix.densityheadFix.density设置为0.0f Set these to a positive non-zero value , like 1.0f , and the dynamic body they're created on should behave more like a physical mass would. 将它们设置为正的非零值 ,例如1.0f ,并且在其上创建动态对象的行为应更像物理质量。 That's assuming all of the other fixtures created on that body also have a density that's greater than zero as well. 假设在该物体上创建的所有其他灯具的密度也都大于零。

For a tutorial on fixtures and density, I'd recommend taking a look at iforce2d's Box2D C++ tutorials - Fixtures . 有关固定装置和密度的教程,建议您看一下iforce2d的Box2D C ++教程-固定装置

Hope this solves the problem or at least helps. 希望这可以解决问题或至少有所帮助。

By the way, I loved the artwork shown in the video! 顺便说一句,我喜欢视频中显示的艺术品!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM