MouseLook script Cap problem

Original answer (by me) on stackexchange:

I working on a MouseLook script, but the Cap system doesnt work great (When i move too rapidly to the Cap, the camera does ugly jumps).
I explain, my cap upper is set at 160 degree, my camera start at 90 degree, when i move rapidly my mouse up, the camera goes over 160 degrees (more speed, more excessive angle)and rapidly after, the script set the angle at 160 degree, its verry ugly, i dont want the angle goes over my cap value.
i will give you my script for better details.
Maybe the rotation system i used is not correct…
Does someone know how exactly work the cap/rotation system of the MouseMove script by Riyuzakisan?

My faulty script can be founded on the top link.

If you need any more details, tell me.

Thank you.

I have make an third-person-cam modul and my error has been to apply non rotation if the caps reached.


    cap = 90
    angle = rads * (180/ 3.14)
    
    if (angle > (90 + cap/2) and y > 0) or (angle < (90 - cap/2) and y < 0) == True:
        empty.applyRotation((0, 0, 0), True)
    else:
        empty.applyRotation((int(y) * rotSpeed, 0, 0), True)

check this out

much more simple + easy to use.

Attachments

SimpleMouse.blend (554 KB)

Thank you guys.
the_cannibal: how i can add the cap lower and cap upper? Please check my script on the top link.

BluePrintRandow: your mouselook system is cool, but it need 2 scripts and many logic bricks.
i want all inside one script.

Thank you guys.

yeah, mine is much much more flexible

and you can use the properties it generates to do anything,

that is why the two scripts

generate mouse property and set in target

and the script in the target decides what to do with the input,

you can change who the mouse generator is targeting in real time,
to swap control to a vehicle, or a crane or a ???

the reason they are separated is so you can re-use the system in new and intresting ways,

only python is usually a bad design.

logic is your handle to events without polling.

all bricks = Bad design
all python = Bad design

Its a really cool idea, good work.
But i need this for my fps template, and in the game, we only see as the player view, there is no drivable vehicle or anything. and if i need another camera, i just have to switch to the new camera.

Your script can be usefull for my GTA-like project.

Thank you.

sorry, have not seen.


  
    capBas = 20
    capHaut = 160

    camOri = camera.localOrientation    
    vec1 = mathutils.Vector([camOri[0][2], camOri[1][2], camOri[2][2]])    
    vec2 = mathutils.Vector([0.0, 0.0, 1.0])
    rads = mathutils.Vector.angle(vec2, vec1)
    angle = rads * (180/ 3.14)

    if (angle > (90 + capBas/2) and y > 0) or (angle < (90 - capHaut/2) and y < 0) == True:
        camera.applyRotation((0, 0, 0), True)
    else:
        camera.applyRotation((int(y) * rotSpeed, 0, 0), True)

I will try this now, thank you for helping me.

edit: “angle” is “Camrotx”?

     Camrot = camera.localOrientation.to_euler() 

   Camrotx = int(math.degrees(Camrot[0]))

have update the post

I get:
NameError: name ‘mathutils’ is not defined.

At this line:

vec1 = mathutils.Vector([camOri[0][2], camOri[1][2], camOri[2][2]])

at the very top you need

import mathutils

you has to add

import mathutils

okay BluePrintRandom was faster

import math is not the same?

mathutils
This module provides access to matrices, eulers, quaternions and vectors

math is little limited
These functions cannot be used with complex numbers

I have also:
from mathutils import Vector

in my script, i have replaced:
vec1 = mathutils.Vector([camOri[0][2], camOri[1][2], camOri[2][2]])

By

vec1 = Vector([camOri[0][2], camOri[1][2], camOri[2][2]])

ok is good to know.

Nothing work, the camera start up the head, it move but it teleport to another location when a cap is reached.

i paste my script, i dont know where is the mistake:


import bge
from bge import render
from mathutils import Vector


#render.showMouse(1)


def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner
    
    # Inclure l'objet "Camera" dans le script.
    camera = own.children['Camera']
    
    # Props
    cap = True
    capBas = 20
    capHaut = 110


    # Prendre le sensor "Mouse".
    mouse = cont.sensors ["Mouse"]
    
    # sensib = Propriété "Sensibilite".
    sensib = own['Sensibilite']
    
    # Recuperer la taille de l'ecran.
    x = render.getWindowWidth()//2
    y = render.getWindowHeight()//2
    
    # Definir le centre de l'ecran.
    centre_ecran = (x,y)
    
    # centre = Centre de l'ecran.
    centre = Vector(centre_ecran)
    
    # Definir la position de la souris.
    position_souris = Vector(mouse.position)
    
    # Définir la rotation a effectuer.
    offset = (position_souris-centre)*-sensib*0.0002
    
    camOri = camera.localOrientation
    vec1 = Vector([camOri[0][2], camOri[1][2], camOri[2][2]])
    vec2 = Vector([0.0, 0.0, 1.0])
    rads = Vector.angle(vec2, vec1)
    angle = rads * (180/ 3.14)
    
    if (angle > (90 + capBas/2) and y > 0) or (angle < (90 - capHaut/2) and y < 0) == True:
        camera.applyRotation((offset.y, 0, 0), True)
    else:
        camera.applyRotation((int(y) * offset.y, 0, 0), True)
        
    # Appliquer rotation horizontale joueur.
    own.applyRotation((0, 0, offset.x), True)
    
    # Garder le curseur au centre de l'ecran.
    render.setMousePosition(x,y)
   
main()


can we get a .blend with your set up? It is really hard to think about if you can’t see it.

but i think you shuld get the mouse.Position in the x and y

x = (render.getWindowWidth() / 2 - mouse.position[0])
y = (render.getWindowHeight() / 2 - mouse.position[1])

and apply

if not mouse.positive:
x = 0
y = 0

my script works quite nicely,

is there something missing you need?
it is pretty performant…

your muddeling with overcomplications…

since my script uses the players body as a reference and you can adjust the min max of each property, it can’t ever go outside the limit, its impossible,

see mouseLook script (not mouse generate?)
rot = own.parent.worldOrientation.to_euler()

rot.x += own[‘x’]

own.worldOrientation=rot

this is the core of the mouselook.

just make the head invisible, set to,wire view in object tab, and parent your camera to it,

add logic and or python to body to drive any movment scheme.

Here is the test blend (i cant give you the original), but i make juste the mouselook on a empty scene, it dit the exactly same:

Test.blend (514 KB)

Please help me to fix the script.

ok i will post my script:

connect an Mouse movement sensor with the name “Mousemove” and an always sensor to an Python controller and switch to modul it is optimazed for you to an First_Person_Cam_Script.



'''
MouseLook by the_cannibal

'''

from bge import logic, render
import mathutils 


def Cam_Movement(cont):
  
    own = cont.owner
    scene = logic.getCurrentScene()

    rotSpeed = 0.005 

    capBas = 20
    capHaut = 160

    mouse = cont.sensors["Mousemove"]
    
    render.setMousePosition(int(render.getWindowWidth() / 2), int(render.getWindowHeight() / 2))
    
    x = (render.getWindowWidth() / 2 - mouse.position[0])
    y = (render.getWindowHeight() / 2 - mouse.position[1])

    if not mouse.positive:
        x = 0
        y = 0    

    own.applyRotation((0 , 0, int(x) * rotSpeed), False)    

    camOri = own.localOrientation    
    vec1 = mathutils.Vector([camOri[0][2], camOri[1][2], camOri[2][2]])    
    vec2 = mathutils.Vector([0.0, 0.0, 1.0])
    rads = mathutils.Vector.angle(vec2, vec1)
    angle = rads * (180/ 3.14)
    
    if (angle > (90 + capHaut/2) and y > 0) or (angle < (90 - capBas/2) and y < 0) == True:
        own.applyRotation((0, 0, 0), True)
    else:
        own.applyRotation((int(y) * rotSpeed, 0, 0), True)
    
    render.setMousePosition(int(render.getWindowWidth() / 2), int(render.getWindowHeight() / 2))


:cool: