Joystick programing

Posted By: LaserJock2000

Joystick programing - 05/14/18 00:43

I want to use a "ThrustMaster T.Flight HOTAS X" joystick to control my movements. The Lite-C tutorial/manual covers the basics of x & y movement but not using the rotation of the joystick. I can find no examples of how to use joy_rot nor an axis map of how to use all 6 axis that should be available. Can you please help? Any more detailed resources on-line or books that go deeper into Lite-C?

Thanks
Dave
Posted By: Superku

Re: Joystick programing - 05/14/18 09:09

Give this a try (paste into a new *.c file and start it in SED):
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////

// joyNum 1/2, buttonNum 1-32
int joy_pressed(int joyNum, int buttonNum)
{
	var bitmask = joy_buttons;
	if(joyNum == 2) bitmask = joy2_buttons;

	if(buttonNum <= 10) return !!((bitmask<<10) & (1 << buttonNum-1));
	return !!(bitmask & (1 << buttonNum-11));
}

// joyNum 1/2, axisNum 1-6
var joy_axis(int joyNum, int axisNum)
{
	VECTOR *source[2];
	
	if(axisNum < 1 || axisNum > 6) return 0;
	if(joyNum == 2)
	{
		source[0] = &joy2_raw;
		source[1] = &joy2_rot;
	}
	else
	{
		source[0] = &joy_raw;
		source[1] = &joy_rot;
	}
	axisNum--;
	var *pv = source[axisNum/3];
	return pv[axisNum%3]; // read var at x,y,z
}

// joyNum 1/2
int joy_dpad(int joyNum)
{
	if(joyNum == 2) return joy2_hat;
	return joy_hat;
}

void main()
{
	fps_max = 60;
	video_mode = 9;
	
	while(1)
	{
		draw_text(str_printf(NULL,"num_joysticks: %d",(int)num_joysticks),20,20,COLOR_RED);
		int i,j;
		for(j = 1; j <= 2; j++)
		{
				draw_text(str_printf(NULL,"Joy%d-DPad: %d",j,(int)joy_dpad(j)),200*j,20,COLOR_RED);
			for(i = 1; i <= 6; i++)
			{
				draw_text(str_printf(NULL,"Joy%d-Axis%d: %d",j,i,(int)joy_axis(j,i)),200*j,40+20*i,COLOR_RED);
			}
			for(i = 1; i <= 32; i++)
			{
				draw_text(str_printf(NULL,"Joy%d-Button%d: %d",j,i,(int)joy_pressed(j,i)),200*j,180+20*i,COLOR_RED);
			}
		}
		
		wait(1);
	}
}



Check the values on screen when you move the joystick or push buttons, then use the corresponding axes in your script (copy the helper functions).
For example:

Code:
camera.pan += joy_axis(1, 1)*0.1*time_step;
camera.pan = ang(camera.pan);
my.pan = camera.pan; // assuming a first person camera
camera.tilt -= joy_axis(1, 2)*0.1*time_step; // might need to be "+" as well instead of "-"
camera.tilt = clamp(camera.tilt,-85,85);

Posted By: LaserJock2000

Re: Joystick programing - 05/15/18 01:29

Thanks! This works. I will now spend some time understanding the details.

Are there any good references for Lite-C? The on-line tutorial and manual leave much to be desired in terms of detail and depth.

Thank you very much.

Dave
Posted By: Superku

Re: Joystick programing - 05/15/18 05:40

joy_pressed and joy_axis could be written in a more legible way. For example as follows:

Code:
// joyNum 1/2, axisNum 1-6
var joy_axis(int joyNum, int axisNum)
{
	if(joyNum == 2)
	{
		switch(axisNum)
		{
			case 1:
			return joy2_raw.x;

			case 2:
			return joy2_raw.y;

			case 3:
			return joy2_raw.z;

			case 4:
			return joy2_rot.x;
			
			case 5:
			return joy2_rot.y;

			case 6:
			return joy2_rot.z;

			default:
			return 0;
		}
	}
	// you can put this in an else case but as the function returns in the case above either way you don't need to
	// joyNum == 1 (or other (invalid) values)
	switch(axisNum)
	{
		case 1:
		return joy_raw.x;

		case 2:
		return joy_raw.y;

		case 3:
		return joy_raw.z;

		case 4:
		return joy_rot.x;
		
		case 5:
		return joy_rot.y;

		case 6:
		return joy_rot.z;
	}
	
	return 0;
}


Alternatively you could for example still use the "source" pointer array approach in my original post and then use a switch case statement (instead of the var *pv stuff).


The manual for lite-C is pretty good actually. I use the offline manual (open SED -> press F1) in "Index" mode usually - you will need to know most commands already for that (the Index approach) though.

What are you missing, speaking of detail and depth? Details about the functions themselves or more general concepts (how to write a movement/ enemy code/ menu code)?

Btw. there's a thing called the acknex user magazine, including small game samples (with a focus on the code, not the artwork): http://www.opserver.de/coni_users/web_users/pirvu/aum/aumonline_e/index.html
Posted By: txesmi

Re: Joystick programing - 05/15/18 12:02

Hi,
if you know what a pointer is, you might sort all those var addresses into an array.

Code:
var *joyAxe[2][6];

void joyAxe_startup () {
	joyAxe[0][0] = &joy_raw.x;
	joyAxe[0][1] = &joy_raw.y;
	joyAxe[0][2] = &joy_raw.z;
	joyAxe[0][3] = &joy_rot.x;
	joyAxe[0][4] = &joy_rot.y;
	joyAxe[0][5] = &joy_rot.z;
	joyAxe[1][0] = &joy2_raw.x;
	joyAxe[1][1] = &joy2_raw.y;
	joyAxe[1][2] = &joy2_raw.z;
	joyAxe[1][3] = &joy2_rot.x;
	joyAxe[1][4] = &joy2_rot.y;
	joyAxe[1][5] = &joy2_rot.z;
}
...
vMove.x = *joyAxe[0][1] * nSpeed * time_step;

Posted By: LaserJock2000

Re: Joystick programing - 05/27/18 18:06

Thanks. Got the joystick working.

New question: My car goes through the walls of the arena that I made in WED. How do I make the walls impassible while using the Physics package?

And, again using the Physics package, how do I make the car respond to the joystick in a relative steering mode vice absolute?

Thanks
Dave
© 2024 lite-C Forums