Mouse/vJoy script help

Official forum for open source FreePIE discussion and development.
Post Reply
EpicValor
One Eyed Hopeful
Posts: 6
Joined: Fri Oct 17, 2014 2:35 am

Mouse/vJoy script help

Post by EpicValor »

I found this great script for mapping a mouse to a vjoy virtual joystick, but there's a problem. I really need it to set x, y, z back to 0 when the mouse stops moving, or gets set back to zero if I move the mouse in the opposite direction. Which ever is easiest. Is there a way to do that? Hmm, maybe give the x, and -x a buffer range to set x back to zero. Kind of like a joystick snapping back to its deadzone.

Code: Select all

from System import Int16

if starting:
   x = 0
   y = 0
   z = 0
   sensibilidadX = 30
   sensibilidadY = 100
   sensibilidadZ = 100

x += mouse.deltaX * sensibilidadX

if mouse.middleButton:
   x = 0

if mouse.rightButton:
   y += mouse.deltaY * sensibilidadY
else:
   y = 0

if mouse.leftButton:
   z -= mouse.deltaY * sensibilidadZ
else:
   z = 0

if (x > Int16.MaxValue):
   x = Int16.MaxValue
elif (x < -Int16.MaxValue):
   x = -Int16.MaxValue

if (y > Int16.MaxValue):
   y = Int16.MaxValue
elif (y < 0):
   y = 0

if (z > Int16.MaxValue):
   z = Int16.MaxValue
elif (z < 0):
   z = 0
   
vJoy[6].x = x
vJoy[6].y = y
vJoy[6].z = z

diagnostics.watch(vJoy[6].x)
diagnostics.watch(vJoy[6].y)
diagnostics.watch(vJoy[6].z)
EpicValor
One Eyed Hopeful
Posts: 6
Joined: Fri Oct 17, 2014 2:35 am

Re: Mouse/vJoy script help

Post by EpicValor »

Does anyone have any ideas on this?

I can't find any documentation on how to put in a deadzone for mouse deltax reads, or auto centering, or just about anything for this.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Mouse/vJoy script help

Post by CyberVillain »

You can use the code completion and write filters to see the helper functions for all kinds of filters FreePIE support, for eaxmple

deadband
ensureMapRange
mapRange
delta
EpicValor
One Eyed Hopeful
Posts: 6
Joined: Fri Oct 17, 2014 2:35 am

Re: Mouse/vJoy script help

Post by EpicValor »

What about getting the current value of x, inserting a pause, then get the new value of x?

Can that be done?

If not, then there needs to be a way to add a delay between returning values from the same variable, and then comparing the two.


Edit:
I've tried importing time, and using time.sleep between getting the values, which them pauses the entire script where no input from the mouse can be received, and always returns a zero, or making a def mmove (): out of it does nothing.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Mouse/vJoy script help

Post by CyberVillain »

You can use the stop watch function

Code: Select all

if starting:
	x = 0

if filters.stopWatch(True,500):
	x = x + 1
	diagnostics.debug(x)
EpicValor
One Eyed Hopeful
Posts: 6
Joined: Fri Oct 17, 2014 2:35 am

Re: Mouse/vJoy script help

Post by EpicValor »

Thank you, I will test it right now.
EpicValor
One Eyed Hopeful
Posts: 6
Joined: Fri Oct 17, 2014 2:35 am

Re: Mouse/vJoy script help

Post by EpicValor »

Here it is, and it works. X goes to zero when the mouse stops moving. Thank you very much!

Edit, this one works much better

Code: Select all

from System import Int16

if starting:
   enabled = False
   x = 0
   sensibilidadX = 50
   a = 0
   b = 0
   c = 0
   d = 0
      
if (enabled):
   b += mouse.deltaX
   x += mouse.deltaX * sensibilidadX
   d += mouse.deltaX
   
if filters.stopWatch(True,300):
   a = b + 0
   
if filters.stopWatch(True,100):
   c = d + 0

if (a - c == 0):
   x = 0  

if (x > Int16.MaxValue):
   x = Int16.MaxValue / 2
elif (x < -Int16.MaxValue):
   x = -Int16.MaxValue / 2
   
vJoy[5].x = x

diagnostics.watch(b)
diagnostics.watch(a)
diagnostics.watch(vJoy[5].x)

toggle = keyboard.getPressed(Key.Home)

if toggle:
    enabled = not enabled
EpicValor
One Eyed Hopeful
Posts: 6
Joined: Fri Oct 17, 2014 2:35 am

Re: Mouse/vJoy script help

Post by EpicValor »

This script from Skagen at https://www.lfs.net/forum/thread/85877- ... ontrollers is more responsive, and smoother for mouse driving. I modified it to suit my needs for mouse driving in Defiance, and removed the game specific stuff from the script. I'm sure it could be adapted for Y-coordinates as well. The mouse axis in this script behaves better than the above script, and you get fewer premature x's to zero.

I still need to do some tweaking.

Code: Select all

if starting:
    enabled = False
    system.setThreadTiming(TimingTypes.HighresSystemTimer)
    system.threadExecutionInterval = 5
    a = 0
    b = 0
    c = 0
    d = 0
   
    int32_max = (2 ** 14) - 1
    int32_min = (( 2** 14) * -1) + 1
    
    v = vJoy[5]
    v.x, = (int32_min,) 

# Mouse settings

    global mouse_sensitivity, sensitivity_center_reduction
    mouse_sensitivity = 300.0
    sensitivity_center_reduction = 2.0
   

# Steering settings

    global steering, steering_max, steering_min, steering_center_reduction    
    # Init values, do not change
    steering = 0.0
    steering_max = float(int32_max)
    steering_min = float(int32_min)
    steering_center_reduction = 1.0

# Steering logic

if steering > 0:
    steering_center_reduction = sensitivity_center_reduction ** (1 - (steering / steering_max))
elif steering < 0:
    steering_center_reduction = sensitivity_center_reduction ** (1 - (steering / steering_min))
elif steering > 500:
    steering_center_reduction = sensitivity_center_reduction ** (0.1 - (steering / steering_max))
elif steering < -500:
    steering_center_reduction = sensitivity_center_reduction ** (0.1 - (steering / steering_min))
    
if (enabled):    
   steering = steering + ((float(mouse.deltaX) * mouse_sensitivity) / steering_center_reduction)
   b += mouse.deltaX
   d += mouse.deltaX

if steering > steering_max:
    steering = steering_max
elif steering < steering_min:
    steering = steering_min

v.x = int(round(steering))

if filters.stopWatch(True,200):
   a = b + 0
   
if filters.stopWatch(True,30):
   c = d + 0

if (a - c == 0):
   steering = 0

if mouse.middleButton:
   steering = 0.0

# PIE diagnostics logic

diagnostics.watch(v.x)

toggle = keyboard.getPressed(Key.Home)

if toggle:
    enabled = not enabled
Post Reply

Return to “FreePIE”