How to set mouse cursor limits inside window?

Hello everyone!

Do you know how to set a limit, (I mean an area/window) where the mouse cursor can move once the player is inside the game?

I know i need python to make it happen. I was searching a way inside Clark’s reference. Maybe if i use “get window height and width” and then defining a range where the mouse can move using “<” and “>” to define an area… But how to do do that exactly?

Thank you for your time!

Yours,

Ortiz


# import Rasterizer
import Rasterizer

# show the mouse pointer
Rasterizer.showMouse(True)

# get game window height
height = Rasterizer.getWindowHeight()

# get game window width
width = Rasterizer.getWindowWidth()

# get the controller
controller = GameLogic.getCurrentController()

# get sensor named Mouse attached to the controller
mouse = controller.sensors["Mouse"]

# get mouse position
pos = mouse.position


You mean something like


# import Rasterizer
import Rasterizer

# show the mouse pointer
Rasterizer.showMouse(True)

# get game window height
height = Rasterizer.getWindowHeight()

# get game window width
width = Rasterizer.getWindowWidth()

# get the controller
controller = GameLogic.getCurrentController()

# get sensor named Mouse attached to the controller
mouse = controller.sensors["Mouse"]

# get mouse position
pos = mouse.position

#if this stays 0, don't take control of the pointer
set=0

GameLogic.getCurrentController().owner['prop']=pos[0]
GameLogic.getCurrentController().owner['prop1']=pos[1]

#if mouse is out of the right side of the frame
if pos[0] &gt; width:
    pos[0] = width
    set=1

#if mouse is out of the left side of the frame
elif pos[0] &lt; 0:
    pos[0] = 0
    set=1

#if mouse is above the frame (the origin of the frame is the top left corner- so the top edge of the screen is 0)
if pos[1] &lt; 0:
    pos[1] = 0
    set=1

#if mouse is below the frame 
elif pos[1] &gt; height:
    pos[1] = height
    set=1

#place the pointer
if set == 1: Rasterizer.setMousePosition(pos[0],pos[1])

I don’t know about windows, but on mac I sometimes get random spikes of lag if setMousePosition runs every frame, so I made it only run if the mouse is out of the frame.

Thank you for your answer Captain Oblivion, but I can’t get the second part of your script: Is it setting the pointer position to zero if it reaches the frame boundaries?

I’m referring to:

#if this stays 0, don't take control of the pointer
set=0

GameLogic.getCurrentController().owner['prop']=pos[0]
GameLogic.getCurrentController().owner['prop1']=pos[1]

#if mouse is out of the right side of the frame
if pos[0] &gt; width:
    pos[0] = width
    set=1

#if mouse is out of the left side of the frame
elif pos[0] &lt; 0:
    pos[0] = 0
    set=1

#if mouse is above the frame (the origin of the frame is the top left corner- so the top edge of the screen is 0)
if pos[1] &lt; 0:
    pos[1] = 0
    set=1

#if mouse is below the frame 
elif pos[1] &gt; height:
    pos[1] = height
    set=1

#place the pointer
if set == 1: Rasterizer.setMousePosition(pos[0],pos[1])

Whitch value should I assign if want to take control of pointer position?

#if this stays 0, don't take control of the pointer
set=0

:spin:

Thank you so much!

“set” is a boolean- 0 is the same as False, whereas 1 is the same as True (I prefer to use the numbers instead of the words because it’s shorter- I sometimes forget that it’s also less clear to other people)

If “set” is False (0), the very last line doesn’t activate, so the cursor is allowed to go free. If it’s True (1), then the cursor is placed at pos (in pixels, where the upper right corner is [0,0]
You can put any screen coordinate in pos, but as long as “set” is False, the mouse won’t be set there.

If you don’t want to bother with that, and prefer to take your chances with the lag (it’s quite possible that was just my computer causing the problem) then take out any reference to “set”- so the second part of the script just reads


GameLogic.getCurrentController().owner['prop']=pos[0]
GameLogic.getCurrentController().owner['prop1']=pos[1]

#if mouse is out of the right side of the frame
if pos[0] &gt; width:
    pos[0] = width

#if mouse is out of the left side of the frame
elif pos[0] &lt; 0:
    pos[0] = 0

#if mouse is above the frame (the origin of the frame is the top left corner- so the top edge of the screen is 0)
if pos[1] &lt; 0:
    pos[1] = 0

#if mouse is below the frame 
elif pos[1] &gt; height:
    pos[1] = height

#place the pointer
Rasterizer.setMousePosition(pos[0],pos[1])

then it will run all the time. Any change you make to pos will move the cursor to wherever you want.

Captain Oblivion has nailed it. You can have a widget parented to a camera so it’s always in the same position onscreen. Rasterizer and a bit of code always displays the pointer within the bounds of the widget. You can have If xPos > 240, xPos = 240 or you can place it back on the left side of the widget at say x = 20 but continue to increase the value of a variable as you sweep the mouse to the right across your desk.

I did a quick and dirty blend.

Attachments

MousePanel.blend (409 KB)

Hi Guys, thank you so much for your support!

Sorry for taking so long to respond.

Attached to this message you should find two files. That’s an orbit viewer with Equip’s script: one for 2.49b and another one for 2.5.

So, Equip’s solution works inside one of them: the 2.5 version. And It works pretty well inside my MacBook :slight_smile:

My problem at the moment is that I need to make this viewer work with 2.49b.

Unfortunately I can’t get Captains Oblivion script to work.

If any of you can put me in the right track (again) I will be glad, :slight_smile:

Thank you for your attention,

Yours,

Ortiz

Attachments

orbit&zoom249b.blend (194 KB)orbit&zoom25.blend (373 KB)

A couple of things I found. In 2.49 you had an Always sensor set to pulse mode which kept the mouse in it’s starting position.

And use:
import Rasterizer
Rasterizer.showMouse(1)
to display the mouse, possibly in both scripts. Sometimes you’ll get a non-fatal error message in the console just for one frame. It doesn’t always matter but it gives you a clue as to which of your scripts are being read first. That doesn’t always matter either but sometimes it does and can be fixed by giving one controller priority over another or some other kind of workaround.

As always if you check the console while your sim is running, you can make little quick trial and error fixes as you go.

BTW you could be using plain ordinary rotation in place of f-curve and quite a lot of coding. You can use rotation in your scripts without causing your object to spin wildly. You set it’s rotation to zero then you apply the rotation you want. That way it turns as you move your mouse and stays put when you let go.