Converting map coords to screen coords

Started by
4 comments, last by szecs 12 years, 6 months ago
I've got a problem where I'm trying to get a character on screen in a top-down 2d game who always faces the mouse cursor. The engine I'm using uses a map, and all objects on the map provide their position relative to the map origin. I can use calls to get the mouse position in screen coordinates and I have a camera thats always centred in the middle of the screen.

I've currently got it working by getting the mouse coords (in screen space), getting the camera (which is provided in map coords for some reason, but since its always centered, I can just set it to half screen height/half screen width) and calculating the delta as (mouse pos - cam pos) and getting the angle from there.

The problem is, the game is designed with a drifting camera that floats slightly ahead of the player as they move forwards to allow them to see further ahead, and since the character turning angle is calculated from the centre of the screen, the character aim is off when the camera drifts ahead, since the character is no longer at the centre of the screen.

What I'm looking for is a way to convert the character in map coords into screen coords, so I can perform the calculation from there.

Any help would be appreciated!

Also, any questions, feel free to ask if I haven't explained something well.
Advertisement
Well, it's not very clear for me.
I suggest you to draw a small diagram in Paint, draw and mark everything you have there, how the map is placed, how objects are placed relative to the map, what distances/coordinates are you after, etc. Draw and mark what you know, and what you want to know. Maybe after doing so, you'll be able to solve the problem yourself, but it's sure that we will understand the problem better.

Just an example:
[attachment=5790:abra.JPG]
Well here is a quick and dirty diagram.

In this case, the map origin is in the centre of the game world, at 0,0. In map coords, the player is at, say, -10,-10 and the camera -7,-7.

Now the mouse cursor is what the player is always looking at, and it is it something like 400,565 since the screen resolution is 960x640 (I know it isn't exact, its just for example)

Currently, I am converting the camera to screen space simply by taking half-screen-width and half-screen-height to get the centre point of the screen and using this with the screen-space mouse cursor coordinates. As I said, since the character is not in the exact centre of the screen, this means the character facing is inaccurate.

What I need is a way to convert the map-coord character position (-10,-10 relative to the map origin) to the screen-space equivalent (for arguments sake, 320, 670).
I don't see how the coordinates of each things are given, especially the camera and the screen with the "drift" or whatever. Try do draw them, not just explain them in words.

Now, it seems that you have a constant distance between the screen center and the player's screen position. So you should be able to calculate the player's screen position using that distance and the current facing angle. What am I missing? Is the camera "zoomable"? Does the said distance depend on the speed of the player or other factors? How do you define or calculate that distance? Can't you just use it as I suggest now?
Nevermind, I removed the screen drift so the character is always dead-centre.

E: The character wasn't a constant distance, if the player started in the centre, then moved forward, the screen would drift forward in front of the player slightly so the player could see more space in front fo them. This also applied if the player turned, so it would drift to the left if the player was moving left, and so on.

Basically, the player and camera were positioned relative to the game map, the mouse coordinates were only in screen coords. Its a massive pain to draw this in MS paint and I can't install a decent drawing program on this machine.
I don't get it. You removed that feature... How do you apply that drift? Why can't you use that distance as I suggested in my previous post? Or the engine does everything for you?
Or I'm not clear?

How do you apply that drift transformation to the camera?

Post code.

This topic is closed to new replies.

Advertisement