Java Reference
In-Depth Information
Figure 13. First code segment for JoystickInputDevice.java
1.
public class JoystickInputDevice implements InputDevice
2.
{
3.
private Joystick joy[] = new Joystick[1];
4.
private Sensor joySensor[] = new Sensor[1];
5.
private SensorRead joySensorRead = new SensorRead();
6.
private Matrix4d matrix = new Matrix4d(); private Matrix3f comMat = new Matrix3f();
7.
private float x, y, z, turningAngle = 0.0f; int direction = 1;
8.
private static final float PAI = 3.14159f, ANGLEPAI = 2.0f*PAI/360.0f; SPAN = 0.02f,
9.
ANGLESTEP = 0.01f;
10.
TransformGroup tran, bodyTrans;
11.
Collision fcollision;
12.
Point3f viewposi;
13.
14.
public JoystickInputDevice(TransformGroup tran, Shape3D body3d,
15.
TransformGroup bodyTrans, Point3f viewposi) {
16.
this.tran = tran;
17.
this.bodyTrans = bodyTrans;
18.
this.viewposi = viewposi;
19.
System.out.println(viewposi);
20.
fcollision = new Collision(tran, body3d, bodyTrans); }
21.
22.
public boolean initialize() {
23.
try { joy[0] = new Joystick(0); } catch (SecurityException se) {} catch (UnsatisfiedLinkError ule) {}
24.
joySensor[0] = new Sensor(this); // make a sensor object out of the input device
25.
comMat.m00 = 1.0f; comMat.m01 = 0.0f; comMat.m02 = 0.0f;
26.
comMat.m10 = 0.0f; comMat.m11 = 1.0f; comMat.m12 = 0.0f;
27.
comMat.m20 = 0.0f; comMat.m21 = 0.0f; comMat.m22 = 1.0f;
28.
return true; }
29.
30.
public void close() {}
31.
32.
public int getProcessingMode() {
return DEMAND_DRIVEN; }
33.
34.
public int getSensorCount() { return 1; }
35.
36.
public Sensor getSensor(int id) {
return joySensor[id]; }
37.
38.
public void setProcessingMode(int mode) {}
39.
Finally, a ProcessInput function is supplied to process the input from the joystick when
the pollAndProcessInput method in line 76 is invoked. Lines 41 and 42 obtain information on
the x and y coordinates, which will be 0 if no button is pressed, and the pressed button of the
joystick. These are used for the purpose of navigation in the virtual world. The movement,
be it rotational or translational, is determined by the button and the coordinates which of
Search WWH ::




Custom Search