我正在为我的Android游戏(Java + LibGDX)开发基于Box 2d物理的移动车辆。当我以为我拥有一切并运行我的时候,我的带有两个轮子的车站在地面上但是当我旋转车轮时车子不动。
这是我的游戏课程:
public class MyGdxGame implements ApplicationListener {
World world = new World(new Vector2(0, -10), true);
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
static final float BOX_STEP=1/60f;
static final int BOX_VELOCITY_ITERATIONS=6;
static final int BOX_POSITION_ITERATIONS=2;
static final int WIDTH=256;
static final int HEIGHT=169;
Hero hero;
MovableBox box;
@Override
public void create() {
camera = new OrthographicCamera();
camera.viewportHeight = HEIGHT;
camera.viewportWidth = WIDTH;
camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);
camera.update();
Vector2[] anArray;
anArray = new Vector2[4];
anArray[0]= new Vector2(-30,0);
anArray[1]= new Vector2(55,55);
anArray[2]= new Vector2(110,55);
anArray[3]= new Vector2(195,0);
//Ground body
BodyDef groundBodyDef =new BodyDef();
groundBodyDef.position.set(new Vector2(-8-13-20-20-10, -44));
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.set(anArray);
//groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
groundBody.createFixture(groundBox, 0.0f);
//Ground body
BodyDef groundBodyDef2 =new BodyDef();
groundBodyDef2.position.set(new Vector2(13*11-4-7-8-13-20-20-10, -44));
Body groundBody2 = world.createBody(groundBodyDef2);
PolygonShape groundBox2 = new PolygonShape();
groundBox2.set(anArray);
//groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
groundBody2.createFixture(groundBox2, 0.0f);
hero = new Hero();
hero.create(world, 0, 0);
box.create(world, WIDTH / 4 * 3, HEIGHT / 2);
boxes.add(box);
debugRenderer = new Box2DDebugRenderer();
}
private void update(){
hero.update();
for(MovableBox b : boxes) {
b.update();
}
}
@Override
public void dispose() {
}
@Override
public void render() {
update();
camera.position.set(hero.getPos(), 0f);
camera.update();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
和英雄级(实际上应该称之为汽车)
Body body;
Body wheel, wheel2;
public void create(World world, int posx, int posy){
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(posx, posy);
body = world.createBody(bodyDef);
PolygonShape dynamicCircle = new PolygonShape();
dynamicCircle.setAsBox(8, 3);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicCircle;
fixtureDef.density = 4.0f;
fixtureDef.restitution = 0.1f;
fixtureDef.friction=0.95f;
body.createFixture(fixtureDef);
BodyDef bodyDef2 = new BodyDef();
bodyDef2.type = BodyDef.BodyType.DynamicBody;
wheel = world.createBody(bodyDef2);
CircleShape dynamicCircle2 = new CircleShape();
dynamicCircle2.setRadius(2);
FixtureDef fixtureDef2 = new FixtureDef();
fixtureDef2.shape = dynamicCircle2;
fixtureDef2.density = 4;
fixtureDef2.friction=3.0f;
fixtureDef2.restitution=0.1f;
wheel.createFixture(fixtureDef2);
wheel2 = world.createBody(bodyDef2);
wheel2.createFixture(fixtureDef2);
RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.bodyA = body;
revoluteJointDef.bodyB = wheel;
revoluteJointDef.localAnchorA.add(-8,-6);
world.createJoint(revoluteJointDef);
RevoluteJointDef revoluteJointDef2 = new RevoluteJointDef();
revoluteJointDef2.bodyA = body;
revoluteJointDef2.bodyB = wheel2;
revoluteJointDef2.localAnchorA.add(8,-6);
world.createJoint(revoluteJointDef2);
}
public Vector2 getPos(){
return body.getPosition();
}
public void update(){
if(Gdx.input.isTouched())
{
if(Gdx.input.getX()<600){
wheel.setTransform(wheel.getPosition().x,wheel.getPosition().y,wheel.getAngle()+0.1f);
wheel2.setTransform(wheel2.getPosition().x,wheel2.getPosition().y, wheel2. getAngle() + 0.1f);
}
else{
wheel.setTransform(wheel.getPosition().x,wheel.getPosition().y,wheel.getAngle()-0.1f);
wheel2.setTransform(wheel2.getPosition().x,wheel2.getPosition().y,wheel2.getAngle()-0.1f);
}
}
}
答案 0 :(得分:0)
嗯,我认为你需要考虑两件事。
首先,我想如果你想让你的物理引擎“强迫”#34;如果汽车在车轮上旋转,你可能应该研究BodyDef
(linearDamping
和angularDamping
)的阻尼参数。设置地面和车轮可能会帮助您在车轮上旋转。
第二件事是你真的需要分析它是否是正确的方法。也许你应该考虑通过移动汽车车身和旋转车轮来模拟汽车的运动。我认为这种方法可以让你更好地控制汽车的运动。