【问题标题】:Gravity and wrong size重力和错误的大小
【发布时间】:2015-02-18 23:05:38
【问题描述】:

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 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;
}

英雄的身体从标准设置中掉下来,但是我添加到测试中的身体盒子表现得很奇怪。

问题1.为什么box2d的box比hero多用这个设置,但是当我除以2的时候,就变成和hero的贴图大小差不多了?可能会关联这样的效果,使身体以所有方向的中心为基础绘制 2. 为什么重力-420的世界中的身体以相同的速度连续下降,但没有我的英雄那么多。如何达到类似的效果呢?

    hero = new Hero(30, midPointY, 18, 21);
    hero1 = new Box2Dhero(world, 90, midPointY, 18, 21);

它的 box2d hero 构造函数

  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);

【问题讨论】:

  • 考虑为您的单位命名。加速度或重力为 420 看起来您使用了错误的单位。 420什么?

标签: java android libgdx box2d


【解决方案1】:
  1. 您对盒子基于所有方向单位长度的中心的观察是正确的。

    它应该会影响你的比较,因为 box2d 的盒子和你的盒子的起源不匹配。这种影响应该比较小。

    • 您将 y 方向的速度限制为 -200。做出这样的假设可能不是一个比较的好主意。查找有关 box2d 功能的资源将是一个好主意(可能是来源)。
    • 大多数物理引擎执行统一的时间步长为deterministic。 Box2D 就是其中之一。您可以阅读有关统一时间步进的信息here

可能会有更多差异/优化,在我看来,查找源代码并比较对您来说是最有效的解决方案。

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-09
    • 2015-11-22
    • 1970-01-01
    • 2013-01-25
    • 2014-07-10
    • 2014-01-20
    • 2012-12-13
    • 1970-01-01
    相关资源
    最近更新 更多