Java Reference
In-Depth Information
Figure 14. Second code segment for JoystickInputDevice.java
40. public void ProcessInput(Joystick joy, Sensor joySensor) {
41.
int buttons = joy.getButtons(); joySensorRead.setTime(System.currentTimeMillis());
42.
x = joy.getXPos(); z = joy.getYPos(); // determine button press on the directional keypad
43.
if (Math.abs(x) < 0.3f) x = 0.0f; if (Math.abs(z) < 0.3f) z = 0.0f;
44.
if (z<0 && x==0) { direction = 1; System.out.println(direction + " Forward");
45.
viewposi.x = viewposi.x-1.0f*SPAN*(float)Math.sin(turningAngle);
46.
viewposi.z = viewposi.z-1.0f*SPAN*(float)Math.cos(turningAngle);
47.
setPosition3D(tran, viewposi); setPosition3D(bodyTrans, viewposi); }
48.
if(z>0 && x==0) { direction = 2; System.out.println(direction+ " Backward");
49.
viewposi.x = viewposi.x+1.0f*SPAN*(float)Math.sin(turningAngle);
50.
viewposi.z = viewposi.z+1.0f*SPAN*(float)Math.cos(turningAngle);
51.
setPosition3D(tran, viewposi); setPosition3D(bodyTrans, viewposi); }
52.
if(x<0 && z==0) { direction = 3; System.out.println(direction + " Rotate right");
53.
turningAngle += ANGLESTEP; setRotation3D(tran, ANGLESTEP, comMat, 1);
54.
setRotation3D(bodyTrans, ANGLESTEP, comMat, 1);
55.
setRotation3D(bodyTrans, -ANGLESTEP, comMat, 1); }
56.
if(x>0 && z==0) { direction = 4; System.out.println(direction + " Rotate left");
57.
turningAngle -= ANGLESTEP; setRotation3D(tran, -ANGLESTEP, comMat, 1);
58.
setRotation3D(bodyTrans, -ANGLESTEP, comMat, 1);
59.
setRotation3D(bodyTrans, ANGLESTEP, comMat, 1); }
60.
if ( ((buttons & Joystick.BUTTON1) != 0)) { direction = 5; System.out.println(direction + " Elevate");
61.
viewposi.y = viewposi.y+SPAN/2.0f; setPosition3D(tran, viewposi);
62.
setPosition3D(bodyTrans, viewposi); return; }
63.
if ( ((buttons & Joystick.BUTTON2) != 0)) { direction = 6;
System.out.println(direction + " Land");
64.
viewposi.y = viewposi.y-SPAN/2.0f; setPosition3D(tran, viewposi);
65.
setPosition3D(bodyTrans, viewposi); return; }
66.
if ( ((buttons & Joystick.BUTTON3) != 0)) { direction = 7; System.out.println(direction + " Shift R");
67.
viewposi.x = viewposi.x+SPAN*(float)Math.sin(turningAngle+Math.PI/2.0);
68.
viewposi.z = viewposi.z+SPAN*(float)Math.cos(turningAngle+Math.PI/2.0);
69.
setPosition3D(tran, viewposi); setPosition3D(bodyTrans, viewposi); return; }
70.
if ( ((buttons & Joystick.BUTTON4) != 0)) { direction = 8;
System.out.println(direction + " Shift left");
71.
viewposi.x = viewposi.x-SPAN*(float)Math.sin(turningAngle+Math.PI/2.0);
72.
viewposi.z = viewposi.z-SPAN*(float)Math.cos(turningAngle+Math.PI/2.0);
73.
setPosition3D(tran, viewposi); setPosition3D(bodyTrans, viewposi); return; }
74.
joySensor.setNextSensorRead( joySensorRead ); }
75.
76.
public void pollAndProcessInput() { ProcessInput(joy[0], joySensor[0]); }
77.
78.
public void processStreamInput() {}
course is associated with the joystick position. Subsequently, the appropriate transformation
can be calculated and invoked to change the view from the ViewPlatform.
sensors
From a programming point of view, sensors serve to obtain and translate inputs from ex-
ternal devices such as a joystick into digital data that can be used within the scene graph in
Search WWH ::




Custom Search