setMousePosition when a Camera is active.

Hi !

I’m back for an other question with Python.

I need to center my Mouse when Camera1 is active, but she must be free when I’m in Camera2. How can I do this easily ?

Actually I center my mouse like this :

bge.render.setMousePosition(xpos,ypos)


#attach this to your camera that you want to center mouse pos. 
#Note that center cursor function requires some further info such as resolution, maybe showmouse

import bge

camera = bge.logic.getCurrentController().owner #the object that script is attached to
scene = camera.scene #the scene of that object, ie the current scene

def CenterCursor():
    bge.render.setMousePosition(xpos,ypos)

if scene.active_camera == camera: #if the active camera equals to this object
    CenterCursor()


You can get the active camera using

scene = logic.getCurrentScene()[FONT=sans-serif]
scene.active_camera

Then center the cursor in the window using

bge.render.setMousePosition(render.getWindowWidth()/2, render.getWindowHeight()/2)

i.e.


import bge
from bge import render, logic
scene = logic.getCurrentScene()
if scene.active_camera == scene.objects["Camera"]:
    bge.render.setMousePosition(int(render.getWindowWidth()/2), int(render.getWindowHeight()/2))

You can simplify this by defining your own variables. I wrote it out in full for understanding purposes.

EDIT: @Kheetor beat me to it :stuck_out_tongue:

Thanks for your reply ! :smiley:

I’v try to put this code in my viewer but it seem to be wrong somewhere. My fault I think.

I start with an moving camera who use a cursor. You can move the mouse on the screen easily during the animation. And when you click, it switch to an other camera who turn around a target when you move the mouse. This move work only if you fix the mouse on center of the screen. And if I push Esc I return to my first camera.
If centermouse is still active, my mouse block and wiggle on center of my view.

I use a lot of Logic modules and I’v try to put the Kheetor’s code on an Always sensors of my camera who have the target. But it dont work. My camera use the free mouse. :’(

Like I said you need to input the screen resolution stuff to the script I wrote. MrPutuLips did write that into his mouse position adjust.

 bge.render.setMousePosition(render.getWindowWidth()/2, render.getWindowHeight()/2) 

Please try to look into it yourself so you learn how to become better at python instead of being dependent in others :slight_smile:

That can help :slight_smile:

That’s work !

I’v add
if scene.active_camera == scene.objects[“CamOrbit”]: bge.render.setMousePosition(xpos,ypos)
in my code and somes modifications. Thank you for your help !