Rectangular selection using cardmaker

Hey folks, I’m trying to draw a rectangular selection using this snippet of code and doesn’t work with render2d, I’m using QPanda3D. When I change render2d to render, it works drawing inside my scene the card but this doesn’t match mouse positions. It appears to be a problem with render2d on QPanda3D because I can create 2d objects on a vanilla Panda3D environment. Any help would be great.

from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
from panda3d.core import CardMaker, CollisionHandlerQueue, CollisionTraverser, LineSegs, CollisionNode, CollisionRay, \
    Point2, BitMask32

from view.World import World

class Select(DirectObject):
    """ Class to manage all selectable objects in the screen"""
    _instance = None
    world = World.instance()

    def __init__(self):
        self.collTrav = CollisionTraverser('selector')
        self.selHandler = CollisionHandlerQueue()
        self.selection = set()
        self.tmpSelection = set()
        CM = CardMaker('sel')
        CM.setFrame(0, 1, 0, 1)
        self.rect = render2d.attachNewNode(CM.generate())
        self.rect.setColor(0, 1, 1, .2)
        LS = LineSegs()
        LS.moveTo(0, 0, 0)
        LS.drawTo(1, 0, 0)
        LS.drawTo(1, 0, 1)
        LS.drawTo(0, 0, 1)
        LS.drawTo(0, 0, 0)
        # binding the key to the click

        # add collision-detection for single clicks
        self.clickColNP ='sel'))
        self.clickColRay = CollisionRay()
        # set the FROM mask to 0x10 to match the INTO mask of the balls
        self.collTrav.addCollider(self.clickColNP, self.selHandler)

    def click(self):
        if not
        self.click_start = Point2(
        self.rect.setPos(self.click_start[0], 0, self.click_start[1])
        t = taskMgr.add(self.update_rect, "update_rect")
        self.acceptOnce("mouse1-up", self.release_click, extraArgs=[t])

    def release_click(self, t):

    def update_rect(self, task):
        if not  # check for mouse first, in case the mouse is outside the Panda window
            return Task.cont
        d = - self.click_start
        self.rect.setScale(d[0] if d[0] else 1e-3, 1, d[1] if d[1] else 1e-3)
        return task.cont

    def instance(cls):
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

If the problem indeed lies with QPanda3D, then I don’t know what it might be–it might be worth asking on the GitHub site to which you linked.

However, in general coordinates in render won’t match the same coordinates in render2d. The former is three-dimensional, and its view transformed by the camera’s position, rotation, lens, and so on.

For example, consider a point with coordinates (0, 0, 0). In render2d, and presuming that no transformation has been applied to render2d, this corresponds to a point at the centre of the screen, I believe. In render, however, it corresponds to a point at the centre of the 3D world, and depending on where the camera is located and pointed (and otherwise transformed, potentially), it could translate to a position anywhere on the screen, or off of it, I daresay!

So, what we want is a means to transform 2D screen-coordinates into 3D world-coordinates. And Panda3D has tools for that purpose! Specifically, via the “extrude” suite of methods. (See the API starting at this entry.)

Now, there is a caveat: a single point in 2D screen-coordinates doesn’t map to a single point in 3D space–it maps to a line. Simply put, a 2D point doesn’t specify “how deep into the screen” the corresponding point is. How you deal with that may depend on your exact purposes.

Here’s a quick example of how one of the “extrude” methods might be used:

        if base.mouseWatcherNode.hasMouse():
            mousePos = base.mouseWatcherNode.getMouse()
            mousePos = Point3()
        # These two points are the variables into which "extrude"
        # will put its results
        nearPoint = Point3()
        farPoint = Point3()
        # This example presumes that you're using the default camera,
        # and without having changed its lens, and so uses the lens
        # associated with that
        base.camLens.extrude(mousePos, nearPoint, farPoint)

I’m trying to mimic this behavior. But I can’t past the rectangle drawing section. I think it’s because QPanda3D copy the graphic stream from Panda3D into an Image Panel. Could it be?