如何使Box2D机身旋转,使其面向用户点击鼠标时给定的点?
我正在尝试实施一种可以将其视为自上而下手电筒的机制。
问题:
为此,在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关联的应移动到面向鼠标单击的方向。
答案 0 :(得分:0)
我从来没有得到过这方面的答案,但我在一些朋友的帮助下想出来了。
为了解决这个问题,我创建了一个自动完成大量数学运算的类。
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()));