我正在使用 Box2D 在 LibGDX 上创建应用程序,并且当我给出body(dynamic)
速度并将摩擦力设置为0时,但身体停止了骑在平台上我该如何解决这个问题?
我试图改变身体的摩擦力或速度,并改变块在平台中的位置(使它们彼此更靠近)。
private World world;
private Box2DDebugRenderer b2dr;
private OrthographicCamera camera;
private Player player;
private SpriteBatch spriteBatch;
private Block[] blocks;
@Override
public void show() {
spriteBatch = new SpriteBatch();
world = new World(new Vector2(0,-9.81f),true);
b2dr = new Box2DDebugRenderer();
camera = new OrthographicCamera();
camera.setToOrtho(false, 640/PPM,480/PPM);
world.setContactListener(new MyContactListener());
blocks = new Block[100];
Body body;
PolygonShape shape = new PolygonShape();
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
for(int i = 0;i<blocks.length;i++){
bodyDef.position.set((320+64*i)/PPM,240/PPM);
body = world.createBody(bodyDef);
shape.setAsBox(32/PPM,32/PPM,new Vector2(32/PPM,32/PPM), 0);
fixtureDef.shape = shape;
blocks[i] = new Block(Block.BlockType.GRASS, body.createFixture(fixtureDef));
}
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(320/PPM,400/PPM);
body = world.createBody(bodyDef);
shape.setAsBox(27.5f/PPM,32/PPM,new Vector2((27.5f)/PPM,(32f)/PPM), 0);
fixtureDef.shape = shape;
fixtureDef.friction = 0;
body.setSleepingAllowed(false);
player = new Player(body.createFixture(fixtureDef));
body.setUserData(player);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
b2dr.setDrawContacts(true);
b2dr.render(world, camera.combined);
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
player.draw(spriteBatch,1);
for (Block block : blocks) block.draw(spriteBatch, 1);
spriteBatch.end();
}
private void update(float dt){
//if(Gdx.input.isKeyPressed(Input.Keys.SPACE))
world.step(dt,6,2);
player.update(dt);
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
player.fixture.getBody().setLinearVelocity(5,player.fixture.getBody().getLinearVelocity().y);
if(Gdx.input.isKeyPressed(Input.Keys.LEFT))
player.fixture.getBody().setLinearVelocity(-5,player.fixture.getBody().getLinearVelocity().y);
if(Gdx.input.isKeyJustPressed(Input.Keys.UP) && player.getState() != State.JUMPING && player.getState() != State.FALLING)
player.fixture.getBody().setLinearVelocity(player.fixture.getBody().getLinearVelocity().x,7.5f);
if(Gdx.input.isKeyPressed(Input.Keys.W))
camera.zoom+=0.1;
if(Gdx.input.isKeyPressed(Input.Keys.S))
camera.zoom-=0.1;
if(Gdx.input.isKeyPressed(Input.Keys.A))
camera.translate(-30/PPM,0);
if(Gdx.input.isKeyPressed(Input.Keys.D))
camera.translate(30/PPM,0);
}
}
public class Block extends Sprite {
public Fixture fixture;
public Block(Texture texture, Fixture fixture){
this.fixture = fixture;
setTexture(texture);
setRegion(texture);
setBounds(fixture.getBody().getPosition().x,fixture.getBody().getPosition().y,texture.getWidth()/ Var.PPM, texture.getHeight()/ Var.PPM);
}
static public class BlockType{
public static final Texture GRASS = new Texture("grass.png");
public static final Texture GROUND = new Texture("ground.png");
}
}
所以,我放大了相机,看到一个像素使我的身体停了下来。