I want to compare the mouse cursor position in a frame to its position in the previous frame to perform certain logic in my code.
Below test code seems to show that the mouse cursor position return by
base.mouseWatcherNode.getMouse()
is not updating every frame? Or is something obvious wrong with the code?
from panda3d.core import *
from direct.task import Task
from direct.showbase.ShowBase import ShowBase
base = ShowBase()
previous_mouse_pos = None
def myTask(task):
global previous_mouse_pos
if base.mouseWatcherNode.hasMouse():
mouse_pos = base.mouseWatcherNode.getMouse()
print(mouse_pos,"\n" , previous_mouse_pos, "\n")
previous_mouse_pos = mouse_pos # store mouse pos to use on next frame
return task.cont
base.taskMgr.add(myTask , "myTask")
base.run()
output:
LPoint2f(-0.8975, -0.0766667)
LPoint2f(-0.8975, -0.0766667)
LPoint2f(-0.8975, -0.0766667)
LPoint2f(-0.8975, -0.0766667)
LPoint2f(-0.93, -0.103333)
LPoint2f(-0.93, -0.103333)
LPoint2f(-0.93, -0.103333)
LPoint2f(-0.93, -0.103333)
LPoint2f(-0.93, -0.103333)
LPoint2f(-0.93, -0.103333)
LPoint2f(-0.92, -0.103333)
LPoint2f(-0.92, -0.103333)
LPoint2f(-0.8975, -0.0966667)
LPoint2f(-0.8975, -0.0966667)
Both variables (previous and current frame mouse cursor position) have same values every time.