Joystick Mouse Emulator Component for Blender Game Engine - Control Mouse with Joystick Input

Hello everyone! I’ve recently created a new script for UPBGE that allows you to emulate the movement of your mouse movement using the joystick as input! (Best for camera controls.)
Simply attach it to your game object, adjust the properties to suit your preferences (including sensitivity and axis inversion), and you’re ready to go!

Give it a try in your projects! (No credit required!) If you have any problems, please ask!

Here is the script.:


import bge

class JoystickMouseEmulator(bge.types.KX_PythonComponent):

    def start(self, args):
        # Define initial values
        self.joy_index = 0  # Adjust the joystick index as needed
        self.dead_zone = 0.2  # Adjust the dead zone as needed
        self.initial_mouse_position = bge.render.getWindowWidth() // 2, bge.render.getWindowHeight() // 2

    def update(self):
        # Get object properties
        invert_x = self.object.get("invert_x", False)
        invert_y = self.object.get("invert_y", False)
        sensitivity = self.object.get("stick_sensitivity", 0.01)

        # Get directional input from the right stick
        joy = bge.logic.joysticks[self.joy_index]
        if joy is not None:
            x = joy.axisValues[2]  # Adjust the joystick x-axis index as needed
            y = joy.axisValues[3]  # Adjust the joystick y-axis index as needed
            
            # Apply dead zone
            if abs(x) < self.dead_zone:
                x = 0
            if abs(y) < self.dead_zone:
                y = 0
                
            y *= -1
            
            # Apply inversion
            if invert_x:
                x *= -1
            if invert_y:
                y *= -1
                
            
            # Move the mouse based on joystick input
            mouse_x, mouse_y = bge.logic.mouse.position
            mouse_x += x * sensitivity
            mouse_y -= y * sensitivity
            
            # Clamp mouse position within window boundaries
            width = bge.render.getWindowWidth()
            height = bge.render.getWindowHeight()
            mouse_x = max(0, min(width - 1, mouse_x))
            mouse_y = max(0, min(height - 1, mouse_y))
            
            # Update mouse position
            bge.logic.mouse.position = (mouse_x, mouse_y)

Alternatively, you can download the script here:

I hope this helps with your projects!

1 Like

I made a V2 of the script, fixing a few issues that were in V1.

import bge

class JoystickMouseEmulator(bge.types.KX_PythonComponent):

    def start(self, args):
        # Define initial values
        self.joy_index = 0  # Adjust the joystick index as needed
        self.dead_zone = 0.1  # Adjust the dead zone as needed
        self.initial_mouse_position = bge.render.getWindowWidth() // 2, bge.render.getWindowHeight() // 2
        self.smooth_factor = 0.4  # Adjust the smoothing factor (0.0 to 1.0) as needed
        self.prev_x = 0
        self.prev_y = 0

    def update(self):
        # Get object properties
        invert_x = self.object.get("invert_x", False)
        invert_y = self.object.get("invert_y", False)
        sensitivity = self.object.get("stick_sensitivity", 0.01)

        # Get directional input from the right stick
        joy = bge.logic.joysticks[self.joy_index]
        if joy is not None:
            x = joy.axisValues[2]  # Adjust the joystick x-axis index as needed
            y = joy.axisValues[3]  # Adjust the joystick y-axis index as needed
            
            # Smooth the input using a simple low-pass filter
            x = self.prev_x + self.smooth_factor * (x - self.prev_x)
            y = self.prev_y + self.smooth_factor * (y - self.prev_y)
            self.prev_x = x
            self.prev_y = y
            
            # Check if joystick input is outside dead zone
            if abs(x) > self.dead_zone or abs(y) > self.dead_zone:
                # Apply dead zone
                if abs(x) < self.dead_zone:
                    x = 0
                if abs(y) < self.dead_zone:
                    y = 0
                    
                y *= -1
                
                # Apply inversion
                if invert_x:
                    x *= -1
                if invert_y:
                    y *= -1
                    
                
                # Move the mouse based on smoothed joystick input
                mouse_x, mouse_y = bge.logic.mouse.position
                mouse_x += x * sensitivity
                mouse_y -= y * sensitivity
                
                # Clamp mouse position within window boundaries
                width = bge.render.getWindowWidth()
                height = bge.render.getWindowHeight()
                mouse_x = max(0, min(width - 1, mouse_x))
                mouse_y = max(0, min(height - 1, mouse_y))
                
                # Update mouse position
                bge.logic.mouse.position = (mouse_x, mouse_y)

If you want to download the script, the link is below:

1 Like