Use mouse and vJoy to emulate wheel for driving game

Official forum for open source FreePIE discussion and development.
Post Reply
fern
One Eyed Hopeful
Posts: 3
Joined: Sat Feb 15, 2014 11:49 pm

Use mouse and vJoy to emulate wheel for driving game

Post by fern »

I wrote this script in order to drive with the mouse in a racing game (I play netkar pro).

It emulates analog gas and brake pedals by using the vertical movement of the mouse, but only when a mouse button is pressed:
- Accelerate: Press the right mouse button and move the mouse down.
- Brake: Press the left mouse button and move the mouse up.

I suggest to set the game to use linear steering, with no smoothing filters, nor deadbands.

The keys Q, A, Z, X, and the 4th and 5th mouse buttons, are set to emulate joy buttons. The mouse wheel is also configured to trigger a joy button click. I use it to shift gear, so I can drive the car with one single hand.

In netkar, when I drive a F1 car that has 400º of max steering range, the sensibility of the controller remains the same, but the rest of the mouse/wheel movement range (up to 900º) is ignored. I set the value "carLockToLockSteering" to 400 in this case (similar to the steeringLock value available for some wheel controllers).

I hope someone else finds it useful, or as a starting point for your own scripts.

Code: Select all

if starting:
   autoCenterSpeed = 0.1		# Auto center speed when middle mouse button pressed
   maxLockToLockSteering = 900	# Max steering lock in game 
   carLockToLockSteering = 900	# Limits the range of the wheel, while keeping the same sensibility
   
   sensX = 3					# Steering sensibility (Horizontal).
   sensY = 60					# Gas sensibility (Vertical down)
   sensZ = 60					# Brake sensibility (Vertical up)

   joy=vJoy[14]
   xMax = joy.axisMax
   yMax = joy.axisMax
   zMax = joy.axisMax

   # Steering lock
   if (carLockToLockSteering < maxLockToLockSteering):
      xMax = xMax * carLockToLockSteering / maxLockToLockSteering

   x = 0
   y = 0
   z = 0
   wheelUp = 0
   wheelDown = 0

# Steering with mouse left-right
x += mouse.deltaX * sensX

# Autocenter (while middle button pressed)
if mouse.middleButton:
   x /= (1 + autoCenterSpeed)

# Accelerate with mouse down (while right button pressed)
if mouse.rightButton:
   y += mouse.deltaY * sensY
else:
   y = 0

# Brake with mouse up (while left button pressed)
if mouse.leftButton:
   z -= mouse.deltaY * sensZ
else:
   z = 0

# Max ranges
if (x > xMax):
   x = xMax
elif (x < -xMax):
   x = -xMax

if (y > yMax):
   y = yMax
elif (y < 0):
   y = 0

if (z > zMax):
   z = zMax
elif (z < 0):
   z = 0

# Joy axis
joy.x = x
joy.y = y
joy.z = z

# Joy buttons
joy.setButton(0, keyboard.getKeyDown(Key.Q))
joy.setButton(1, keyboard.getKeyDown(Key.A))
joy.setButton(2, keyboard.getKeyDown(Key.Z))
joy.setButton(3, keyboard.getKeyDown(Key.X))
joy.setButton(4, mouse.getButton(3))
joy.setButton(5, mouse.getButton(4))

if mouse.wheelDown:
   wheelDown = 10
if wheelDown > 0:
   wheelDown -= 1
   joy.setButton(6,True)
else:
   joy.setButton(6,False)

if mouse.wheelUp:
   wheelUp = 10
if wheelUp > 0:
   wheelUp -= 1
   joy.setButton(7,True)
else:
   joy.setButton(7,False)


diagnostics.watch(joy.x)
diagnostics.watch(joy.y)
diagnostics.watch(joy.z)
ariefansori
One Eyed Hopeful
Posts: 4
Joined: Wed Dec 17, 2014 4:06 pm

Re: Use mouse and vJoy to emulate wheel for driving game

Post by ariefansori »

Thanks, it's really helpful for me :idea:
now I can write my own script for playing automobilista :D :mrgreen:
anyway, here is my script...

Code: Select all

import winsound

def update():
	global yaw
	global pitch
	global roll
	global x
	global y
	global z

# initialization
if starting:
	# mouse
	enabled			= False
	mousex			= 0
	mousey			= 0
	mousez			= 0
	throttle_inc	= 250
	throttle_dec	= 400
	brake_inc		= 1200
	brake_dec		= 1600
	mod				= 0

	# trackIR
	x			= 0
	y			= 0
	z			= 0

	xMax = vJoy[0].axisMax
	yMax = vJoy[0].axisMax
	zMax = vJoy[0].axisMax

# mouse sensitivity
global mouse_sensitivity
mouse_sensitivity = 4

# mouse sensitivity modifier
mod = mousex / 800
mouse_sensitivity += (mod ** 2) / 2

# mouse steering control
if enabled:
	mousex += mouse.deltaX * mouse_sensitivity
	# throttle (left mouse button)
	if mouse.leftButton:
		mousey += throttle_inc
	else:
		mousey -= throttle_dec
	# brake (right mouse button)
	if mouse.rightButton:
		mousez += brake_inc
	else:
		mousez -= brake_dec
else:
	mousex	= 0
	mousey	= 0
	mousez	= 0

# trackIR control for head movement
trackIR.yaw		= mousex / 300
trackIR.roll	= mousex / 500
trackIR.pitch	= 0
trackIR.x		= 0
trackIR.y		= 0
trackIR.z		= 0

# limit movement range
if (mousex > xMax):
	mousex	= xMax
elif (mousex < -xMax):
	mousex	= -xMax
if (mousey > yMax):
	mousey = yMax
elif (mousey < 0):
	mousey = 0
if (mousez > zMax):
	mousez = zMax
elif (mousez <  0):
	mousez = 0

# toggle mouse steering
if keyboard.getPressed(Key.NumberPad0):
    enabled = not enabled
    winsound.Beep(2500,1000)

# mouse to vJoy
vJoy[0].x = mousex
vJoy[0].y = mousey
vJoy[0].z = mousez

# vJoy buttons
vJoy[0].setButton(1, keyboard.getKeyDown(Key.LeftShift))	# shift up
vJoy[0].setButton(2, keyboard.getKeyDown(Key.LeftControl))	# shift down
vJoy[0].setButton(3, mouse.middleButton)					# speed limiter

# watch
diagnostics.watch(enabled)
#diagnostics.watch(vJoy[0].x)
#diagnostics.watch(vJoy[0].y)
#diagnostics.watch(vJoy[0].z)
#diagnostics.watch(mod)
#diagnostics.watch(mouse_sensitivity)

Post Reply

Return to “FreePIE”