我正在尝试用libgdx中的box2d制作样本。
我希望球在box2d世界中随着重力而下降。现在它下降但没有加速(我看到球在加速度= 0时下降)。我试图添加阻尼,密度......但没有变化。请帮我找到我的错。非常感谢你。
这是此问题的视频:youtube.com/watch?v=NkbcVMsB2S0
;在此视频中,您可以看到圆圈在加速度= 0时下降。我试图改变一些参数,但看起来加速度始终为零。
这是一个简单的课程:
public class MyGdxGame implements ApplicationListener, InputProcessor {
World world;
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
static final float BOX_STEP = 1 / 45f;
static final int BOX_VELOCITY_ITERATIONS = 6;
static final int BOX_POSITION_ITERATIONS = 2;
static final float WORLD_TO_BOX = 0.01f;
static final float BOX_WORLD_TO = 100f;
private int counter = 100;
private Body body;
@Override
public void create() {
world = new World(new Vector2(0, -200f), false);
world.setContinuousPhysics(true);
camera = new OrthographicCamera();
camera.viewportHeight = 320;
camera.viewportWidth = 480;
camera.position.set(camera.viewportWidth * .5f,
camera.viewportHeight * .5f, 0f);
camera.update();
// Ground body
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vector2(0, 10));
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBody.createFixture(groundBox, 0.0f);
// Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(camera.viewportWidth / 2, camera.viewportHeight);
bodyDef.angularDamping = 0.01f;
bodyDef.linearDamping = 0.0f;
body = world.createBody(bodyDef);
CircleShape dynamicCircle = new CircleShape();
dynamicCircle.setRadius(10f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicCircle;
fixtureDef.density = 10.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0f;
body.createFixture(fixtureDef);
MassData massData = new MassData();
massData.mass = 50f;
body.setMassData(massData);
body.setGravityScale(1f);
debugRenderer = new Box2DDebugRenderer();
Gdx.input.setInputProcessor(this);
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if (counter < 10) {
body.applyForceToCenter(0, 200000f, true);
}
counter++;
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() {
}
@Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
counter = 0;
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
答案 0 :(得分:4)
我认为这是因为身体瞬间处于最大速度(因为重力非常高)。 Box2D有最大速度,我认为每步2米。您似乎是在移动设备上运行它,通常限制为60FPS。这意味着您每秒最多可以执行60次更新步骤,最大速度/速度为120m / s。顺便说一句,您的更新速率取决于帧速率。
我看到你已经定义了米到像素和像素到米的常数,但你没有在代码中的任何地方使用它们。这意味着你使用1:1的米到像素比率,因此任何身体可以拥有的最大速度是120px / s。这不是很多,从视频判断它可能是正确的。
常见的诀窍是缩小相机的视口,而不是缩放所有与box2d相关的内容。像这样:
camera.viewportHeight = Gdx.graphics.getHeight() * WORLD_TO_BOX;
camera.viewportWidth = Gdx.graphics.getWidth() * WORLD_TO_BOX;
答案 1 :(得分:0)
我有同样的问题。在我的情况下,解决方案是将BOX_STEP参数的值更改为1/240(以前为1/60,然后最大线速度为120)
static final float BOX_STEP=1/240f;
static final int BOX_VELOCITY_ITERATIONS=20;
static final int BOX_POSITION_ITERATIONS=3;
所以在渲染器()步骤中现在采用上面的
world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
我还介绍了noone answear,但没有改变步骤方法它没有工作