我对Box2d还是很陌生,我正在尝试制作一个简单的程序,使物体在上面绘制精灵时掉落。当我运行我的程序时,除了身体不断降低速度,但仍在加速,一切正常。例如,速度变为0,-10,-20,-30,-40,-50,-60,-20,-30,-40,-50 ...等。我做错了很多事情,因为我对此很陌生。谢谢您的帮助!
<KeyboardAvoidingView style={{flex: 1}}>
}
答案 0 :(得分:0)
Box2d最好使用0.1到10的值,因为这些大小上的值开始降低精度。每个Box2D单元应该代表1米,因此您的对象是256米x 256米,这是巨大的。再加上Box2D将对象的最大速度限制在每步2米的事实,这意味着您的256m正方形对象在60 FPS(每秒250英里)的速度下每秒以120米的速度掉落。
这通常通过在屏幕单位和box2d单位之间使用某种形式的转换来解决。使用常量值将256转换为0.1到10之间的范围,或者使用相机矩阵在屏幕上放大对象。
我已更新您的示例以使用视口。这不能用Box2D的最大速度来修复问题,但它应该通过使用与Box2D设计为最佳配合使用的单位相匹配的较小对象来帮助降低效果。
package com.mygdx.gtest;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
public class Test extends ApplicationAdapter {
private SpriteBatch batch;
private World world;
private BodyDef bodyDef;
private Body body;
private Texture texture;
private Sprite sprite;
private Box2DDebugRenderer debugRenderer;
private ExtendViewport viewport;
@Override
public void create() {
debugRenderer = new Box2DDebugRenderer(true,true,true,true, true, true);
viewport = new ExtendViewport(64,48); // adding viewport so we can use multiple resolutions easily
viewport.getCamera().position.set(32, 24, 0); // set cam position to make 0,0 bottom left corner
batch = new SpriteBatch();
batch.setProjectionMatrix(viewport.getCamera().combined); // tell the batch how things should be rendered
texture = new Texture("badlogic.jpg");
sprite = new Sprite(texture);
sprite.setSize(10,10); // set the size of the sprite in box2d units (10 meters x 10 meters)
// not needed as position is set in update method
//sprite.setPosition(Gdx.graphics.getWidth() / 2 - sprite.getWidth() / 2, Gdx.graphics.getHeight() / 2);
world = new World(new Vector2(0, -10), true);
bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(32 , 47);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(5,5);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
@Override
public void render() {
world.step(Gdx.graphics.getDeltaTime(), 8, 3);
sprite.setPosition(body.getPosition().x - (sprite.getWidth()/2), body.getPosition().y -(sprite.getHeight()/2));
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(viewport.getCamera().combined); // tell the batch how things should be rendered incase of resized window
batch.begin();
batch.draw(sprite, sprite.getX(), sprite.getY(),sprite.getWidth(),sprite.getHeight());
batch.end();
System.out.println(body.getLinearVelocity().y);
debugRenderer.render(world, viewport.getCamera().combined);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
world.dispose();
debugRenderer.dispose();
}
}