Yet another v2.5 first person mouselook

Just converted this from an old version made by someone else, I like it because it’s shorter than the other ones I’ve seen and it has a good working constraint on the Y axis, as if your head actually has a neck. Hopefully that’s easy to understand.

EDIT (Updated blender file): I did some changes to the script, and tried to explain it as well as I could:

import bge
r=bge.render

c=bge.logic.getCurrentController()
o=c.owner

mouse=c.sensors["Mouse"] #this is used for info about the mouse cursor's position
rotG=c.actuators["Motion"] # this is used to look left/right.

fcurve=c.actuators["F-Curve"] # this is used to look up/down. 

sens=0.001 # define mouse sensitivity.

movtx=(int(r.getWindowWidth()/2)-mouse.position[0]) * sens # distance diff between center
# of the screen and the mouse to determine how to move the camera, in the X axis

movty=(int(r.getWindowHeight()/2)-mouse.position[1]) * sens * 8 # same as above for the Y
#axis. Times eight because the F Curve uses bigger numbers for the same amount of movement
# as dRot on this case.

rotG.dRot=[0,0,movtx] 

c.activate(rotG) 
c.activate(fcurve)

o['mousey']+=(movty) # add vertical mouse movement to this property, whether it's negative
# or positive (up or down). Basically converts the mouse movement to the property which
# is then read by the F-Curve actuator, which converts it to rotation of the camera.

if o['mousey']>30: # put a cap on how much you can look up/down.
    o['mousey']=30
if o['mousey']<0: 
    o['mousey']=0

r.setMousePosition(int(r.getWindowWidth()/2),int(r.getWindowHeight()/2)) #center the mouse

c.deactivate(rotG) 
c.deactivate(fcurve)


EDIT: It seems to work really well right now. The stuttering I said I got before editing this post seemed to be caused more by my mouse than the script.

Download the .blend here:
http://www.mediafire.com/file/ajc07743jd3uhi8/mouselook.blend

No replies for almost 24 hours? Come on…

Nice job on shortening the script :wink: One thing that still needs to be fixed is the camera drifting though.

Thanks. I fixed it and tried to replace the file but apparently that didn’t work very well. Bleh, I’ll use mediafire now that I have 10 posts.

Some hints:

  • Rather to use multiple commands in one line (which should be avoided according to PEP 8) simply remove the commented code [readablity].

  • There is no need to cast to int and round() at the same time (last line) especially as it is done for one axis only :wink: [performance] or is there a reason I miss?

  • There is no information what the named sensor and the named actuators are and what they do [readablity, usablity].

  • The sensor is never checked to be positive [performance?]

  • The actuators are never deactivated but always activated [performance?].

Sorry, I forget the good things:

  • it is simple
  • it is short
  • a python newbie should be able to understand how it works.

Thanks for the suggestions.
I don’t think it’s necessary to check for the sensor to be positive, since the sensor, when it’s “positive”, is what causes the script to run in the first place.
I updated the script… I don’t really understand what the purpose of deactivating the actuator is, but am I doing it correctly?

The round() was because I was playing around with it trying to get it to work correctly… until I found that dividing the window width/height by two when defining movtx/movty was causing the problem.

Works for me. I like the fact that it doesn’t drift, and keeps the mouse in the center of the screen. Mike pan’s drifts, but is beautifully smooth, which I love. I think I’ll switch to yours. Thanks.

Thanks. It wasn’t that difficult to make this stop drifting, just had to always state the center of the screen as int(), here:
movtx=(int(r.getWindowWidth()/2)-mouse.position[0]) * sens
movty=(int(r.getWindowHeight()/2)-mouse.position[1]) * sens * 10
And further at the end.

Here I experimented a bit and tried to use a rotation matrix to look up/down. Thoughts?

import bge
from math import sin, cos

r=bge.render

c=bge.logic.getCurrentController()
o=c.owner

mouse=c.sensors["Mouse"] #this is used for info about the mouse cursor's position
rotG=c.actuators["Motion"] # this is used to look left/right.

sens=0.001 # define mouse sensitivity.

movtx=(int(r.getWindowWidth()/2)-mouse.position[0]) * sens 
movty=(int(r.getWindowHeight()/2)-mouse.position[1]) * sens * 10
rotG.dRot=[0,0,movtx] 

c.activate(rotG)

o['mousey']+=(movty) # add vertical mouse movement to this property, whether it's negative
# or positive (up or down). Basically converts the mouse movement to the property which
# is then read by the F-Curve actuator, which converts it to rotation of the camera.

mov=-(o['mousey']-15)/13
# rotation matrix thing
o.localOrientation[1][2]=-sin(mov)
o.localOrientation[1][1]=cos(mov)

o.localOrientation[2][1]=sin(mov)
o.localOrientation[2][2]=cos(mov)

if o['mousey']>30: # put a cap on how much you can look up/down.
    o['mousey']=30
if o['mousey']<0: 
    o['mousey']=0

r.setMousePosition(int(r.getWindowWidth()/2),int(r.getWindowHeight()/2)) #center the mouse

Attachments

mouselook_ymatrix.blend (434 KB)

thanks for uploading!!