getting level map coords with mouse click

Hello,

I am currently stumped with an issue that I have searched high and low for a solution to, but with no luck.
I have a top down map in java 2d which is larger than the view port (a JPanel extending Canvas). The map is 1920x, 1152y and my JPanel viewport is 1280x, 800y.
What I want to be able to do is click on the screen and get the world map x,y coords and not the screens x,y coords.

Currently, its fine until the viewport moves beyond the 1280x800 area of the world map, so that when I click the screen, instead of it returning the world map coords, say 1900x, 900y, its just returning the current x,y position of the viewport.

I’m sure there’s a solution out there, but I’m reasonably new to games coding, so any help would be most gratefully received!
Thanks

Hi rainey,

A click will always return the position in screen coordinates. You need to add your viewport’s position to the click coordinates. So for example, if your viewport is at (100, 150) in world coordinates, and your click is at (500, 800) in screen coordinates, then the position of the click in world coordinates is (500 + 100, 800 + 150), hence (600, 950).

Does that make sense?

nerb.

That makes perfect sense!

Now my next question is, how do I keep track of where my screen is at?

hold on I have some code for this shizzle.


PointerInfo a = MouseInfo.getPointerInfo();//two imports are needed here
		Point b = a.getLocation();
		mx = (int) b.getX() - frame.getX();//frame is your Jframe;
		my = (int) b.getY() - frame.getY();
		if (mx < 0) {//all of this is to stop out of bounds errors.
			mx = 0;
		}
		if (my < 0) {
			my = 0;
		}
		if (mx > width) {
			mx = 0;
		}
		if (my > height) {
			my = 0;
		}


Thats for use with JFrames if you are using an applet you will need to do this.


PointerInfo a = MouseInfo.getPointerInfo();
		Point b = a.getLocation();
		Point c = this.getLocationOnScreen();//for getting the applets position.
		mx = (int) ((int) b.getX() -c.getX());//it returns a double so you need to cast it to an int.
		my = (int) ((int) b.getY() -c.getY());
		if (mx < 0) {//checks
			mx = 0;
		}
		if (my < 0) {
			my = 0;
		}
		if (mx > width) {
			mx = 0;
		}
		if (my > height) {
			my = 0;
		}

With a getter, just put in the screen class:

public int getXOffset() {
      return xOffset; //This is your X from the screen
}

This is only for the x, as you already can see :stuck_out_tongue:
-RoseSlayer

From that comment, it sounds like you are moving the screen around somehow. I guess you are using an x & y position/offset variable of some sorts? That’s the variable that you want to use to keep track of your screen.

So you’re going to want a reference to the object that holds the viewport’s position variables. If that is your JPanel, then make sure whatever is handling the mouse clicks has a reference to the panel, either directly or indirectly. Then you can grab the x/y offset variables themselves, or use a getter method as roseslayer has pointed out.

Fantastic guys, thanks for all your help!

Much appreciated.
Rainey