Print X, Y on Mouse Click

Hi Guys,

I have a 2D display:

        self.grid = self.grid.create()
        self.grid.reparentTo(render)
        self.grid.setAlphaScale(0.1)
        self.grid.setScale(1.00, 1.00, 1.00)
        self.grid.setPos(0, 0, 0)

        self.lens = OrthographicLens()
        ratio = base.getAspectRatio()
        self.lens.setFilmSize(10000.0, 10000.0/ratio)
        #self.lens.setNearFar(-1000, 1000)
        base.cam.node().setLens(self.lens)

        base.cam.setPos(0, -90, 0)
        base.cam.lookAt(0,0,0)

base.mouseWatcherNode.getMouse()

  • Seems to get a percentage of where I’m at, but I need the spacial co-ordinates.

Thanks.

On a more post related note:

Do you want the x and y coordinates on the mouse on the screen?
Then indeed base.mouseWatcherNode.getMouse() returns the fraction of the screen at which the mouse is (0.5,0.5 as the center).

mouse_x = base.mouseWatcherNode.getMouseX()*base.win.getXSize()
mouse_y = base.mouseWatcherNode.getMouseY()*base.win.getYSize()

Or do you want the x and y coordinates of the position inside the game world where the mouse is pointing?
Then this might help:
panda3d.org/manual/index.php … 3D_Objects

Yep, I’m looking for the coordinates in the game world. Slowly trying to figure this example out. Thanks. I can’t seem so far to figure where I read the coordinates in the world though.

Do you mean that, considering the camera’s view-plane to be a plane located in the world, you want the coordinates of a point on that plane based on a given mouse coordinate? If so, surely that would be a 3D point, not a 2D point?

If that is what you want, and there’s nothing in the API that provides it, I think that it should largely be a matter of trigonometry. I think that the following should be more or less correct:

  • Let the input mouse coordinates be “mouseX” and “mouseY”, specified both in the range [-1, 1].

  • Find the camera’s position, which we’ll call “posCam”, the world-space depth of the near-plane, “nearDepth”, and the world-space width-and-height size of the camera’s film, “filmSize”. I’m not sure that the results of the Lens class’s “getNear” and “getFilmSize” will get quite the values desired here; if not, I think that you should be able to use the “extrude” method, passing in a 2D point of (1, 1) to specify a corner of the near-plane, and then examine the result returned in the “near_point” parameter.

  • Let the output 3D vector be “result”.

  • Find the camera’s right-, up- and forward- vectors (using something along the lines of “camera.getQuat().getForward()” and the similar “getRight()” and “getUp()” methods); we’ll here call them “camForward” and “camRight”.

  • result = camForwardnearDepth + mouseXfilmSize.xcamRight + mouseYfilmSize.y*camUp

If you just want the coordinates relative to the plane, that would seem to just be a multiple of the mouse coordinates that you already have.

If that’s a plane in the 3d scene you want the mouse pointer to collide with then there’s this great snippet by rdb, that I use all the time:

Thanks everyone,

I should have mentioned, I have a 2D space:

        
self.lens = OrthographicLens()
ratio = base.getAspectRatio()
self.lens.setFilmSize(10000.0, 10000.0/ratio)
base.cam.node().setLens(self.lens)

base.cam.setPos(0, -90, 0)
base.cam.lookAt(0,0,0)

In that case, is the point not just:

worldX = (mouseX*0.5 + 0.5)*filmSize.x

worldZ = (mouseY*0.5 + 0.5)*filmSize.y

(I’ve used worldZ above because you’ve used your world-space y-axis as the depth axis.)

Regarding the mutiplication and addition of 0.5 above, I checked, and in my version of Panda at least (1.8.0, I think) mouse coordinates range from (-1, -1) at the bottom-left of the screen to (1, 1) at the top-right, if I recall correctly. Hence, in order for the mouse to map from (0, 0) at the bottom-left to (filmSize.x, filmSize.y) at the top-right we halve the range of the mouse coordinates (so that they cover a range of 1 instead of 2) and then shift them so that (0, 0) is at the bottom-left rather than the centre.