(WIP) Basic Hydra -> vJoy analog racing script

Official forum for open source FreePIE discussion and development.
Post Reply
TiagoTiago
One Eyed Hopeful
Posts: 33
Joined: Thu Mar 13, 2014 1:49 am

(WIP) Basic Hydra -> vJoy analog racing script

Post by TiagoTiago »

Since I have a hard time ever finishing things and this is already in a somewhat usable state though still quite far from finished; I thought perhaps I should post it here:

Code: Select all

"""
HydraJoy: Basic Analog Racing v0.001

Author: TiagoTiago
Forum thread: http://www.mtbs3d.com/phpBB/viewtopic.php?f=139&t=20660

Disclaimer: This is provided as is. You are responsible for what happens to your computer and files.
"""

"""
Hold the two Hydra controllers like as if you're holding a steering wheel in front of you. Turns beyond 90 degrees are capped at the max value for each side.
If you need to make smaller adjustments while in the middle of the game spread your hands wide; bring them closer for turning faster (emergent property of how the steering wheel angle is calculated :)

If it's behaving weird try placing the controllers on the base and taking them out again.
Throttle on right trigger and brake on left (might need to be assigned in the game; or depending of the game, need to be recoded as a single axis).
"""

"""
Configuration
"""

SteeringCurveFactor = 1.75 # Use 1.0 for linear; bigger than zero but less than one for twitchier center; and values bigger than 1.0 to make it less sensitive close to center and more towards the edges.
ThrottleCurveFactor = 1.0 #Same as above, but instead of center it's when you're pressing it lightly
BrakeCurveFactor = 1.0


"""
End of configurations
"""

from math import *

def sign(val): #Returns -1 for negatives, 0 for zero and 1 for positives
	return val and (1, -1)[val<0] 

L = 0
R = 1




def applyHydraSide():
	test = 0
	global L, R
	if(hydra[0].side == "L"):
		L = 0
		R = 1
		test = "It's L"
	if(hydra[0].side == "R"):
		L = 1
		R = 0
		test = "It's R"
	

	
	

def update():
	applyHydraSide()
	
	
	steering = -filters.ensureMapRange(atan2((hydra[R].y - hydra[L].y), (hydra[R].x - hydra[L].x)), -pi/4.0 , pi/4.0, -1.0, 1.0)
	
	vJoy[0].x = ((abs(steering) ** SteeringCurveFactor) * sign(steering)) * vJoy[0].axisMax
	vJoy[0].y = ((hydra[R].trigger ** ThrottleCurveFactor) * -vJoy[0].axisMax)
	vJoy[0].z = ((hydra[L].trigger ** BrakeCurveFactor) * vJoy[0].axisMax)
	
	
	vJoy[0].setButton(0, hydra[R].bumper)
	vJoy[0].setButton(1, hydra[L].bumper)
	vJoy[0].setButton(2, hydra[R].one)
	vJoy[0].setButton(3, hydra[R].two)
	vJoy[0].setButton(4, hydra[R].three)
	vJoy[0].setButton(5, hydra[R].four)
	
	vJoy[0].setButton(6, hydra[L].one)
	vJoy[0].setButton(7, hydra[L].two)
	vJoy[0].setButton(8, hydra[L].three)
	vJoy[0].setButton(9, hydra[L].four)
	
	vJoy[0].setButton(10, hydra[R].start)
	vJoy[0].setButton(11, hydra[L].start)
	
	
	if ((hydra[R].joyx == 0) and (hydra[R].joyy == 0)):
		vJoy[0].setDigitalPov(0, VJoyPov.Nil)
	else:
		if (hydra[R].joyx > 0) :
			vJoy[0].setDigitalPov(0, VJoyPov.Right)
			
		if (hydra[R].joyx < 0) :
			vJoy[0].setDigitalPov(0, VJoyPov.Left)
			
		if (hydra[R].joyy > 0) :
			vJoy[0].setDigitalPov(0, VJoyPov.Up)
			
		if (hydra[R].joyy < 0) :
			vJoy[0].setDigitalPov(0, VJoyPov.Down)
			
 

if starting:
	applyHydraSide()		
	
    
    
update()


I can't promise anything; but if you have any suggestions feel free to post them here.


edit: Btw, I tested it only on Trackmania Nations Forever (free game); it doesn't got analog braking (and I think the so called "analog throttle" is not really analog), so you might need to tweak the control mappings in the game to get it working.
Last edited by TiagoTiago on Mon Jan 12, 2015 5:43 pm, edited 2 times in total.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: (WIP) Basic Hydra -> vJoy analog racing script

Post by CyberVillain »

Nice work!
Post Reply

Return to “FreePIE”