Box2d setAngularVelocity 不适用于高速
Box2d setAngularVelocity do not work for high speeds
我正在使用 Box2d 玩游戏,虽然我使用大常量设置 angular 速度,但我能获得的最快速度是 3.86 秒转一圈。
我已经在以下线程中检查了我的源代码,一切都与我在此处和教程中的用户建议的相同:
但是我注意到以下未解决的线程:
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 英里/小时)的速度移动。不幸的是,在 Java 中似乎没有直接的方法来更改此限制,因为此限制似乎是在本机 C++ 库中定义的。
但我认为真正的问题是你的比例尺不对。 Box2D 使用 MKS(米、千克和秒)。您可能使用了像素而不是米。 Box2D的FAQ建议使用
objects [that are] between 0.1 - 10 meters
否则你会遇到奇怪的情况。
见http://www.iforce2d.net/b2dtut/gotchas#speedlimit
和 https://code.google.com/p/box2d/wiki/FAQ
我刚刚发现了问题,而且很简单。我只是要 post 在这里为未来的 googlers:
对象实际上是正确旋转的,问题出在我的绘图方法上,我没有在我的 batch.draw 中使用弧度与度数之间的转换,它以弧度解释所有内容。我知道这样一个业余错误!非常感谢您的宝贵时间。
我正在使用 Box2d 玩游戏,虽然我使用大常量设置 angular 速度,但我能获得的最快速度是 3.86 秒转一圈。
我已经在以下线程中检查了我的源代码,一切都与我在此处和教程中的用户建议的相同:
但是我注意到以下未解决的线程: 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 英里/小时)的速度移动。不幸的是,在 Java 中似乎没有直接的方法来更改此限制,因为此限制似乎是在本机 C++ 库中定义的。
但我认为真正的问题是你的比例尺不对。 Box2D 使用 MKS(米、千克和秒)。您可能使用了像素而不是米。 Box2D的FAQ建议使用
objects [that are] between 0.1 - 10 meters
否则你会遇到奇怪的情况。
见http://www.iforce2d.net/b2dtut/gotchas#speedlimit 和 https://code.google.com/p/box2d/wiki/FAQ
我刚刚发现了问题,而且很简单。我只是要 post 在这里为未来的 googlers:
对象实际上是正确旋转的,问题出在我的绘图方法上,我没有在我的 batch.draw 中使用弧度与度数之间的转换,它以弧度解释所有内容。我知道这样一个业余错误!非常感谢您的宝贵时间。