[FIXED] Mouse Coords in a tiled world.

Hey guys. I need some help with my platformer game, you see- I cannot figure out at all how to find where the mouse is on the screen. You may be saying:

Well, just add them mouse coords and camera coords, then it will work.

but no. It doesnt. I have no clue why! Let me keep in mind, it works if I use the player’s x,y I just cant add the mouse factor into that.

Since I probally havent given enough info on the code itself, you can generalize, but this is what ive tried so far.

WORKS.

actionPos =  new Point(x,y);

DOESNT WORK. WELL, SORT OF

actionPos =  new Point(mouse.x+camera.x,mouse.y+camera.y); //camera is just the players x and y with half the screen subtracted

Any suggestions? Now here is the silly thing. It works, but the action position races ahead of the cursor. Basically at 0,0 on the screen, where camera.x, camera.y is, it is 100% accurate, but 2 pixels away, it may be 4-6 pixels away, at about half the screen its a quarter screen away from the cursor, etc.

I hope you guys can help. Ill give any more stuff on the issue if its needed! This is urgent!

Are you doing any sort of scaling when you render that you haven’t accounted for when calculating your mouse/world position?

IF you are scaling your tiles you will need to dived the x or y position by the scale amount.

Example if I am scaling by 32 I would take x/32 or y/32

my program has an issue like that when the window is different size than the view port, the drawing is scaled up and down nicely, but the mouse co-ords are still relative to the physical screen resolution.

Just want to point out, that statement is completely wrong. You cannot have a window that is less than 1920x1080 pixels still take up 1920x1080 pixels. If a window is 800x600, how the hell could you still have 1920x1080 pixels within the window? Its physically impossible.

Maybe he means in-game units?

Try calling glViewport with the window size.

You might need to be doing x-camera.x,y-camera.y instead of addition.

You mean it doesn’t scale the objects in game, correct? I would highly advise against this. If someone plays in a different screen ratio, all your objects are going to look distorted. Minecraft and Terraria handle resizing the correct way by scaling, I have no idea why that would be a bad thing.

Ah I see what you mean. That doesn’t happen with Minecraft or Terraria, but no matter, I won’t derail your thread anymore.