Java 是否将Box2D实体旋转到鼠标输入点?
如何使Box2D实体旋转,使其在用户单击鼠标时面向给定点 我正在尝试实现一个你可以想象为自上而下手电筒的机械装置 问题:Java 是否将Box2D实体旋转到鼠标输入点?,java,libgdx,box2d,lwjgl,Java,Libgdx,Box2d,Lwjgl,如何使Box2D实体旋转,使其在用户单击鼠标时面向给定点 我正在尝试实现一个你可以想象为自上而下手电筒的机械装置 问题: 我感觉我用PPM(每米像素数)不正确地缩放相机 这盏灯转不正确 不知道如何在两个位置之间切换 为此,在show()方法中,我使用以下方法将聚光灯附加到Box2D主体上: PolygonShape shape = new PolygonShape(); shape.setAsBox(5 / PPM, 5 / PPM); BodyDef bdef =
- 我感觉我用PPM(每米像素数)不正确地缩放相机
- 这盏灯转不正确
- 不知道如何在两个位置之间切换
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()方法中呈现如下内容:
我以这种方式处理输入:
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;
}
鼠标单击时,灯光会旋转,这意味着侦听器正在工作,但它不会旋转到正确的点。我认为这与我的数学不正确有关,从米到像素的缩放是错误的
有人能帮我解决这两个问题吗?预期行为如下所示:
单击鼠标时,主体和聚光灯应朝着鼠标单击的方向移动
我的完整代码可以在这里查看:我从来没有得到过这个问题的答案,但我在一些朋友的帮助下找到了答案 为了解决这个问题,我创建了一个类,它可以自动完成大量的数学运算
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()));
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()));