Box2d setAngularVelocity不适用于高速

时间:2015-02-25 14:01:50

标签: java eclipse libgdx box2d bulletphysics

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

我在下面的帖子中检查了我的源代码,所有内容与我在这里和教程中的用户建议的内容相同:

setAngularVelocity rotates really slowly

然而,我注意到以下未解决的线程: 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?

2 个答案:

答案 0 :(得分:1)

因为您还没有显示太多代码,我可以 100%确定我是对的,但我认为您正在打造内置代码每个时间步长最大移动限制为2.0个单位。这意味着在60Hz的典型帧速率下,每时间步长覆盖2个单位的物体以120米/秒或432千米/小时(270英里/小时)的速度移动。不幸的是,似乎没有直接的方法来改变Java中的这个限制,因为这个限制似乎是在本机C ++库中定义的。

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

  

对象[在] 0.1到10米之间

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

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

答案 1 :(得分:0)

我刚刚发现问题,而且非常简单。我只想在这里发布这个未来的googlers:

对象实际上正在旋转,问题出在我的绘图方法中,我没有在我的batch.draw中使用弧度到度数之间的转换,并且它以弧度解释所有内容。我知道这样的业余错误!非常感谢你的时间。