2d to 3d projection

Hi,

I don’t know if this has been asked before, but I can’t find specifics on how to convert the mouse screen coordinate into 3d coordinate.

the mouse is normally 2d. maybe you want to map my onto a button, but therefor mx and mz should move relative to my. if not you would move for ages your mouse.
(mx = mouse X and mz = mouse Y)

mousePOS = Vec3(mx,my,mz)

or you want to get the mouse on a mesh, here a example which im using in my game…

self.dragging = False
        self.isACTIVE= 0
        base.disableMouse() 
        self.myTraverser = CollisionTraverser()
        self.myHandler     = CollisionHandlerQueue()
        pickerNode=CollisionNode('mouseRay')
        pickerNP=camera.attachNewNode(pickerNode)
        pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())
        self.pickerRay=CollisionRay()
        pickerNode.addSolid(self.pickerRay)
        self.myTraverser.addCollider(pickerNP, self.myHandler)
        self.DISPLAYS = render.attachNewNode("DISPLAYS")
        self.GUI = render.attachNewNode("GUI")
        self.renderLIST = [self.DISPLAYS,self.GUI]
        taskMgr.add(self.mouseTask,"mouseTASK")
        taskMgr.add(self.test,"testTASK")
    def mouseTask(self,task):
        if base.mouseWatcherNode.hasMouse():
            mpos=base.mouseWatcherNode.getMouse()
            self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
            
            for i in range(len(self.renderLIST)):
                getRENDER = self.renderLIST[i]
                self.myTraverser.traverse(getRENDER)
                if self.myHandler.getNumEntries() > 0:
                      self.myHandler.sortEntries() 
                      pickedObj=self.myHandler.getEntry(0).getIntoNodePath() # change getIntoNodePath() in getSurfacePoint(nodePath) now you get the 3d coordinates back and modify the rest of the code
                      pickedObj=pickedObj.findNetTag('screens')
                      for i in range(len(displayLIST)):
                          dis= str(displayLIST[i])
                          pick = str(pickedObj)
                          if (dis == pick):
                              self.isACTIVE = i

It sounds like you’re asking about picking. There is a sample program that demonstrates this, and lots of discussion about this in the forums.

David

If you want to intersect the mouse ray with a plane:
discourse.panda3d.org/viewtopic.php?t=5409

I tried the sample program, but the question is not about picking, is about putting the object I picked where I click in the 3d window.

the ray with a plane is a good option, in addition I found this:
en.wikipedia.org/wiki/3D_projection

is there a formula for my purpose?

Thank you!

I am going to be trying to do something similar soon…

my plan is to use mouseX and MouseY to set X and Z in the scene (converting mouse to screen coords is heavily discussed in the forum in other places, so I won’t repeat it here), and use the scroll wheel to run a variable up and down to set the Y position.

I anticipate much success!! :^)

But that’s pretty much what pro-softs script is doing: It puts an object on a ground plane on a position where the mouse is pointing at. Picking/putting is just a different way of dealing with the information…

yeah, very good coding, but I knew that I didn’t need to create a plane for this task, the chessboard sample program shows how to do it with mathematics. That’s what I would like to know.

A plane is mathematics. In oro-rsoft’s plane example, the Plane is used as a convenient way to represent the plane equation.

You can also do the same arithmetic yourself. The magic part of the whole thing is base.camLens.extrude(), which converts a 2-d point into a 3-d ray. (Note that it is a ray, not a point, because each point on the 2-d screen corresponds to a whole line of points in the 3-d space.)

What you do with that ray is up to you. You can use simple algebra to figure out where it intersects the z = 0 plane (the ground), or you can use a Plane class to do that calculation for you, as pro-rsoft has shown.

David