Smooth and exact mouse movement

The combination of a mouse sensor and this script gives a smooth and exact mouse control.

import bge
from mathutils import Vector

scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner

Mouse = cont.sensors["Mouse"]
sensitivity = 0.002
smooth=0.8

w = bge.render.getWindowWidth()//2
h = bge.render.getWindowHeight()//2
screen_center = (w, h)
# Init code
if "oldX" not in own:
    bge.render.setMousePosition(w + 1, h + 1)         
    own["oldX"] = 0.0
    own["oldY"] = 0.0
    
else:      

    scrc = Vector(screen_center)
    mpos = Vector(Mouse.position)
    
    x = scrc.x-mpos.x
    y = scrc.y-mpos.y
    
    # Smooth movement
    own['oldX'] = (own['oldX']*smooth + x*(1.0-smooth))
    own['oldY'] = (own['oldY']*smooth + y*(1.0-smooth))
    
    x = own['oldX']* sensitivity
    y = own['oldY']* sensitivity                               

    # Center mouse in game window
    bge.render.setMousePosition(*screen_center) 
    
    own.applyRotation([0, 0, x], False)
    own.applyRotation([y, 0, 0], True)

UPBGE_0.3_MousePython.blend (823.4 KB)

2 Likes