【问题标题】:Rotating a Box2D entity to mouse input point?将 Box2D 实体旋转到鼠标输入点?
【发布时间】:2014-12-28 07:25:18
【问题描述】:

如何使 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;
}

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

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

当鼠标点击时,身体和 ConeLight 的关联应该移动到面对鼠标点击的方向。

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

【问题讨论】:

    标签: java libgdx box2d lwjgl


    【解决方案1】:

    我从来没有得到答案,但在一些朋友的帮助下我想通了。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多