Capping a mouselook

I am writing a mouse look script. I know I could use an already made script, but I like to make most of my code so I can understand it and learn. One thing that I haven’t been able to figure out though, is how to stop it from looking all the way above the character and then being upside down infront. This is a third person script by the way.

import Rasterizer as r
import GameLogic as gl
import mathutils as m
cont=gl.getCurrentController()
own = cont.owner
sensUPDOWN=.0009
sensLEFTRIGHT=.0009
mouse=cont.sensors['Mouse']
updown=cont.actuators['updown']
leftright=cont.actuators['leftright']
pivot = gl.getCurrentScene().objects['cam_pivot2']
#r.showMouse(True)


# extract width and height from gameScreen
width = r.getWindowWidth()
height = r.getWindowHeight()

# distance moved from screen center
x = int(width/2) - mouse.position[0]
y = int(height/2) - mouse.position[1]

r.setMousePosition(int(width/2), int(height/2))
updown.dRot=[y*sensUPDOWN,0,0]
leftright.dRot=[0,0,x*sensLEFTRIGHT]
cont.activate(updown)
cont.activate(leftright)

#angle capping

#works in 2.57

pivot is an empty that controls up and down movement. Thank you for any help.

I took look over the script and what they did was they created a Z vector for the camara and for the objct the camara is parented to. And they then got the angle between the two and saw if it was within the cap.

I tried that after posting this, the problem is that i can only use one angle for both up and down because you are not able to tell if the camera is pointing up or down. Another problem is after i find if it’s outside the cap range, howdo I put it back in the limits? I tried applying a backwards rotation if it went above a certain angle. It worked for if you looked down too far, but if you looked up too far the camera would circle all the way around the player.

A slight modification to your own code should allow for capping.

import Rasterizer as r
import GameLogic as gl
import mathutils as m
cont=gl.getCurrentController()
own = cont.owner
sensUPDOWN=.0009
sensLEFTRIGHT=.0009
mouse=cont.sensors[‘Mouse’]
updown=cont.actuators[‘updown’]
leftright=cont.actuators[‘leftright’]
pivot = gl.getCurrentScene().objects[‘cam_pivot2’]
#r.showMouse(True)

extract width and height from gameScreen

width = r.getWindowWidth()
height = r.getWindowHeight()

distance moved from screen center

x = int(width/2) - mouse.position[0]
y = int(height/2) - mouse.position[1]

#New stuff
#grabbing vectors

orient = own.localOrientation
orientz = [orient[0][2], orient[1][2], orient[2][2]]
vec1 = Mathutils.Vector(orientz)

par = own.parent
parOrient = obj.orientation
parOrientz = [parOrient[0][2], parOrient[1][2], parOrient[2][2]]
vec2 = Mathutils.Vector(parOrientz)
angle = Mathutils.AngleBetweenVecs(vec1, vec2)

if angle < 180 and angle > 0:
r.setMousePosition(int(width/2), int(height/2))
updown.dRot=[ysensUPDOWN,0,0]
leftright.dRot=[0,0,x
sensLEFTRIGHT]
cont.activate(updown)
cont.activate(leftright)

else:
pass

#works in 2.57

I haven’t tested the modifications on 2.55, my 2.57 is on the other computer. This is 2.49,

The code I posted may get you stuck if you look up or down too far, because it wont let you move. I’ll edit it and get back to you.

Thank you, I’ll try it out when I get on my home computer.

I tried to get it working, but wasn’t able to. I changed the code for 2.57, and the object to reflect how my rig is set up, but it wont work. I tried printing the angle to see if that was working, and it seems to only give a value between 0 and 3.11:confused:. Here is a blend file if it helps. Thank you so much for the help.

Attachments

mouselook_problem.blend (779 KB)

Yes thanks the blend helped a lot. I hadn’t built an actual setup, and I was able to identify errors in your blend.

Blender was giving you blender units of rotation, which I think are number of rotations. I imported math, and used math.degree() to get the degrees. Another problem is that blender runs two 180 degree circles rather than one 360 degree circle. You had your empty rotated so the angle between the vectors went from 90-90 degrees. I flipped your empty by 90 degrees so you have a 0-180 degree rotation.

The error that I pointed out before was that once you got past 180 degrees my script would just not let you rotate the camera anymore, I also fixed that by allowing movement away from the cap limit.

Here is the revised code. It has been checked in 2.57b and works perfectley.

import Rasterizer as r
import GameLogic as gl
import mathutils as m
import math
cont=gl.getCurrentController()
own = cont.owner
sensUPDOWN=.0009
sensLEFTRIGHT=.0009
mouse=cont.sensors[‘Mouse’]
updown=cont.actuators[‘updown’]
leftright=cont.actuators[‘leftright’]
pivot = gl.getCurrentScene().objects[‘cam_pivot2’]
#r.showMouse(True)

extract width and height from gameScreen

width = r.getWindowWidth()
height = r.getWindowHeight()

distance moved from screen center

x = int(width/2) - mouse.position[0]
y = int(height/2) - mouse.position[1]

#New stuff
#grabbing vectors
orient = pivot.localOrientation
orientz = [orient[0][2], orient[1][2], orient[2][2]]
vec1 = m.Vector(orientz)
#print(dir(m.Vector))
par = pivot.parent
par2 = par.parent
parOrient = par2.orientation
parOrientz = [parOrient[0][2], parOrient[1][2], parOrient[2][2]]
vec2 = m.Vector(parOrientz)
angle = math.degrees(vec1.angle(vec2))
r.setMousePosition(int(width/2), int(height/2))
leftright.dRot=[0,0,xsensLEFTRIGHT]
cont.activate(leftright)
if angle < 160 and angle > 20:
updown.dRot=[y
sensUPDOWN,0,0]
cont.activate(updown)
elif angle > 160 and y < 0:
updown.dRot=[ysensUPDOWN,0,0]
cont.activate(updown)
elif angle < 20 and y > 0:
updown.dRot=[y
sensUPDOWN,0,0]
cont.activate(updown)
else:
cont.deactivate(updown)
#works in 2.57

And here is your blend back mouselook_problem_test.blend (724 KB)

Thank you so much for the help, this has been puzzling me for quite some time.

I came across this thread and tried out your file, and for once the mouse look actually worked! But when I try to move forward, backwards, etc… it just goes in circles. Why is that?

mcguinessdr do you mind if I use your code?

Yes you may use this code, half of it’s yours now anyway, and anyone else who wants to use it is welcome. I also noticed the thing with it going in circles, here is a fixed file. Sorry for the late reply.