Limit Rotation for mouse control script.

Ok, so lets say Ive got a set up like this. Where my cubes rotation is controlled by the movement of the mouse via this script;

from bge import logic as Gfrom bge import events
sensitivity = 10.0
speed = 0.1
owner = G.getCurrentController() .owner
y = 0.5 - G.mouse.position[1]


if abs(y) > 0.001 : owner.applyRotation([ y, 0, 0], True)
G.mouse.position = (0.5,0.5)



But i dont want the cube to rotate all the way around, lets just say 45 and -45 would be the constraints for now, does anybody know how to go about this? Thanks in advanced

1 add a second object in the same location as the other cube, that we will call “center”

import Mathutils
Diff = own.worldOrientation-center.worldOrientation

Diff = Diff.toEuler
Min=-45
Max=45

if (y > .01) and (y + Diff.y<Max):
    owner.applyRotation([ y, 0, 0], True)
if (y < -.01) and (y + Diff.y>Min):
    owner.applyRotation([ y, 0, 0], True)
## this may need a little correcting

ok,
I am having some trouble changing the rotations into a matrix,

from bge import logic as Gfrom bge import events
import mathutils


sensitivity = 10.0
speed = 0.1
owner = G.getCurrentController() .owner
y = 0.5 - G.mouse.position[1]


scene = G.getCurrentScene()
center=scene.objects['Center']




cont = G.getCurrentController()
own = cont.owner






DiffRot = own.worldOrientation-center.worldOrientation
matrix1 = mathutils.Matrix(DiffRot)


Diff = matrix1.toEuler()
Min=-45
Max=45




if (y > .01) and (y + Diff.y<Max):
    owner.applyRotation([ y, 0, 0], True)
if (y < -.01) and (y + Diff.y>Min):
    owner.applyRotation([ y, 0, 0], True)




G.mouse.position = (0.5,0.5)



Hmm, Tried using that code, but its not rotating anymore :stuck_out_tongue:

it’s not right yet, it needs correction ,

Okay, that makes sense haha, Thanks for the help!

I think MarcoIT could hep, he is a genius :smiley:

I don’t use rotation -> euler and back to rotations very often,

Hi,

this is the “correction” of the first code:




from bge import logic as G
from bge import events
sensitivity = 10.0
speed = 0.1
owner = G.getCurrentController() .owner
#y = 0.5 - G.mouse.position[1]




#if abs(y) > 0.001 : owner.applyRotation([ y, 0, 0], True)
#G.mouse.position = (0.5,0.5)


mouse_y = 0.5 - G.mouse.position[1]


if abs(mouse_y) > 0.001:
    
    ### get the rotation in radians
    x, y, z = owner.localOrientation.to_euler()
    
    # add the rotation from the mouse imput
    y += mouse_y
    
    ### set the limit in radians (45° = 0.8 more or less)
    min_ang = -0.8
    max_ang = 0.8
    
    ### adjust the value if out of limits
    if   y < min_ang : y = min_ang
    elif y > max_ang : y = max_ang
    
    ### rewrite the matrix (the real matrix is written automatically , these here just 3 floats)
    owner.localOrientation = x, y, z
    
    G.mouse.position = (0.5,0.5)
    
    

it use “localOrientation” , is so relative to the matrix of parent if there a parent, otherwise is the equivalent of worldOrientation

PS: of course , the mouselook is a bit “poor” :wink:

Thanks a bunch!

and here is a example

you need a “init” value to make it not twitch on start

like
if “On”=False
cursor =.5,.5
On=True

Attachments

XandYSepetate.blend (460 KB)

I think I may use this in wrectified for the players hand control IK

Interesting. A script like this really has a lot of uses! Thanks for all the help guys!