简体   繁体   English

LibGDX PhysicsBox2D,如果我设置线速度,则物理无法正常工作

[英]LibGDX PhysicsBox2D, if i set linear velocity, physics does not work correctly

my world is world = new World(new Vector2(0, 0), true); 我的世界是world = new World(new Vector2(0, 0), true); //there is no gravity and i have walls in the left, right, top bottom of the screen, so elements do not fly off the display. //没有重力,并且我在屏幕的左,右,上底部都有墙,因此元素不会从显示屏上飞走。

I have the following code in my render class: 我的渲染类中有以下代码:

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);

Here is my spawnElement() class: 这是我的spawnElement()类:

private void spawnElement() {
    Rectangle elementRect = new Rectangle();
    elementRect.x = MathUtils.random(0, 800 - 32);
    elementRect.y = 400;
    elementRect.width = 32;
    elementRect.height = 32;
    Element element = new Element(elementRect, (int) elementRect.x % 2);
    elements.add(element);
    // First we create a body definition
    BodyDef bodyDef = new BodyDef();
    // We set our body to dynamic, for something like ground which doesnt
    // move we would set it to StaticBody
    bodyDef.type = BodyType.DynamicBody;
    // Set our body's starting position in the world
    bodyDef.position.set(elementRect.x, elementRect.y);

    // Create our body in the world using our body definition
    body = world.createBody(bodyDef);

    // Create a circle shape and set its radius to 6
    CircleShape circle = new CircleShape();
    circle.setRadius(6f);

    // Create a fixture definition to apply our shape to
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = 0.5f;
    fixtureDef.friction = 0.4f;
    fixtureDef.restitution = 0.6f; // Make it bounce a little bit

    // Create our fixture and attach it to the body
    Fixture fixture = body.createFixture(fixtureDef);
    element.body = body;

    lastDropTime = TimeUtils.nanoTime();
}

Now if i set gravity to exist world = new World(new Vector2(-2, -20), true); 现在如果我将引力设置为存在world = new World(new Vector2(-2, -20), true); and comment out this line: element.body.setLinearVelocity(Gdx.input.getAccelerometerY() * 50, -Gdx.input.getAccelerometerX() * 50); 并注释掉以下行: element.body.setLinearVelocity(Gdx.input.getAccelerometerY() * 50, -Gdx.input.getAccelerometerX() * 50); the physics works great, but i cannot control my objects. 物理效果很好,但我无法控制我的对象。 If i leave the code as it is (no gravity and that line in), i can control my objects, but when an element hits the wall, and another element comes, they overlap, and only then start to divide. 如果我按原样保留代码(没有重力,也没有插入那条线),则我可以控制我的对象,但是当一个元素碰到墙而另一个元素出现时,它们就会重叠,然后才开始分裂。 I need them to hit one another, and remain close, but not overlap at all. 我需要他们互相撞,并保持亲密,但一点也不重叠。 Any ideea on how this could be made? 关于如何做到这一点的任何想法?

instead of using element.body.setLinearVelocity(Gdx.input.getAccelerometerY() * 50, -Gdx.input.getAccelerometerX() * 50); 而不是使用element.body.setLinearVelocity(Gdx.input.getAccelerometerY() * 50, -Gdx.input.getAccelerometerX() * 50); i have used this: 我用了这个:

Vector2 gravity = new Vector2(Gdx.input.getAccelerometerY() * 50, -Gdx.input.getAccelerometerX() * 50);
world.setGravity(gravity);

And then it answers just like when i set a static gravity, but i change the gravity at every render, with values from my accelerometer 然后它的响应就像我设置静态重力时一样,但是我会使用加速度计中的值来更改每个渲染的重力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM