Need help with mouse look in 2d game ..

Hullo to the blender artist forum! this is my first post … about my first big ‘small-game’ project …

I’m making a 2d game and i think i know almost everything to start working on it except one … how do i make a mouse crosshair?? It should be an object able to move in response to mouse movement …

I went through the BSoD blender game engine basic and advanced and couldn’t find any help from it …

I’ve attached a rough diagram of what i want to achieve … :stuck_out_tongue:

It’s a 2d game, and i need be able to move the crosshair around the screen with the mouse … the character’s aim will track the crosshair object …

Is there a python script that i can use/tweak?? I’m not very good with python so i’ll need some assistance on how to get going with this …

  • Arvind

Attachments


ohk … so here’s the deal so far … i found this code, very popular around blender forums and tutorials …

######################################################
#
#    MouseLook.py        Blender 2.46
#
#    Tutorial for using MouseLook.py can be found at
#
#    www.tutorialsforblender3D.com
#                                                                                                         
######################################################

# import Rasterizer
import Rasterizer

# get controller
controller = GameLogic.getCurrentController()

# get the object this script is attached to
player = controller.getOwner()

# Get sensor named Mouse
mouse = controller.getSensor("Mouse")
   
# Get the actuators
lookLeftRight = controller.getActuator("LookLeftRight")
lookUpDown = controller.getActuator("LookUpDown") 

# get width and height of game window
width = Rasterizer.getWindowWidth()
height = Rasterizer.getWindowHeight()
     
# define mouse movement function
def mouseMove():
   
   # distance moved from screen center       
   x = width/2 - mouse.getXPosition()
   y = height/2 - mouse.getYPosition()
      
   # intialize mouse so it doesn't jerk first time
   if hasattr(player, 'mouseInit') == False:
      x = 0
      y = 0
      # bug in Add Property
      # can't use True.  Have to use 1
      player.mouseInit = 1
      
   # return mouse movement
   return (x, y)

# get mouse movement from function
move = mouseMove()

# set mouse sensitivity
sensitivity = 0.001

# Amount, direction and sensitivity
leftRight = move[0] * sensitivity
upDown = move[1] * sensitivity

# set the values
lookLeftRight.setDRot( 0.0, 0.0, leftRight, False) // <b>can i set these to something like DLoc and make the</b>  
lookUpDown.setDRot( upDown, 0.0, 0.0, True)

# Use them
GameLogic.addActiveActuator(lookLeftRight, True)
GameLogic.addActiveActuator(lookUpDown, True)

# Center mouse in game window
Rasterizer.setMousePosition(width/2, height/2)

Ohki … i looked around and this is what i got … still stuck though :spin: ,

here is a very popular piece of code i found online for moving the camera with the mouse - for looking, like in an fps …
This is the video tut i used. I try it out and I seem to be doing everything right, but i cant seem to get this thing working …

######################################################
#
#    MouseLook.py        Blender 2.46
#
#    Tutorial for using MouseLook.py can be found at
#
#    [www.tutorialsforblender3D.com](http://www.tutorialsforblender3D.com)
#                                                                                                         
######################################################

# import Rasterizer
import Rasterizer

# get controller
controller = GameLogic.getCurrentController()

# get the object this script is attached to
player = controller.getOwner()

# Get sensor named Mouse
mouse = controller.getSensor("Mouse")
   
# Get the actuators
lookLeftRight = controller.getActuator("LookLeftRight")
lookUpDown = controller.getActuator("LookUpDown") 

# get width and height of game window
width = Rasterizer.getWindowWidth()
height = Rasterizer.getWindowHeight()
     
# define mouse movement function
def mouseMove():
   
   # distance moved from screen center       
   x = width/2 - mouse.getXPosition()
   y = height/2 - mouse.getYPosition()
      
   # intialize mouse so it doesn't jerk first time
   if hasattr(player, 'mouseInit') == False:
      x = 0
      y = 0
      # bug in Add Property
      # can't use True.  Have to use 1
      player.mouseInit = 1
      
   # return mouse movement
   return (x, y)

# get mouse movement from function
move = mouseMove()

# set mouse sensitivity
sensitivity = 0.001

# Amount, direction and sensitivity
leftRight = move[0] * sensitivity
upDown = move[1] * sensitivity

# set the values
lookLeftRight.setDRot( 0.0, 0.0, leftRight, False)   
lookUpDown.setDRot( upDown, 0.0, 0.0, True)

# Use them
GameLogic.addActiveActuator(lookLeftRight, True)
GameLogic.addActiveActuator(lookUpDown, True)

# Center mouse in game window
Rasterizer.setMousePosition(width/2, height/2)

If i can get this to work, i think i might have a solution for my problem …

My solution:

First of all i’ll add this code to my crosshair object, not to the camera …

In the lines where we set the values …

# set the values
lookLeftRight.setDRot( 0.0, 0.0, leftRight, False)   // <i><b>Here, instead of DRot, i can write DLoc, to make the object move in one of the axis ..</b></i>
lookUpDown.setDRot( upDown, 0.0, 0.0, True)  // <b>Similarly, DLoc here for the other axis .. movement in both axes (2d) corresponding to mouse movement acheived ... right???????</b>

The trouble is, i cant get this code working even for the camera :spin: … neeeeeed haaaaaalp!!

This should help with moving the cross hair, just change the cursor image…
http://blenderartists.org/forum/showthread.php?t=89089&highlight=custom+cursors

Search the forums because I remember a similar topic being discussed a couple of months ago.

Here is the link…
http://blenderartists.org/forum/showthread.php?t=159645

Thanks hunter!!! great help!!

Something’s really fishy here… I followed the first link … followed all the steps mentioned … when i hit ‘P’ , i did get my object to move, but it moved in a very sticky fashion … as if the framerate were too low … and once i moved my mouse off the the screen, the object ceased to move …

Then i tried the .blend file given for download … it worked perfectly … so i’m using that now as a template … :yes: :yes: :yes:

although my problem is solved, i’d like to find out what i did wrong the first time…

Did you make the objects center on the corner? because it wasent working for me either until i did that.