Getting accurate mouse coordinates

I’ve been using a variety of methods to try and capture mouse coordinates, but for some reason they tend to be “inaccurate” - as the mouse move farther away from the center of the screen, the coordinates reported are farther off from the actual ones – or at least, that’s what my tests seem to show.

Is

    newX = base.mouseWatcherNode.getMouseX()
    newY = base.mouseWatcherNode.getMouseY() 

the most accurate method of obtaining mouse coordinates (of the -1 to 1 sort)?

Yes, those functions should always return mouse coordinates precisely matching the current mouse position, in the range -1 … 1.

Note, however, that the coordinate system of aspect2d is not -1 … 1, but is usually wider (for instance, -1.33 … 1.33), so if you are comparing your mouse position to aspect2d coordinates, you will find it is increasingly wrong as you get further from the center.

You can use Panda’s relative coordinate spaces to convert from a DirectGui object’s coordinate system to render2d (the parent of the 2-d scene graph, and which has a coordinate system in the range -1 … 1). For instance:

top, left, bottom, right = myGuiObject.getBounds()
ll = render2d.getRelativePoint(myGuiObject, Point3(left, 0, bottom))
ur = render2d.getRelativePoint(myGuiObject, Point3(right, 0, top))

This will compute the lower left and upper right coordinates of myGuiObject, in the coordinate space of render2d (which will match your mouse’s position as returned by base.mouseWatcherNode).

David

That might explain it, then… thanks!

However, I’ve switched from using mouse coordinates to using the event ‘enter-pg0’, or enter-ButtonName, etc, which I found through turning on verbose.
It’s worked out a lot better for me - makes drag and drop/tooltips much easier.
I’m not sure why this event and others like it were not in the documentation (or if they were, I couldn’t find them, heh :stuck_out_tongue:)