hit check

Is there a hit check like function in Panda3d? Something that will return what object or nodepath is under a specific pixel. This is a purely 2d function, only about what is rendered on the screen. I would think it is easy to do, and most engines do have it, all you have to do is create a lookup table while rendering the objects and reference that.

Which object should hit check return if there are two or more (semi-)transparent objects under the mouse cursor, e.g. for, water or plants? Random, first, all?

I’d say the most generic way is to do a raycast from the camera/lens position “into” the screen (+y direction in camera coordinate system). And then use code to handle the raycast results.

The line of code below is from Tiptoe’s “Point & Click Player Movement” demo (https://discourse.panda3d.org/viewtopic.php?t=2067), and does most of the work. I think the problem is the same.


self.pickerRay.setFromLens(base.camNode, mousepos.getX(),mousepos.getY())

I think I posted the exact thing you’re looking for a few hours before your post.

This actually uses a box of 2d coordinates (a region) since just one point on the 2d plane is unlikely to match exactly to the 3d point of the node.


There is a function that goes the opposite direction without providing a region (You supply the 3d position and it will give you the 2d location on the screen.)

def map3dToAspect2d(self, node, point): 
		"""Maps the indicated 3-d point (a Point3), which is relative to 
		the indicated NodePath, to the corresponding point in the aspect2d 
		scene graph. Returns the corresponding Point3 in aspect2d. 
		Returns None if the point is not onscreen. """ 

		# Convert the point to the 3-d space of the camera 
		p3 = base.cam.getRelativePoint(node, point) 

		# Convert it through the lens to render2d coordinates 
		p2 = Point2() 
		if not base.camLens.project(p3, p2): 
			return None 

		r2d = Point3(p2[0], 0, p2[1]) 

		# And then convert it to aspect2d coordinates 
		a2d = aspect2d.getRelativePoint(render2d, r2d) 

		return a2d

The above code was written by someone else a while ago. It may be what you need but based on what you said I’d guess the code in the thread I linked to above will give you what you need.