Moving the Cross-Hair with mouse movement in 2D side-scroller

Hello,
I am new to the Blender Game Engine. Currently I am running tests with the engine, trying to figure out how to move the cross-hair in a 2D side-scroller, that is, when the mouse moves, the cross hair moves along with it.
I made a mesh named ‘MouseReticule’ and used the following script on it:

from bge import logic as G
from bge import render as R

sensitivity=.001

def crossHair():
cont = G.getCurrentController()
screen_width=R.getWindowWidth()
screen_height=R.getWindowHeight()
mouse_move=cont.sensors[‘MouseMove’]
owner = G.getCurrentController().owner
motion = cont.actuators[‘Motion_Reticule’]

R.setMousePosition(int(screen_width/2),int(screen_height/2))

x = (int(screen_width/2)-mouse_move.position[0])
y = (int(screen_height/2)-mouse_move.position[1])

motion.dLoc=[-xsensitivity,0,ysensitivity] (I am facing the x-z plane)
cont.activate(motion)

This module moves the cross-hair at a decent pace along the screen.

But the problem is, the mouse position remains stuck at the centre of the screen which is nulling any condition for mouse-over sensor on any other object in the scene. For example, I can’t levitate a cube with the mouse.
If I remove this–
R.setMousePosition(int(screen_width/2),int(screen_height/2))
then the cursor no longer remains stuck, but the cross-hair now goes haywire all over the place and out of screen—to infinity and beyond.

I would appreciate any help on this matter.
Thanks in advance.

regards,
Shubhabrata.

maybe try this:
R.setMousePosition(int(screen_width),int(screen_height))
no /2. and if that dont work try various numbers…

it might work, but I haven’t tested it :smiley:

Hi megalolligagger,

I tried out your suggestion, but it didn’t work out. :frowning:
You see, by setting the mouse position, we are getting it stuck at a place (be that any place). This is what can’t be if we are to interact with other objects in the scene with the mouse.
Is there any way to map the position of the mouse to the position of the cross-hair mesh?
I found owner.position vector. I am not sure how it works though. Any suggestions on this?

thanks,
regards,
Shubhabrata.

Use my draw file, the “cursor” part :smiley:

http://blenderartists.org/forum/showthread.php?t=210003&highlight=

If you’re looking to tweak it, which is advisable if you need high accuracy with the mouse/cursor placement, you will need to change the property in the script "cursor.py, the variable is called “sensitivity”… (I know the most accurate value is somewhere just below it’s current value, but I couldn’t be bothered to keep trial and error changing as it was 1AM :o)

If it is not what you’re looking for, just mention and I’ll try and actually help… :smiley:

EDIT: the script is called “mouse.py” and the variables are separate, sensY and sensX :smiley:

Hi Haker23,

I tried your mouse.py. While your blend file works perfectly well in blender 2.49, I’m using blender2.54. The python API has changed. I tried tweaking the code to support the new API but to no effect. I would appreciate any help from your part.
And sorry for the delay in response; I was working on my final year project at college. :slight_smile:

Thanks,

Shubhabrata.

I’ll see what I can do…
Whilst otherwise pointless, this message is only an acknowledgement instead of “silence on the airways” :smiley: