Mouse-X Rotation

I have a script that rotates an empty on it’s X axis via the mouse. Since I am bad with combining math and programming (I know, oxymoron), I was wondering how I could modify the following script.

The rotation is run by a 360 degree ipo, but what I want to do is have the mouse only use the right half of the screen. In other words, I want the mouse to move across the right half of the screen twice instead of running the full length, while keeping the rotation property continuous. This means the full 360 degree rotation will be achieved by moving the mouse across the right half twice.

Here is the script:

import Rasterizer as r
import GameLogic as gl

cont = gl.getCurrentController()
own = cont.getOwner()

##Definables

mouse = cont.getSensor("mouse")
mouse_x = mouse.getXPosition()
win_wid = r.getWindowWidth()
win_height = r.getWindowHeight()

## rotX is a float property on the owner object that runs the ipo

own.rotX = (float(mouse_x) / float(win_wid)) * 360

if own.rotX > 359.5:
    set = int((5.5 / 360.0) * win_wid)
    r.setMousePosition(set, mouse.getYPosition())

if own.rotX < 5:
    set2 = int((359 / 360.0) * win_wid)
    r.setMousePosition(set2, mouse.getYPosition())

Any help here would be greatly appreciated! If you need anything clarified just ask!

~~Stu

  • Is this a game-mechanic that you really want to implement, or are you trying to bypass some other problem? Because if it’s the latter, there might be better ways around it.
  • Do you want the mouse to be restricted to the right half of the screen, or do you want to keep it free, but do the calculations only when mouse is on the right side?
  • When you say it needs to move “two times” across the right half of the screen, I assume you mean all the way from the middle to the right edge, and then all the way from the right edge to the middle (back and forth)? Or maybe you want the mouse to teleport back to the middle when it first reaches the right edge, and then just keep going right?Either way, it doesn’t seem like anything all too difficult, but it’s better if I have a clear idea of what you want exactly, before I make an example.

Good questions…

    • This is attempting to bypass a (bug?) with the mouse. When the mouse interacts with the right side of the screen, all works fine. When it interacts with the left side of the screen, it will occasionally “stick” and not register. I’ve seen this problem with bleder cursor files as well. Other MGP members have noticed this problem as well with the script.
    • Restricted to the right half.
    • The latter; I would like the mouse to teleport back to the middle when it hits the right side, and vice versa.

That should help. If there’s a way to correct that bug that I don’t know about, do tell. Thanks!

~~Stu

In your original .blend, with your mouse over the 3D viewport, do [CTRL] + [UP] to make the 3D window full screen. Now play your game and see if the mouse gets stuck on both sides.

If it does, the problem is as follows:

Triggering actions in relation to mouse position over screen edges will not work on screen edges that are actually lined up with the edge of the monitor (this especially becomes an issue in full screen situations). So, assuming that the left edge of your blender 3D window is lined up with the left edge of the monitor, the conditional: mouse.getXPosition() < 0, would never return true.

Now, is this a bug or not - I don’t know. Either way, in such cases the lowest possible value that mouse.getXPosition() can return is 1.

…Get it?

I’m pretty sure that this is your problem, but just in case if I’m wrong (which is actually likely), I made the example do what you actually asked for:
http://socialstorage.googlepages.com/helpStu_mouse.blend

The simple fix for the “Edge Melt” problem is also implemented, (beginning = 2; end = width - 2), so if I was right about what the problem is, you can just get rid of that whole “middle” junk - because then there’s no reason to restrict the mouse.

PS: Lol, “Edge Melt”…It’s time to Log-off.

Nice, thanks for the explanation and the file. I completely understand what you’re saying, however, I think there’s something else wrong here too. I’ll send you the file.

Thanks for taking the time!

~~Stu