我的 box2d body 下降速度太慢
My box2d body drops way too slow
我几乎尝试了所有方法,但没有任何效果。
如果你 运行 它你会看到一个正方形下落非常缓慢。
我是初学者,所以解释起来不要太复杂。
关于此代码的任何问题 dylan.missu@gmail.com
这是我的代码:
@Override
public void show() {
camera = new OrthographicCamera();
debugRenderer = new Box2DDebugRenderer();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(100,100);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Gdx.graphics.getHeight()/6,Gdx.graphics.getHeight()/6);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
fixtureDef.restitution = 2;
Fixture fixture = body.createFixture(fixtureDef);
}
private void line(float X, float Y, float w, float h)
{
BodyDef bodyDef = new BodyDef();
bodyDef.type=BodyDef.BodyType.StaticBody;
bodyDef.position.set(X,Y);
PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(w,h);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=0.4f;
fixtureDef.friction=0.5f;
Body theFloor=world.createBody(bodyDef);
theFloor.createFixture(fixtureDef);
}
private void wall(float A)
{
// is there a better way of doing this?
line(0,Gdx.graphics.getHeight()/2-A,Gdx.graphics.getWidth()/2-A,0);
line(Gdx.graphics.getWidth()/2-A,0,0,Gdx.graphics.getHeight()/2-A);
line(0,-Gdx.graphics.getHeight()/2+A,Gdx.graphics.getWidth()/2-A,0);
line(-Gdx.graphics.getWidth()/2+A,0,0,Gdx.graphics.getHeight()/2-A);
}
@Override
public void render(float delta) {
world.step(1 / 5f, 6, 2);
OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
processAccelerometer();
wall(1);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
}
@Override
public void dispose() {
world.dispose();
debugRenderer.dispose();
}
private void processAccelerometer() {
float y = Gdx.input.getAccelerometerY();
float x = Gdx.input.getAccelerometerX();
if (prevAccelX != x || prevAccelY != y) {
world.setGravity(new Vector2(y, -x));
prevAccelX = x;
prevAccelY = y;
}
}
@Override
public void hide()
{
// TODO: Implement this method
}
@Override
public void resize(int p1, int p2)
{
// TODO: Implement this method
}
@Override
public void resume()
{
// TODO: Implement this method
}
@Override
public void pause()
{
// TODO: Implement this method
}
@Override
public void render()
{
// TODO: Implement this method
}
}
在 Box2D 中,您必须考虑到 1 像素 = 1 米。所以,基本上,您在 Box2D 中模拟的所有内容默认情况下都是巨大的。并且 Box2D 模拟对于大距离、大质量、大速度并不准确...
因此,您所要做的就是使用转换因子转换视口,这样您就可以看到更容易模拟的小实体。
例如,假设您想要 100 像素 = 1 米,您可以在创建游戏画面时输入该代码:
WORLD_TO_BOX = 1/100f;
BOX_TO_WORLD = 1/WORLD_TO_BOX;
//Creation of the camera with your factor 100
camera = new OrthographicCamera();
camera.viewportHeight = Gdx.graphics.getHeight() * WORLD_TO_BOX;
camera.viewportWidth = Gdx.graphics.getWidth() * WORLD_TO_BOX;
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f);
camera.update();
然后,您将根据 camera.viewportWidth
和 camera.viewportHeight
而不是 Gdx.graphics.getWidth()
和 Gdx.graphics.getHeight()
创建框
我的情况是:
PolygonShape shape = new PolygonShape();
shape.setAsBox(camera.viewportHeight/6,camera.viewportHeight/6);
你可以在我的代码中看到,还有一个BOX_TO_WORLD
转换因子。当你想在你的 box2D 物体上渲染图形时会用到它。
为此,请查看 .
我几乎尝试了所有方法,但没有任何效果。 如果你 运行 它你会看到一个正方形下落非常缓慢。 我是初学者,所以解释起来不要太复杂。 关于此代码的任何问题 dylan.missu@gmail.com 这是我的代码:
@Override
public void show() {
camera = new OrthographicCamera();
debugRenderer = new Box2DDebugRenderer();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(100,100);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Gdx.graphics.getHeight()/6,Gdx.graphics.getHeight()/6);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
fixtureDef.restitution = 2;
Fixture fixture = body.createFixture(fixtureDef);
}
private void line(float X, float Y, float w, float h)
{
BodyDef bodyDef = new BodyDef();
bodyDef.type=BodyDef.BodyType.StaticBody;
bodyDef.position.set(X,Y);
PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(w,h);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=0.4f;
fixtureDef.friction=0.5f;
Body theFloor=world.createBody(bodyDef);
theFloor.createFixture(fixtureDef);
}
private void wall(float A)
{
// is there a better way of doing this?
line(0,Gdx.graphics.getHeight()/2-A,Gdx.graphics.getWidth()/2-A,0);
line(Gdx.graphics.getWidth()/2-A,0,0,Gdx.graphics.getHeight()/2-A);
line(0,-Gdx.graphics.getHeight()/2+A,Gdx.graphics.getWidth()/2-A,0);
line(-Gdx.graphics.getWidth()/2+A,0,0,Gdx.graphics.getHeight()/2-A);
}
@Override
public void render(float delta) {
world.step(1 / 5f, 6, 2);
OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
processAccelerometer();
wall(1);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
}
@Override
public void dispose() {
world.dispose();
debugRenderer.dispose();
}
private void processAccelerometer() {
float y = Gdx.input.getAccelerometerY();
float x = Gdx.input.getAccelerometerX();
if (prevAccelX != x || prevAccelY != y) {
world.setGravity(new Vector2(y, -x));
prevAccelX = x;
prevAccelY = y;
}
}
@Override
public void hide()
{
// TODO: Implement this method
}
@Override
public void resize(int p1, int p2)
{
// TODO: Implement this method
}
@Override
public void resume()
{
// TODO: Implement this method
}
@Override
public void pause()
{
// TODO: Implement this method
}
@Override
public void render()
{
// TODO: Implement this method
}
}
在 Box2D 中,您必须考虑到 1 像素 = 1 米。所以,基本上,您在 Box2D 中模拟的所有内容默认情况下都是巨大的。并且 Box2D 模拟对于大距离、大质量、大速度并不准确...
因此,您所要做的就是使用转换因子转换视口,这样您就可以看到更容易模拟的小实体。
例如,假设您想要 100 像素 = 1 米,您可以在创建游戏画面时输入该代码:
WORLD_TO_BOX = 1/100f;
BOX_TO_WORLD = 1/WORLD_TO_BOX;
//Creation of the camera with your factor 100
camera = new OrthographicCamera();
camera.viewportHeight = Gdx.graphics.getHeight() * WORLD_TO_BOX;
camera.viewportWidth = Gdx.graphics.getWidth() * WORLD_TO_BOX;
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f);
camera.update();
然后,您将根据 camera.viewportWidth
和 camera.viewportHeight
而不是 Gdx.graphics.getWidth()
和 Gdx.graphics.getHeight()
我的情况是:
PolygonShape shape = new PolygonShape();
shape.setAsBox(camera.viewportHeight/6,camera.viewportHeight/6);
你可以在我的代码中看到,还有一个BOX_TO_WORLD
转换因子。当你想在你的 box2D 物体上渲染图形时会用到它。
为此,请查看