我使用OrthographicCamera和unproject功能将屏幕点转换为世界点。
一切正常,但有时#unproject方法会返回一些差不多好/期望值,但差不多,所以你会看到它作为屏幕/世界跳跃X像素/单位左右。
//screenX, screenY -> mouse position when wheel is scrolled
(...) <- calculate new world size because of zooming and set it up in viewport object
//calculate difference in world two points
//where mouse was before zooming and where mouse is after it,
//related to worldCoords, and apply it to camera:
Vector2 before = viewport.unproject(new Vector2(screenX, screenY));
camera.viewportWidth = viewport.getWorldWidth();
camera.viewportHeight = viewport.getWorldHeight();
camera.update();
Vector2 after = viewport.unproject(new Vector2(screenX, screenY));
camera.position.x -= after.x - before.x;
camera.position.y -= after.y - before.y;
这段代码只是将重点放在&#34;指向世界&#34;功能 - 缩放
编辑/解决方案? :
OrthographicCamera类中的方法更新:
@Override
public void update (boolean updateFrustum) {
projection.setToOrtho(zoom * -viewportWidth / 2, zoom * (viewportWidth / 2), zoom * -(viewportHeight / 2), zoom
* viewportHeight / 2, near, far);
view.setToLookAt(position, tmp.set(position).add(direction), up);
combined.set(projection);
Matrix4.mul(combined.val, view.val);
if (updateFrustum) {
invProjectionView.set(combined);
Matrix4.inv(invProjectionView.val);
frustum.update(invProjectionView);
}
}
看起来这条线是有罪的:
view.setToLookAt(position, tmp.set(position).add(direction), up);
您可以复制此方法的代码并创建自己的代码,不带它。我想这种方法只是小马车,很难找到。现在别人不得不深入了解setToLookAt中的真正原因。