2

I'm playing a game and I want to draw a line on it. I can't use Graphics.DrawLine because it takes the window/screen coordinates, not the game coordinates.

I want to draw a line from game's pos A to games's pos B. If I put those coordinates in DrawLine, it will take X's and Y's coordinates of the window/screen, not from the game.

In the image below as a example, I want to draw the blue line, but using DrawLine it will draw the grey line.

enter image description here

I want to draw even if the points were not visible in the screen, as I showed in this example. If I move the screen through the game's scenario, the line keeps static since the coordinates of the points A and B keep the same.

Is there a way to do it?

9
  • Is the screen area relation to game area always the same? Does it stay the same during the game? Commented May 13, 2019 at 14:20
  • @A.A. Yes. Always the same resolution. e.g. game area = 1920*1080 and screen area = 1366*768. No matter if I change monitor resolution, game area will resize too. But even so, I don't think if the screen area was dynamic, it would not change anythig, because the drawning is based only in the game area. But the screen area moves through the game area as the user moves the mouse. Commented May 13, 2019 at 14:25
  • The coordinates will not be the issue. If you know them and maybe a scale you can transform the graphics object. But will those pixel stay (or go) as you want them to?? Commented May 13, 2019 at 14:30
  • 2
    @EduardoM No, there is no such API and generally requires some interesting code to hijack game's drawing (can't help you here - beyond my skill level). Based on the way you've phrased the question most likely you don't yet have amount of information and skill needed to hack DX games to draw extra UI elements. I'd recommend finding corresponding community that plays and hacks game in question to see if there is already helper libraries to do so. Commented May 13, 2019 at 18:00
  • 1
    @SimonMourier, I've already found the solution. The point here is to do a world to screen calculation in order to convert world coordinates to screen coordinates. I can do it with GDI despite hooking DirectX is actually much more efficient. I'll answer this question soon. Commented Feb 10, 2020 at 5:58

1 Answer 1

1

The point here is to convert the world coordinates into screen coordinates. For example, suppose I want to draw a line from point x = 100, y = 600 to point x = 700, y 600 in the world map and the left of my screen is at x = 300 and it's bottom is at y = 300 in world coordinates, then the drawing should start from x = -200, y = 300 to x = 400, y = 300 in screen coordinates which would finish the drawed line at the center of the screen, assuming it's resolution is 800x600.

Since the screen moves in relation to the world scenario, the code for the world to screen method could be:

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 }; } 

Now we just take these converted coordinates and draw on the screen using GDI, DirectX or whatever you want.

PS: Screen (or camera) coordinates are usually related to the center of the screen. I used the edges here just for simplification.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.