aspect2d object hops when clicked, aspect2d scaling issue?

I have a drag and drop interface using directbuttons. Whenever I click on an object it hops to the left or right, depending on where it is on the screen. Also, the the object doesn’t correctly follow the mouse in the x direction. I think this is all because of the aspect2d scaling issue. I tried scaling by the screen aspect ratio, but it doesn’t work. Any suggestions would be appreciated.

class demo(DirectObject):
	def __init__(self):
#MODELS
		jewelHook=loader.loadModel('jewels')
		jewelSearch=jewelHook.findAllMatches('*/*')
		jewelList=list()
		for i in jewelSearch:
			i.setHpr(0,90,0)	# xz coordinates!!!
			jewelList.append(list([i.getName(),i]))
		jewelList.sort()
		jewel=jewelList[2][1]
#SCREEN		
		self.scaleScreen=base.aspect2d.getScale()	
		def scaleScreenCoords(self):
			scaleScreen= base.aspect2d.getScale()
		scaleScreenCoords(self)
		self.accept(base.win.getWindowEvent(),scaleScreenCoords)
#BUTTON/MOUSE
		self.mouseHasObject=False
		def printClicked():
			taskMgr.add(buttonMove,'buttonGrabbed')
			self.mouseHasObject=True
		def buttonMove(task):
			mouseX=0;mouseY=0
			if base.mouseWatcherNode.hasMouse():
				mouseX=base.mouseWatcherNode.getMouseX()
				mouseY=base.mouseWatcherNode.getMouseY()
			button.setPos(mouseX,0,mouseY)
			return task.cont
		def itemGrabber(mouseDown):
			if mouseDown and self.mouseHasObject:				
				taskMgr.remove('buttonGrabbed')
		button=DirectButton(geom=jewel,scale=2,command=printClicked,relief=None)
#INPUT
		self.accept('mouse1-up',itemGrabber,[False])
		self.accept('mouse1', itemGrabber,[True])
#CAMERA/LIGHTING
		base.disableMouse()
		light=AmbientLight('light1')
		light.setColor((1,1,1,1))
		render.setLight(render.attachNewNode(light))
#RUN
demoInstance=demo()
run()

Here’s the source with all the graphics.
sites.google.com/site/ambyra/all/button.zip

As to the jumping, if I may ask: what version of Panda are you using? I seem to recall an issue in my own code that was similar to the impression given by your description, but I think that it disappeared when I switched versions (from 1.7.2 to 1.8.0, I think it was).

As to the movement, I suspect that the problem there is indeed related to the scaling of aspect2d. Specifically, I think that mouse coordinates are in the range -1 to 1.

Is the code that you present in your post that which you used for scaling your coordinates? (I’m afraid that I haven’t downloaded the zip file.) If so, am I correct in guessing that the indentation was messed up, and that the methods declared after init are on the same level as it?

I note in particular that, if I’m not much mistaken, the following should do very little:

def scaleScreenCoords(self):
         scaleScreen= base.aspect2d.getScale() 

Note that there is no “self” before “scaleScreen”, meaning that it should be a local variable and not affect “self.scaleScreen”.

I also don’t see anything that appears to apply the screen scaling to your mouse coordinates.

The mouse moves from -1 to 1 and that’s not the same as aspect2d (unless the window is square), but there’s render2d that fits.
I think that sayind that the pointer moves in render2d is not true, but there is something there…

Try it like this:

button.setPos(base.aspect2d.getRelativePoint(render2d, Point3(mouseX,0, mouseY))) 

Works great, thanks!