java - 将 Box2D 实体旋转到鼠标输入点?

标签 java libgdx box2d lwjgl

如何使 Box2D 主体旋转,使其面向用户单击鼠标时给出的点?

我正在尝试实现一种机制,您可以将其想象为自上而下的手电筒。

问题:

  • 我觉得我用 PPM(每米像素)错误地缩放了相机
  • 灯无法正确转动
  • 不知道如何在位置之间补间

为此,在 show() 方法中,我使用以下方法将 ConeLight 连接到 Box2D 主体:

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(5 / PPM, 5 / PPM);

    BodyDef bdef = new BodyDef();
    bdef.position.set(160 / PPM, 200 / PPM);
    bdef.type = BodyType.DynamicBody;
    body = world.createBody(bdef);
    
    FixtureDef fdef = new FixtureDef();
    fdef.shape = shape;
    body.createFixture(fdef);
    
    rayHandler = new RayHandler(world);
    cone = new ConeLight
    (rayHandler, 40, Color.WHITE, 30, 160 / PPM, 200 / PPM, -90, 40);

然后,再次在 show() 方法中设置相机:

    b2dcam = new OrthographicCamera();
    b2dcam.setToOrtho(false, Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM);

我在 render() 方法中像这样渲染它:

    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
    world.step(1f/60f, 6, 2);
    b2dr.render(world, b2dcam.combined);
    rayHandler.setCombinedMatrix(b2dcam.combined);
    rayHandler.updateAndRender();

我正在以这种方式处理输入:

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    
    if(button == Buttons.LEFT){
        body.setTransform(body.getPosition(), (float) (Math.atan2( (body.getPosition().y - screenY),
                                                       (screenX - body.getPosition().x) ) ));
    }
    return false;
}

鼠标点击时灯光会旋转,这意味着监听器正在工作,但它没有旋转到正确的点。我认为这与我的数学不正确以及从米到像素的缩放错误有关。

有人可以帮助我解决这两个问题吗?预期行为如下所示:

The setup I wish to use

The intended behavior of the moving flash light

单击鼠标时,主体以及关联的 ConeLight 应移动以面向鼠标单击的方向。

我的完整代码可以在这里查看:https://gist.githubusercontent.com/Elsealabs/1afaa812aafb56ecd3c2/raw/5d0959df795516c89fb7e6ab81aecc01dc8cd441/gistfile1.txt

最佳答案

我从来没有得到这个问题的答案,但在一些 friend 的帮助下我找到了答案。

为了解决这个问题,我创建了一个自动执行大量数学运算的类。

public class Rot2D {
  public static Rot2D fromDegrees(double angle) {
     return fromRadians(Math.toRadians(angle));
  }

  public static Rot2D fromRadians(double angle) {
     return new Rot2D(Math.cos(angle), Math.sin(angle));
  }

  public static Rot2D fromVector(double dx, double dy) {
     float length = (float) Math.sqrt(dx * dx + dy * dy);
     return new Rot2D(dx / length, dy / length);
  }

  public double cos, sin;

  private Rot2D(double cos, double sin) {
     this.cos = cos;
     this.sin = sin;
  }

  public Rot2D load(Rot2D that) {
     this.cos = that.cos;
     this.sin = that.sin;

     return this;
  }

  public Rot2D copy() {
     return new Rot2D(cos, sin);
  }

  public Rot2D rotate(Rot2D that) {
     double cos = (this.cos * that.cos) - (this.sin * that.sin);
     double sin = (this.cos * that.sin) + (this.sin * that.cos);

     this.cos = cos;
     this.sin = sin;

     return this;
  }

  public static double cross(Rot2D a, Rot2D b) {
     return (a.cos * b.sin) - (a.sin * b.cos);
  }
}

在我的游戏代码中,我使用以下代码来实现运动:

    if (Gdx.input.isTouched())
    {
        Vector3 tmp = camera.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
        touch_target = new Vector2(tmp.x, tmp.y);
    }

    touch_angleWant = Rot2D.fromVector(
        touch_target.x - entity_player.getBody().getPosition().x,
        touch_target.y - entity_player.getBody().getPosition().y
    );

    double cross1 = Rot2D.cross(touch_angleCur, touch_angleWant);

    if (cross1 > 0.0)
        touch_angleCur.rotate(Rot2D.fromDegrees(5.0));
    else
        touch_angleCur.rotate(Rot2D.fromDegrees(-5.0));

    double cross2 = Rot2D.cross(touch_angleCur, touch_angleWant);

    if (Math.signum(cross1) != Math.signum(cross2))
        touch_angleCur.load(touch_angleWant);

     entity_player.getBody().setTransform(entity_player.getBody().getPosition(), (float) (touch_angleCur.getAngle()));

关于java - 将 Box2D 实体旋转到鼠标输入点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27674463/

相关文章:

java - Eclipse LibGdx GPGS

java - libGDX 3D 阴影伪像

java - (Triangle 类)设计一个名为 Triangle 的类,它扩展了 GeometricObject

java - grep 命令不适用于 git blame 文件和 git log 以进行修订

android - (Libgdx) BitmapFont 颜色和 Opengl 2

c++ - 堆栈帧中重复的内联构造函数导致 "pure virtual method called"?

processing - 在同一处理草图中使用 Box2D 和视频的问题

c++ - Box2D 和过剩。帧率问题

java - 在 GraphQL (Apollo) 中转换 InputType 对象

java - 当我在java中单击它们时,我想显示相同的类图 block