我正在尝试使用libgdx和box2d在java中制作一辆可移动的汽车; 我开了一辆车,m_car,车位在车中间。 在每个步骤中,我采取m_car的当前位置,使用汽车宽度/高度减少该值,所以我有左下角。当它在直线地面上时,它显示得很好,纹理应该在哪里,但是当地面有一个角度时,纹理位置在汽车的下方或上方。 我不明白TextureRegion是如何工作的,我认为当角度不是0时,我不会画得好,我不知道如何解决它。帮帮我吧:) TY。
编辑:
我的代码是乱七八糟的,它不是一个实际的项目,它是一个ideea,我拿了碎片,把它们放下来看看它们是如何工作的,所以当我启动项目时,我会知道该怎么做,这是我的车声明:
private void car2(){
m_hz = 5.0f;
m_zeta = 0.7f;
m_speed = 50.0f;
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
part1(bd);
part2(bd);
part3(bd);
wheels(bd);
m_car.setAwake(true);
}
private void part3(BodyDef bd) {
PolygonShape shape = new PolygonShape();
shape.setAsBox(0.01f, 0.35f,new Vector2(1.01f - 3, 4.85f - 4), 0);
m_car.createFixture(shape, 1.0f);
}
private void part1(BodyDef bd){
PolygonShape chassis = new PolygonShape();
Vector2 vertices[] = new Vector2[6];
vertices[0] = new Vector2(-2, -0.5f);
vertices[1] = new Vector2(-2, 0.5f);
vertices[2] = new Vector2(1.25f, 0.5f);
vertices[3] = new Vector2(2.5f, 0.15f);
vertices[4] = new Vector2(2.5f, -0.5f);
vertices[5] = new Vector2(-2, -0.5f);
chassis.set(vertices);
bd.position.set(new Vector2(3, 4));
m_car = m_world.createBody(bd);
m_car.createFixture(chassis, 1.0f);
}
private void part2(BodyDef bd){
PolygonShape chassis = new PolygonShape();
Vector2 vertices[] = new Vector2[5];
vertices[0] = new Vector2(3, 4.5f);
vertices[1] = new Vector2(3, 5.25f);
vertices[2] = new Vector2(3.80f, 5.25f);
vertices[3] = new Vector2(4.25f, 4.5f);
vertices[4] = new Vector2(3, 4.5f);
for (int i = 0; i < 5; ++ i){
vertices[i].x -= 3;
vertices[i].y -= 4;
}
chassis.set(vertices);
//m_car = m_world.createBody(bd);
m_car.createFixture(chassis, 1.0f);
}
private void wheels(BodyDef bd){
CircleShape circle = new CircleShape();
circle.setRadius(0.4f);
FixtureDef fd = new FixtureDef();
fd.shape = circle;
fd.density = 1.0f;
fd.friction = 0.9f;
bd.position.set(2f, 3.5f);
m_wheel1 = m_world.createBody(bd);
m_wheel1.createFixture(fd);
bd.position.set(4.5f, 3.5f);
m_wheel2 = m_world.createBody(bd);
m_wheel2.createFixture(fd);
WheelJointDef jd = new WheelJointDef();
Vector2 axis = new Vector2(1.0f, 0.5f);
jd.initialize(m_car, m_wheel1, m_wheel1.getPosition(), axis);
jd.motorSpeed = 0.0f;
jd.maxMotorTorque = 20.0f;
jd.enableMotor = true;
jd.frequencyHz = m_hz;
jd.dampingRatio = m_zeta;
m_spring1 = (WheelJoint) m_world.createJoint(jd);
jd.initialize(m_car, 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 = (WheelJoint) m_world.createJoint(jd);
}
一些explenation:为什么我有顶点去判断3和4?我不知道如果我将位置设置为bodydef,顶点会认为该点为0,0,当我发现时,对我来说更容易下降(因为它仅用于测试,看看它是如何工作的)
这就是我画的方式:
float angle = (float) Math.toDegrees(m_car.getAngle());
batch.draw(textureRegion, x, y, 3f, 4f,
4.5f, 2.75f, 1f, 0.61f, angle);
答案 0 :(得分:1)
对于Box2D中的真实汽车,您可能会发现这很有用:http://www.iforce2d.net/b2dtut/top-down-car
关于角度问题:您需要将汽车的角度提供给spriteBatch。 TextureRegion不知道应该在何处或如何呈现它。你需要告诉spriteBatch这些信息。
你可以使用Sprite。可以使用setRotation(), setPosition()
等操纵精灵,然后使用Sprite.draw(spriteBatch)
代替`spriteBatch.draw(sprite)
此外,请小心您的PIXEL_TO_METER转换,并记住正确更新您的相机。