Getting the actual mouse position on screen

Hi i’m really new to python and panda3d but i’ve playing around with it for a few months. i’m actually trying to make an Rts game for my school project using panda3d and python but i’ve run into some problems with attaching objects to the mouse coordinates on the screen.

RTS games let you drag a selection box over units in order to make a selection. What i initially tried was to import a draw.py class that enabled me to draw 2d lines and circles on the screen. So in order to create a line i had to specify a start and end point in 3d. I inserted the mouse x and y values into the x and y values for the start point (the start point is crated everytime i do a left mouse click). However due to some function that either scales or contrains the mouse coordinates so that they do not go beyond -1 or 1, the start point is always created somewhere near the origin.

It seems like the mouse coordinates uses a different scale from the coordinates of the 3d objects on the screen. Can someone please try to help me :frowning: i’m supposed to do the programming for the game in my team and i can’t let them down. I desperately need help :frowning:

The mouse coordinates onscreen range from -1 to 1 in both dimensions. This is the same range as render2d, which is the 2-d screen layer, so if you attach your 2-d drawings to render2d they will exactly match your mouse coordinates.

The 3-d scene represented by render has little relation to the onscreen coordinates, of course. It can’t, because it is 3-d. A single point on the 2-d plane corresponds to an infinite number of points in the 3-d space, all of which map to the same point onscreen.

It is possible, however, to convert from 2-d space to 3-d space. There are lots of approaches, but the most common is “picking”, that is, shooting a collision ray into the scene along the line corresponding to your selected pixel, and seeing what object it hits. There are lots of examples of this in the manual, in the forums, and in the sample programs distributed with Panda.

David

here there is a post with some working stuff to study with, close to what you need

Also, consider this alternative to collisions:
discourse.panda3d.org/viewtopic.php?t=5409
Muuuch faster and easier. The only downside is that you are picking on a plane in space, instead of on geometry - but for a lot of cases that is sufficient.

Hi guys. Im really grateful for your help. I’ve managed to make my stuff work. Thanks alot and i apologise for the late reply. *bows respectfully.