base.mouseWatcherNode.getMouse() does not update every frame?

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.

getMouse returns a reference to an internally-stored Point2 object, so you’re not actually creating a copy of the mouse position, you’re just referencing the same Point2. Use Point2(...) to create a copy.

I also find this behaviour very annoying and unintuitive and intend to change this.

3 Likes

Strange behavior indeed.
Thanks.

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 = Point2(mouse_pos) # store mouse pos to use on next frame
	
	return task.cont
	
base.taskMgr.add(myTask , "myTask")

base.run()

For the record, this annoyance will be fixed in Panda3D 1.11.0.

2 Likes