setAngularVelocity旋转非常缓慢的节奏(setAngularVelocity rot

2019-10-21 22:01发布

我使用的基本libgdx Box2D的管理游戏的物理运算。 一切工作正常,除了旋转:即使我设置

anyobject.body.setAngularVelocity(someLargeConstant);

对象真的慢(几乎以相同的速度)旋转,不管是什么“someLargeConstant”的。 除了当我使用的参数小的数字,它可以旋转速度较慢。 因此,我认为我不知有我的世界对象,应设置一些小的值内最大恒定角速度。

(我还与线速度类似的问题之前,我通过调整个像素/米的规模解决了这个问题。因此,它不太可能的问题是一个比例的问题。)

我怎样才能使物体旋转得更快?

下面是我使用的代码:

private static World world = new World(new Vector2(0, 0), true);   //Create a world with no gravity

创建一个对象我调用另一个类

 public Object(World world, short category, short mask, float x, float y, float radius, Sprite image, 
        float maxSpeed, float frictionStrength, float linearDamping, float angularDamping, boolean movable,
        float elasticity, float mass){

    this.world = world; 
    this.category = category;
    this.mask = mask;
    // We set our body type
    this.bodyDef = new BodyDef();
    if(movable==true){bodyDef.type = BodyType.DynamicBody;}else{bodyDef.type = BodyType.StaticBody;}
    // Set body's starting position in the world
    bodyDef.position.set(x, y);
    bodyDef.linearDamping = linearDamping;
    bodyDef.angularDamping = angularDamping;
    // Create our body in the world using our body definition
    this.body = world.createBody(bodyDef);
    // Create a circle shape and set its radius
    CircleShape circle = new CircleShape();
    circle.setRadius(radius);
    // Create a fixture definition to apply our shape to
    fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = (float) (mass/(Math.PI*radius*radius)); 
    fixtureDef.friction = frictionStrength;
    fixtureDef.restitution = elasticity;
    fixtureDef.filter.categoryBits = category;
    fixtureDef.filter.maskBits = mask;
    // Create our fixture and attach it to the body
    this.fixture = body.createFixture(fixtureDef);
    // BodyDef and FixtureDef don't need disposing, but shapes do.
    circle.dispose();

    ... unrelated functions after that
    }

在这里,我只是尽量使其旋转快:

    tempBall.body.setAngularVelocity(20000);

Answer 1:

angularvilocity用于设置的旋转方向,当涉及到与使用它actionListener像键或鼠标利斯特,这里是使用的一个示例:

 case KeyEvent.VK_RIGHT:
              ball.setAngularVelocity(-20); // Directly set the angular velocity

case KeyEvent.VK_LEFT:
              ball.setAngularVelocity(20); // Directly set the angular velocity

就像你可以在这里看到的代码使球体旋转到KEY_RIGHT权压和左中KEY_LEFT压,并与它的参数,以增加或降低转速我可以玩和身边和它非常适合我,在这里是我body定义尝试应用相同的值,并且必须没有问题的工作:

private Body createObject(Shape shape, BodyType type, Vec2 position, float orientation, Sprite sprite) throws InvalidSpriteNameException {
           for(Sprite s:spriteList) {
            if(s.getName().equals(sprite.getName())) {
                throw new InvalidSpriteNameException(sprite.getName()+" already used.");
            }
           }
        Body body = null;
        FixtureDef fixDef = new FixtureDef();
        fixDef.shape = shape;
        fixDef.density = 0.1f;
        fixDef.isSensor = false;
        fixDef.restitution = 0.1f;

        BodyDef bodyDef = new BodyDef();
        bodyDef.type = type;

        bodyDef.angularDamping = 0.1f;
        bodyDef.linearDamping = 0.1f;

        bodyDef.fixedRotation = false;
        bodyDef.gravityScale = 1f;

        bodyDef.linearVelocity = new Vec2(0,0);
        bodyDef.angularVelocity = 0;
        bodyDef.position = new Vec2(position);
        bodyDef.angle = orientation;
        bodyDef.allowSleep = true;
        spriteList.add(sprite); // Save the sprite to the list (sprites must be serialiazed in the PhysicalWorld)
        bodyDef.userData = sprite; // Link the body and the sprite

        do {
            body = jBox2DWorld.createBody(bodyDef);
        } while(body== null); // Wait until the object is really created
        sprite.linkToBody(body); // Link the body to the sprite (this link is not serialiazed)
        body.createFixture(fixDef);
        return body;
    }


Answer 2:

我刚刚发现的问题,这是非常简单的。 我只是要在这里发布此为将来的Google:

对象竟是旋转正常,问题是在我的画法,我没有在我的batch.draw使用弧度之间的转换为度,并以弧度解释一切。 我知道,这样一个业余的错误! 非常感谢您的时间。



文章来源: setAngularVelocity rotates really slowly