Box2d Body 跟随鼠标移动
Box2d Body to follow mouse movement
我正在尝试让 box2d body 跟随鼠标旋转。这是一张图片来阐明我的意思。
红色和蓝色圆圈是鼠标的当前点,body(对应颜色)moving/rotating跟随它。基本上矩形应该旋转,其一端指向鼠标指针所在的位置。
到目前为止,这是我的代码,
World world;
Body body, bullet;
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
@Override
public void create() {
world = new World(new Vector2(0, 0f), true);
debugRenderer = new Box2DDebugRenderer();
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(10, 10);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(2, 5);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
Gdx.input.setInputProcessor(this);
camera = new OrthographicCamera(200, 100 * (h / w));
camera.position.set(camera.viewportWidth / 2f, camera.viewportHeight / 2f, 0);
camera.update();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
camera.update();
debugRenderer.render(world, camera.combined);
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
int mouseX = screenX;
int mouseY = Gdx.graphics.getHeight() - screenY;
float rotateAngle = MathUtils.radiansToDegrees * MathUtils.atan2((float) mouseY - (float) body.getPosition().y, (float) mouseX - (float) body.getPosition().x);
body.setTransform(body.getPosition(), rotateAngle / 57.2957795f);
return true;
}
这是它现在的样子的动图。
如您所见,body 倾斜并且旋转不正确。我错过了什么?
歪斜看起来像是宽高比校正的问题,试试,
camera = OrthographicCamera(200, 100 * (w / h));
甚至,
camera = OrthographicCamera(200, 100);
要让方框的末端像图片中那样跟随鼠标只需要重新定义方框即可,
shape.setAsBox(5, 2, Vector2(offset, 0), 0);
我正在尝试让 box2d body 跟随鼠标旋转。这是一张图片来阐明我的意思。
红色和蓝色圆圈是鼠标的当前点,body(对应颜色)moving/rotating跟随它。基本上矩形应该旋转,其一端指向鼠标指针所在的位置。
到目前为止,这是我的代码,
World world;
Body body, bullet;
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
@Override
public void create() {
world = new World(new Vector2(0, 0f), true);
debugRenderer = new Box2DDebugRenderer();
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(10, 10);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(2, 5);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
Gdx.input.setInputProcessor(this);
camera = new OrthographicCamera(200, 100 * (h / w));
camera.position.set(camera.viewportWidth / 2f, camera.viewportHeight / 2f, 0);
camera.update();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
camera.update();
debugRenderer.render(world, camera.combined);
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
int mouseX = screenX;
int mouseY = Gdx.graphics.getHeight() - screenY;
float rotateAngle = MathUtils.radiansToDegrees * MathUtils.atan2((float) mouseY - (float) body.getPosition().y, (float) mouseX - (float) body.getPosition().x);
body.setTransform(body.getPosition(), rotateAngle / 57.2957795f);
return true;
}
这是它现在的样子的动图。
如您所见,body 倾斜并且旋转不正确。我错过了什么?
歪斜看起来像是宽高比校正的问题,试试,
camera = OrthographicCamera(200, 100 * (w / h));
甚至,
camera = OrthographicCamera(200, 100);
要让方框的末端像图片中那样跟随鼠标只需要重新定义方框即可,
shape.setAsBox(5, 2, Vector2(offset, 0), 0);