Android LibGDX PhysicsBox2D,如果我设置了线速度,物理将无法正常工作
我的世界是Android LibGDX PhysicsBox2D,如果我设置了线速度,物理将无法正常工作,android,box2d,physics,libgdx,collision,Android,Box2d,Physics,Libgdx,Collision,我的世界是world=newworld(newvector2(0,0),true)//没有重力 我在屏幕的左、右、上、下都有墙,所以元素不会飞离屏幕 我的渲染类中有以下代码: Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); camera.update(); Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer(); batch.
world=newworld(newvector2(0,0),true)代码>//没有重力
我在屏幕的左、右、上、下都有墙,所以元素不会飞离屏幕
我的渲染类中有以下代码:
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture, 0, 0);
for (Element element : elements) {
element.body.setUserData(element);
element.rect.x = element.body.getPosition().x-16;//16 = half the width of element
element.rect.y = element.body.getPosition().y-16;
if (element.goodOrEvil == 0) {
batch.draw(happyImage, element.rect.x, element.rect.y);
} else if (element.goodOrEvil == 1) {
batch.draw(sadImage, element.rect.x, element.rect.y);
}
} //i draw the elements that i have to fix the bodies to
batch.end();
debugRenderer.render(world, camera.combined);
Iterator<Element> iter = elements.iterator();
//next i set the accelerometer to move the elements, so that i could control them, i need to move all the elements in the same direction, in the same time
while (iter.hasNext()) {
Element element = iter.next();
element.body.setLinearVelocity(Gdx.input.getAccelerometerY() * 50, -Gdx.input.getAccelerometerX() * 50);
}
if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
spawnElement(); // this creates a new element
world.step(1 / 45f, 6, 2);
现在,如果我设置重力存在world=newworld(newvector2(-2,-20),则为true)代码>并注释掉这一行:element.body.setLinearVelocity(Gdx.input.getAccelerometry()*50,-Gdx.input.getAccelerometrx()*50)物理效果很好,但我无法控制我的物体。如果我让代码保持原样(没有重力和那条线),我可以控制我的对象,但是当一个元素撞到墙上,另一个元素出现时,它们重叠,然后才开始分割。我需要它们相互撞击,保持紧密,但一点也不重叠。关于如何做到这一点有什么想法吗?而不是使用元素.body.setLinearVelocity(Gdx.input.getAccelerometry()*50,-Gdx.input.getAccelerometrx()*50)代码>
我用过这个:
Vector2 gravity = new Vector2(Gdx.input.getAccelerometerY() * 50, -Gdx.input.getAccelerometerX() * 50);
world.setGravity(gravity);
然后它的回答就像我设置静态重力一样,但是我在每次渲染时都会用加速度计的值来改变重力
Vector2 gravity = new Vector2(Gdx.input.getAccelerometerY() * 50, -Gdx.input.getAccelerometerX() * 50);
world.setGravity(gravity);