java - Box2d setAngularVelocity 不适用于高速

标签 java eclipse libgdx box2d bulletphysics

我正在使用 Box2d 进行游戏,虽然我使用较大的常数来设置角速度,但我可以获得的最快速度是 3.86 秒转 1 圈。

我已经在以下线程中检查了我的源代码,一切都与我在此处和教程中的用户建议的内容相同:

setAngularVelocity rotates really slowly

但是我注意到以下 Unresolved 线程: http://www.reddit.com/r/libgdx/comments/1qr2m3/the_strangest_libgdxbox2d_behaviour/

并注意到这实际上可能是问题所在。这是我的处理方法

    public void dispose() {
    //Get Rid of Everything!
    Assets.Clear();
    GameEngine.Clear();
    BallMap.clear();
    PlayerMap.clear();
    shapeRenderer.dispose();
    debugRenderer.dispose();
    world.dispose();
    batch.dispose();
    font.dispose();
}

它们都在开始时重新初始化,如下所示:

    this.game       = game;
    this.cameraWidth = cameraWidth*pixelRatio;
    this.cameraHeight = cameraHeight*pixelRatio;

    batch           = new SpriteBatch();
    shapeRenderer   = new ShapeRenderer();
    stateTime       = 0F;
    Scores          = new Integer[]{0, 0};

    debugRenderer   = new Box2DDebugRenderer();
    world           = new World(new Vector2(0, 0), true);   //Create a world with no gravity
    GameEngine.setContactListener(world);

我使用以下代码浏览屏幕:

    public void create () {
    scene_menu = new MainMenuScreen(this, cameraWidth, cameraHeight);
    setScreen(scene_menu);
}

public void swtogame(){
    scene_menu.dispose();
    scene_game = new MatchScreen(this, cameraWidth, cameraHeight);
    setScreen(scene_game);
}

public void swtomenu(){
    scene_game.dispose();
    scene_menu = new MainMenuScreen(this, cameraWidth, cameraHeight);
    setScreen(scene_menu);
}

我初始化对象的方式:

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
}

我的处理方式正确吗?这是一个错误吗?有什么方法可以绕过它并正确使用 setAngularVelocity 吗?

最佳答案

因为您没有显示太多代码,所以我可以 100% 确定我是对的,但我认为您已经达到了每个时间步 2.0 单位的内置最大移动限制。这意味着在 60Hz 的典型帧速率下,每个时间步覆盖 2 个单位的物体以 120 m/s 或 432 km/h (270 mph) 的速度移动。不幸的是,似乎没有直接的方法可以在 Java 中更改此限制,因为此限制似乎是在 native C++ 库中定义的。

但我认为真正的问题是你的尺度错误。 Box2D 使用 MKS(米、千克和秒)。您可能使用像素而不是米。 Box2D的FAQ建议使用

objects [that are] between 0.1 - 10 meters

否则你可能会遇到奇怪的情况。

参见http://www.iforce2d.net/b2dtut/gotchas#speedlimithttps://code.google.com/p/box2d/wiki/FAQ

关于java - Box2d setAngularVelocity 不适用于高速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28720959/

相关文章:

java - 从对象列表中更改对象属性 - 每个对象都会更改属性

java - 我的 Eclipse 无法再运行(或调试)我的 JUnit 测试

java - Libgdx 用纹理重构一个类

java - 如何使用 Sprite 作为另一个 Sprite 的 anchor ?

Java 版本仍然显示 Java 1.7

java - 使用公钥的 sftp 无法正常工作

java - 本地主机上的服务器 Apache TomEE 无法启动

java - AlertDialog.Builder 不是语句

java - Eclipse 不在运行时将 Groovy 脚本编译成 java 类

android - 从另一个类调用 render 方法