问题描述
我创建了一个具有地球重力的世界,并将一个实体(包含一个精灵和一个物体)放置在场景中,它像气球一样缓慢下落.
I've created a World with earth gravity and I place an entity in the scene (contains a sprite and a Body) and it falls down slowly like a balloon.
这是我设定世界的方式:
Here's how I set the World:
world = new World(new Vector2(0, -GRAVITY_EARTH), true);
这是Body等的相关Box2D代码:
and here's the relevant Box2D code for the Body etc:
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(positionX, positionY);
// Create our body in the world
body = world.createBody(bodyDef);
// Grab the first idle sprite to use as initial
Sprite sprite = idleSprites.get(0);
// Create a box shape to represent our hit box
PolygonShape box = new PolygonShape();
box.setAsBox(sprite.getWidth() / 2f, sprite.getHeight() / 2f);
// Create a fixture definition to apply our shape
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = box;
fixtureDef.density = 1f; // Give it full density
fixtureDef.friction = 0f; // Give it no friction
fixtureDef.restitution = 0f; // Make it not bouncy
// Create our fixture and attach it to the body
fixture = body.createFixture(fixtureDef);
// Remember to dispose of any shapes after you're done with them!
// BodyDef and FixtureDef don't need disposing, but shapes do.
box.dispose();
以及我如何绘制精灵:
TextureRegion keyFrame = idleAnimation.getKeyFrame(stateTimeSeconds, true);
Vector2 position = body.getPosition();
batch.draw(keyFrame, position.x - keyFrame.getRegionWidth() / 2f, position.y - keyFrame.getRegionHeight() / 2f);
和render()方法中的相关代码:
and the relevant code in the render() method:
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
final float deltaTime = Gdx.graphics.getDeltaTime();
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
spriteBatch.draw(sky, 0, 0);
tim.animate(spriteBatch, deltaTime);
spriteBatch.draw(floor, 0, 0);
spriteBatch.end();
// Render physics for debug
debugRenderer.render(world, camera.combined);
// Run physics
doPhysicsStep(deltaTime);
}
private void doPhysicsStep(float deltaTime) {
// fixed time step
// max frame time to avoid spiral of death (on slow devices)
float frameTime = Math.min(deltaTime, 0.25f);
accumulator += frameTime;
while (accumulator >= TIME_STEP) {
world.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
accumulator -= TIME_STEP;
}
}
我尝试过更改灯具的密度,并且尝试过更改重力值,并且尝试过更改TIME_STEP,但没有任何效果.身体就像气球一样缓慢下落.
I've tried changing the density of the fixture, and I've tried changing the gravity value, and I've tried changing the TIME_STEP and nothing is having an effect. The body just falls down slowly like a balloon.
推荐答案
在我看来,您好像以像素为单位,box2d将每个单位都视为米,因此您达到了2.0单位的内部限制每个时间步,请参阅 http://www.iforce2d.net/b2dtut/gotchas .您可以通过以世界单位而不是像素设置相机来解决此问题,但是您必须缩放所有子画面和位置以适合世界单位而不是像素.
这样的事情可能会做到:
It looks to me like you're using pixels as your units, box2d treats every unit as a meter and so you're hitting the internal limit of 2.0 units per time step, see http://www.iforce2d.net/b2dtut/gotchas. You can get around this by setting up your camera in world units instead of pixels, you have to scale all your sprites and positions to fit into world units instead of pixels though.
Something like this may do:
float w = (float) Gdx.graphics.getWidth();
float h = (float) Gdx.graphics.getHeight();
camera = new OrthographicCamera(30, 30 * (h / w));
此处设置相机的方式允许视口的高度根据屏幕的纵横比而变化.
the way the camera is set up here allows the height of the viewport to be variable based on the screens' aspect ratio.
然后设置精灵,将其更改一个设定因子
Then to setup the sprite change it by a set factor
sprite.setSize(sprite.getWidth / PIX2M, sprite.getHeight / PIX2M);
其中PIX2M是一个静态字段,用于定义box2d中一米的多少像素
where PIX2M is a static field defining how many pixels are a meter in box2d
或者,您可以将子画面的尺寸显式设置为一个具有物理意义的值,并具有原始图像的长宽比(我个人喜好).因此,可以这样设置一个人的图像,例如100 x 500.
Alternatively you can set the dimensions of the sprite explicitly to a value which makes physical sense and with the aspect ratio of the original image(my personal preference) . So an image of a person which is 100 x 500 for example could be set like this.
sprite.setSize(.4f, 2f);
表示该人高2米,宽0.4米.同样,使用这种方法,您不需要PIX2M转换因子,并且您将永远知道您身体的确切大小.由于您将摄像机设置为特定数量的世界单位,在这种情况下为30,因此无论显示分辨率如何,子画面都将占据屏幕上相同的空间.
meaning the person is 2 meters high and .4 meters wide. Also with this method you don't need a PIX2M conversion factor and you will always know the exact size of your body. Since you set the camera to a specific number of world units, 30 in this case, the sprite will take up the same amount of room on the screen no matter the resolution of the display.
这篇关于身体在任何重力下缓慢下落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!