FPS-like movement

Hi all !

In my program, you use a FPS-like movement to move around. It works (and it works well) but there is a tiny bug left. I activate the mouse-guided rotation of the camera by right clicking, and depending on where this right click is done, I see the camera rotating although the mouse is not moving at all. I suspect there is a rounding error, but I can’t find a way to make it disappear.

Here is the code I use :

# init
props = base.win.getProperties()
self.windowSizeX = props.getXSize()
self.windowSizeY = props.getYSize()
self.windowCenterX = self.windowSizeX / 2
self.windowCenterY = self.windowSizeY / 2   
self.rotationSpeed = 40
# getting current cursor position

mouseX = base.mouseWatcherNode.getMouseX()
mouseY = base.mouseWatcherNode.getMouseY()

dtheta = (self.oldMouseX - mouseX) * self.rotationSpeed
dphi = (mouseY - self.oldMouseY) * self.rotationSpeed

# replacing the cursor at its stored position
# when I right click, self.oldMouseX and self.oldMouseY take values contained in base.mouseWatcherNode.getMouseX/Y
base.win.movePointer(0,int(self.windowCenterX * (1+self.oldMouseX)),int(self.windowCenterY * (1 - self.oldMouseY)))

# updating camera position

theta = base.camera.getH()
phi = base.camera.getP()
base.camera.setHpr(theta + dtheta,phi + dphi,0)

Any idea ?

try base.win.getXSize() and base.win.getYSize() instead of using WindowProperties.

It does not change anything.

I think that I know where the error is. I believe that if I do not reposition the mouse afterwards, there will be no problem. The line with all the ‘int’ seems to be the troublemaker.

If you do not reposition the mouse back to the center of the screen your cursor will leave the screen when in window mode.

I know that. I just wanted to point out the fact that the repositionning is responsible for my trouble.

Is there a way to set the mouse position with the same coordinate system as the one used when you get the mouse position ?

Do it the other way round: get mouse position in the same CS as the one you use for setting the mouse.

md = base.win.getPointer( 0 )
x = md.getX( )
y = md.getY( )
if base.win.movePointer( 0, x, y ): ...

Both are pixel coordinates, that is integers in the range of (0,0) to (800,600) or whatever resolution you use.


Thanks ! It works fine.