gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:16:23 24 4
gpt4 key购买 nike

如何使 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com