I was shocked to learn that many users do not know about the existence of many useful mathematical classes and functions that Panda provides. Thus, many users use Panda’s collision system if they want to detect where a mouse ray hit a ground plane (for point-n-click movement in a 2d world) etc etc.
Thus, I quickly threw this snippet together that shows how to use the Plane class to calculate the intersection point with a mouse line. Super-fast. (Note that there are many more uses to this.)
from direct.showbase.ShowBase import ShowBase
from panda3d.core import Plane, Vec3, Point3, CardMaker
class YourClass(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.disableMouse()
self.camera.setPos(0, 60, 25)
self.camera.lookAt(0, 0, 0)
z = 0
self.plane = Plane(Vec3(0, 0, 1), Point3(0, 0, z))
self.model = loader.loadModel("jack")
self.model.reparentTo(render)
cm = CardMaker("blah")
cm.setFrame(-100, 100, -100, 100)
render.attachNewNode(cm.generate()).lookAt(0, 0, -1)
taskMgr.add(self.__getMousePos, "_YourClass__getMousePos")
def __getMousePos(self, task):
if base.mouseWatcherNode.hasMouse():
mpos = base.mouseWatcherNode.getMouse()
pos3d = Point3()
nearPoint = Point3()
farPoint = Point3()
base.camLens.extrude(mpos, nearPoint, farPoint)
if self.plane.intersectsLine(pos3d,
render.getRelativePoint(camera, nearPoint),
render.getRelativePoint(camera, farPoint)):
print "Mouse ray intersects ground plane at ", pos3d
self.model.setPos(render, pos3d)
return task.again
YourClass()
base.taskMgr.run()