How do you get an old mouse look to stop drifting in UPBGE?

I’ve been updating some old code into UPBGE and I can’t get the mouse look to work without breaking the whole script.
I could use help or eliminate the mouselook part of the script without breaking it.
Thanks!

import bge
from bge import events
from bge import render
from bge import logic




def main():

    cont = bge.logic.getCurrentController()
    scene = bge.logic.getCurrentScene()
    player = cont.owner
    
    keyboard = bge.logic.keyboard
    
    wkey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.WKEY]
    skey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SKEY]
    dkey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DKEY]
    akey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.AKEY]
    ekey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.EKEY]
    spacekey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]
    qkey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.QKEY]
    
#-Object-----------------------------------------------------------------------------------
    PlayerBox = scene.objects['PlayerBox']
    Camera = scene.objects['CameraE']
    EText = scene.objects['E-Text'] 
    

#-Movement-properties-for-the-PlayerBox---------------------------------------------------------
    moveSpeed = 0.1
    runSpeed = 0.005
    

#-Ladder------------------------------------------------------------------
#-Ladder------------------------------------------------------------------
    Collision_Ladder = cont.sensors['Collision_Ladder']

    if Collision_Ladder.positive and not ekey:
        EText.playAction("Text.001Action",0,10, priority= 1, play_mode= 3, blendin= 20)        
    else:
        EText.playAction("Text.001Action",10,0, priority= 1, play_mode= 3, blendin= 20)             
    if Collision_Ladder.positive and ekey:
        PlayerBox['LadderNear'] = True
        
    else:
        PlayerBox['LadderNear'] = False            

    if PlayerBox['LadderNear'] is True:

        if wkey:
            PlayerBox.setLinearVelocity((0,0,3),True)
            if wkey and spacekey:
                PlayerBox.setLinearVelocity((0,0,6),True)
        else:
            PlayerBox.setLinearVelocity((0,0,0.18),False)                      

        if skey:
            PlayerBox.setLinearVelocity((0,0,-3),True)
            if skey and spacekey:
                PlayerBox.setLinearVelocity((0,0,-6),True)

        if dkey:
            PlayerBox.setLinearVelocity((2,0,0.18),True)


        if akey:
            PlayerBox.setLinearVelocity((-2,0,0.18),True)            
#-Ladder------------------------------------------------------------------
#-Ladder------------------------------------------------------------------

#-Movement-for-the-playerbox--------------------------------------------------------------

    if PlayerBox['LadderNear'] is False:

        if wkey:
            PlayerBox.applyMovement((0,moveSpeed,0),True)
            if spacekey:
                PlayerBox.applyMovement((0,moveSpeed + runSpeed,0),True)

        if skey:
            PlayerBox.applyMovement((0,-moveSpeed,0),True)
            if spacekey:
                PlayerBox.applyMovement((0,-moveSpeed - runSpeed,0),True)
        
        if dkey:
            PlayerBox.applyMovement((moveSpeed,0,0),True)
            if spacekey:
                PlayerBox.applyMovement((moveSpeed + runSpeed,0,0),True)
        if akey:
            PlayerBox.applyMovement((-moveSpeed,0,0),True)
            if spacekey:
                PlayerBox.applyMovement((-moveSpeed - runSpeed,0,0),True)

        if qkey:
            PlayerBox.applyMovement((0,0,0.1+moveSpeed),True)
#-Movement-for-the-playerbox--------------------------------------------------------------

           
#-Mouse-sensibility-properties-for-the-Camera----------------------------------------------
    sensdown = 0.001
    sensup = 0.002
    


#-Mouse-Look------------------------------------------------------------------------------
        
    mouse = logic.mouse
    res_x = render.getWindowWidth()
    res_y = render.getWindowHeight()
    mid_x = int(res_x/2)
    mid_y = int(res_y/2)
   
    x = (mid_x-(res_x*mouse.position[0]))*sensdown
    y = (mid_y-(res_y*mouse.position[1]))/2*sensup
    
    PlayerBox.applyRotation((0,0,x),False)


            
    Camera.applyRotation((y,0,0),True)

    
    render.setMousePosition(mid_x , mid_y)

main()
resX = render.getWindowWidth()
resY = render.getWindowHeight()
ratio = resY/resX

X, Y = logic.mouse.position
X = (X*resX)/(resX-1)
Y = (Y*resY)/(resY-1)

X = (X-0.5)*4
Y = (0.5-Y)*4*ratio

logic.mouse.position = (0.5, 0.5)

I erased everything up to PlayerBox.applyRotation((0,0,x),False) and it doesn’t work.
I even got rid of everything below and only player movement works.
Here’s the project it came from.
BGE_Ladder6.blend (4.1 MB)

That x and y should be X and Y

I swear I’m bad luck with code or something.
It’s always 1 dumb little thing off. :confused:

#-Mouse-Look------------------------------------------------------------------------------
        
    resX = render.getWindowWidth()
    resY = render.getWindowHeight()
    ratio = resY/resX

    X, Y = logic.mouse.position
    X = (X*resX)/(resX-1)
    Y = ((Y*resY)/(resY-1))*ratio

    logic.mouse.position = (0.5, 0.5)                                         

    PlayerBox.applyRotation((0,0,X),False)


            
    Camera.applyRotation((Y,0,0),True)

    
    render.setMousePosition(mid_X , mid_Y)

main()

ditch the setMousePosition

EDIT:
oops, i forgot part of it… one sec

i fixed my post above

I tried this:

        
    resX = render.getWindowWidth()
    resY = render.getWindowHeight()
    ratio = resY/resX

    X, Y = logic.mouse.position
    X = (X*resX)/(resX-1)
    Y = (Y*resY)/(resY-1)

    X = (X-0.5)*4
    Y = (0.5-Y)*4*ratio

    logic.mouse.position = (0.5, 0.5)
    
    PlayerBox.applyRotation((0,0,x),False)


            
    Camera.applyRotation((y,0,0),True)

   
    render(mid_x , mid_y

main()

and this:

        
    resX = render.getWindowWidth()
    resY = render.getWindowHeight()
    ratio = resY/resX

    X, Y = logic.mouse.position
    X = (X*resX)/(resX-1)
    Y = (Y*resY)/(resY-1)

    X = (X-0.5)*4
    Y = (0.5-Y)*4*ratio

    logic.mouse.position = (0.5, 0.5)
    
    PlayerBox.applyRotation((0,0,x),False)


            
    Camera.applyRotation((y,0,0),True)

main()

and neither work

ok I adjusted my camera script for you, remove everything below your #mouselook-------- and replace it with this:

    from mathutils import Vector

    sensitivity = 1.5  # mouse sensitivity
    smooth = 0.6        # mouse smoothing (0.0 - 0.99)
    min = 5            # min camera angle in degrees 
    max = 150          # max camera angle in degrees 

    camera              = cont.owner.scene.active_camera

    if camera.parent:
        #playerbox
        camera_controller   = PlayerBox
    else:
        #if no camera active then you get worldview camera
        #to be able to look around with that one we set it here
        camera_controller   = camera 

    # center mouse on first frame, create temp variables
    if "oldX" not in camera:
        logic.mouse.position = (0.5,0.5)
        camera["oldX"] = 0.0
        camera["oldY"] = 0.0
    else:
        w, h = render.getWindowWidth() - 1, render.getWindowHeight() - 1
        center_x = w // 2 / w # truediv operation
        center_y = h // 2 / h # truediv operation
        x = center_x - logic.mouse.position[0]
        y = center_y - logic.mouse.position[1]
        
        x *= sensitivity
        y *= sensitivity
       
        # Smooth movement
        camera['oldX'] = (camera['oldX']*smooth + x*(1.0-smooth))
        camera['oldY'] = (camera['oldY']*smooth + y*(1.0-smooth))
        x = camera['oldX']
        y = camera['oldY']

        # set the values
        if camera_controller:
            
            camera_controller.applyRotation([0, 0, x], False) # global Z axis left/right

        # get the orientation of the object
        ori = camera.worldOrientation
        
        # create a mathutils vector 1 
        vec1 = Vector([ori[0][2], ori[1][2], ori[2][2]])

        # create a mathutils compare vector 2
        vec2 = Vector([ 0.0, 0.0, 1.0])

        # find angle in radians between two vectors and convert to degrees
        Ang = vec2.angle(vec1) * 180.0 / 3.14159

        # Cap the camera
        if (Ang > min and y<0) or (Ang < max and y>0):
            camera.applyRotation([y, 0, 0], True) # local x axis up/down
       
        # Center mouse in game window
        logic.mouse.position = (0.5,0.5)

Okay! It works! :smile:
Turns out the previous lines of code were way off and I’m not as bad luck with code as I thought!
Thanks again Cotaks!

Np.

i could have given you the fix:

        w, h = render.getWindowWidth() - 1, render.getWindowHeight() - 1
        center_x = w // 2 / w # truediv operation
        center_y = h // 2 / h # truediv operation
        x = center_x - logic.mouse.position[0]
        y = center_y - logic.mouse.position[1]

But that would create an other problem because your script lets it rotate always when moving mouse (try it out you see). So in this case you and me are better of if i just handed you my script hehe

Tho the base code(made for me) and the fix(asked/bug report in a topic of me on upbge github) of this script are not made by me. I adjusted it over the past 7 years to keep it working. (no credits attached)

So have fun with it :slight_smile:

It works too but, what are these other problems?

hmm it works like you like it or?

if i move only 1 mm the camera/player will move into a lot of angles very fast. It stops the drifting indeed, but then it would be impossible to look around normally (at least on my end).

It works as normal.
I was able to climb and look around at the same time too.
BGE_Ladder6 problem line.blend (4.1 MB)

Worried to use it permanently if you said there were problems involved.

Yes my script you can use for like ever lol.
I just meant the fix alone icw. your old script.