Java 重力和错误的大小
box2d出现问题。我想增加已经存在的游戏物理,但遇到了问题。首先,我通过计算绘制的游戏世界Java 重力和错误的大小,java,android,libgdx,box2d,Java,Android,Libgdx,Box2d,box2d出现问题。我想增加已经存在的游戏物理,但遇到了问题。首先,我通过计算绘制的游戏世界 public Hero(float x, float y, int width, int height) { this.width = width; this.height = height; position = new Vector2(x, y); velocity = new Vector2(0, 0); acceleration = new Vector
public Hero(float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
velocity = new Vector2(0, 0);
acceleration = new Vector2(0, -420);
}
public void update(float delta){
velocity.mulAdd(acceleration, delta);
if(velocity.y < -200){
velocity.y = -200;
}
position.mulAdd(velocity,delta);
}
public void onTap(){
velocity.y = 140;
}
它的box2d英雄构造函数
public Box2Dhero(World world, float x, float y, int width, int height ) {
bodyDef = new BodyDef();
bodyDef.position.set(x,y);
bodyDef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bodyDef);
box = new PolygonShape();
box.setAsBox(width,height);
fixtureDef = new FixtureDef();
fixtureDef.shape = box;
body.createFixture(fixtureDef);
}
我的游戏世界大小
float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
float gameHeight = 385;
float gameWidth = screenWidth / (screenHeight / gameHeight);
你们观察到的盒子是以中心为基础的,单位长度在各个方向上都是正确的
它会影响您的比较,因为box2d的长方体和您的长方体的原点不匹配。这种影响应该相对较小
你将y方向的速度限制在-200。作出这样的假设可能不是进行比较的好主意。找到一个关于box2d做什么的资源可能是一个好主意。
大多数物理引擎执行统一的时间步进。Box2D就是其中之一。您可以阅读有关统一时间步进的内容。
可能还有更多的差异/优化,在我看来,查找源代码并进行比较是最有效的解决方案
祝你好运 考虑命名你的单位。加速度或重力为420,看起来你使用了错误的单位。什么?
float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
float gameHeight = 385;
float gameWidth = screenWidth / (screenHeight / gameHeight);