From VRwiki
Jump to: navigation, search
Using a Windows joystick under XVR is pretty easy. Just remember that, under the skin, all the joysticks (and steering wheels) look the same: they are things with axis and buttons. XVR accesses them through a
object that is unique. This also seems to imply that there can be only ever one joystick attached. At any rate, this object has a set of properties that reflect that state that the joystick is in: its position on the various axis, the state of the "hat" control (normally used to select a view in flight simulators) and whether the buttons are pushed. For example, to get the position on the X axis, you would do something like this
var Joy=CVmJoystick()

The following example manipulates an object in reaction to user activity on the x,y,z,r,u and v axis, indicates the direction of the "hat" control if it is being actuated, and displays which of the 15 (!) possible buttons have been pressed. Of course, there can be more than one button pressed at the time.

function joystickManipulate(obj)
	//pass it an object, and it will be manipulated by the joystick
	//just to have it on hand
	var j = CVmJoystick();
	//scale factors for joystick actions
	var scale = 10;
	var rScale = -90;
	var x,y;
	// how to convert degrees to radians
	var degtorad = 0.017453292;
	var povrad;
	var i,k,b;
	// Complain if no joystick is connected
	if (!j.enabled)
		ConsoleText(0.1,0.9,"To run the joystick demo, connect a joystick and restart.");
		//Quit("No joystick connected: demo cannot run.");
	// X and Y axis move the object around, Z axis in this joystick is the gas control
	// Other axis are mapped to object rotations
	// Somehow show what the hat is doing
	if (j.pov<=360.0)
		povrad = degtorad*(90-j.pov);
		x = .5+cos(povrad)/3.0;
		y = .5+sin(povrad)/3.0;
	// Binary-convert the Buttons property and display appropriate boxes and text
    b = j.Buttons;
	for (i=0;i<=15;i+=1)

Again, it is likely that your specific joystick does not have 6 axis and 15 buttons, or that it maps them in a way that is different from the one we are using.

Tricky hat

joystick hat from a Saitek Cyborg Evo Force

The hat control is actually a little eight-direction digital joystick on top of your joystick. It is normally used to effect a momentary change in the point of view, but of course you are not limited to that. Its value when read by XVR goes from 0 to 315 in 45 degrees increments, with 0 being straight up relative to the joystick axis. When the user is not touching the hat control, its value is 655.35. So, you would probably use code that look like this:

	if (j.pov<=360.0)
		//do stuff

See Also