0 registered members (),
1,459
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Joystick programing
[Re: LaserJock2000]
#472686
05/14/18 09:09
05/14/18 09:09
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
Give this a try (paste into a new *.c file and start it in SED):
///////////////////////////////
#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:
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);
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual Check out my new game: Pogostuck: Rage With Your Friends
|
|
|
Re: Joystick programing
[Re: LaserJock2000]
#472700
05/15/18 05:40
05/15/18 05:40
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
joy_pressed and joy_axis could be written in a more legible way. For example as follows:
// 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
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual Check out my new game: Pogostuck: Rage With Your Friends
|
|
|
Re: Joystick programing
[Re: Superku]
#472708
05/15/18 12:02
05/15/18 12:02
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, if you know what a pointer is, you might sort all those var addresses into an array.
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;
|
|
|
|