直接在游戏中绘图,而不是在屏幕中

Drawing directly in the game, not in the screen

我正在玩游戏,我想在上面画一条线。我不能使用 Graphics.DrawLine,因为它使用 window/screen 坐标,而不是游戏坐标。

我想从游戏的位置 A 到游戏的位置 B 画一条线。如果我将这些坐标放在 DrawLine 中,它将采用 window/screen 的 X 和 Y 坐标,而不是来自游戏。

以下图中为例,我想画蓝线,但是使用DrawLine会画灰线。

即使点在屏幕上不可见,我也想绘制,如我在本例中所示。如果我在游戏场景中移动屏幕,由于 A 点和 B 点的坐标保持不变,因此直线保持静止。

有办法吗?

这里的重点是将世界坐标转换为屏幕坐标。例如,假设我想在世界地图上从点 x = 100, y = 600 到点 x = 700, y 600 画一条线,我屏幕的左边是 x = 300,它的底部是 y = 300在世界坐标中,那么绘图应该从屏幕坐标中的 x = -200, y = 300 到 x = 400, y = 300 开始,假设它的分辨率是 800x600,这将在屏幕中心完成绘制的线。

由于屏幕会根据世界场景移动,因此世界屏幕方法的代码可以是:

static int[] WorldToScreen(int worldX, int worldY, int worldX2, int worldY2, int screenLeft, int screenBottom)
        {
            int screenX = worldX - screenLeft;
            int screenY = worldY - screenBottom;

            int screenX2 = worldX2 - screenLeft;
            int screenY2 = worldY2 - screenBottom;

            return new int[] { screenX, screenY, screenX2, screenY2 };
        }

现在我们只需使用这些转换后的坐标并使用 GDI、DirectX 或任何您想要的方式在屏幕上绘制。

PS:屏幕(或相机)坐标通常与屏幕中心有关。我在这里使用边缘只是为了简化。