我想在java中使用libgdx和box2d製作一個可移動的汽車; 我做了一輛車,m_car,位置設置在車中間。 在每一步,我採取m_car的當前位置,減少值與汽車的寬度/高度,所以我有左下角。當在直線地面上時,它表現出色,紋理應該是在哪裏,但是當地面有一個角度時,紋理將定位在其下面或上方。 我不明白TextureRegion是如何工作的,我認爲當角度不是0時,我沒有把握得好,我不知道如何解決它。幫我請他:) Ty。使用textureRegion的繪製
編輯:
我的代碼是一個爛攤子,它不是一個實際的項目,它的IDEEA,我就拿塊,並把它們記錄下來,看看他們是如何工作的,所以當IL啓動該項目,我會知道該怎麼做,這是我的汽車宣言:
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,當我發現,是easyer我只是減少(因爲它只是爲了測試,看看它是如何工作的)
,這是我畫:
float angle = (float) Math.toDegrees(m_car.getAngle());
batch.draw(textureRegion, x, y, 3f, 4f,
4.5f, 2.75f, 1f, 0.61f, angle);
ty for info :)我給紋理區域的角度,但不是繪製的好位置:這是我的問題: https://www.dropbox.com/s/divt8fbg3jri6go/Untitled4.jpg 角度是好的,它的定位不好,以及idk爲什麼,可能當角度改變時,繪製位置發生變化,但是idk如何改變,我應該給出什麼位置 –
這看起來可能是原點問題。如果身體/圖片的來源不同,或者不在汽車中間,圖片可能會上下移動,具體取決於角度。但沒有更多的代碼只是猜測 – noone
原點必須是紋理的中心嗎? –