No changes from the base application are needed for this example except to set the right name of the class to use in the applet.
There are several different approaches that could be employed to track
the user's location in this example. For simplicity, we will use an explicit
action to read the location and orientation rather than using field listeners
like in other examples. To start with, add a button to the base of the applet
window, and ActionListener interface needed.
In the actionPerformed() method, we need to fetch the node that
would tell us where the user is (an X3D ProximitySensor node).
From that node we can read the two field values position_changed and
orientation_changed. Finally a small bit of code is used to write the
two values to a simple text file.
public class Xj3DAppletTutorial7 extends Applet implements ActionListener { public voidinit() { setLayout(new BorderLayout()); browser = (Xj3DBrowser) getBrowser(); JButton b = new JButton("Get View Position"); b.addActionListner(this); add(b, BorderLayout.SOUTH); loadScene(); } public boolean actionPerformed(ActionEvent evt) { X3DScene scene = (X3DScene) browser.getExecutionContext(); X3DNode sensor = scene.getNamedNode("PROXIMITY_SENSOR"); SFVec3f position = (SFVec3f)sensor.getField("position_chnaged"); SFRotation orientation = (SFRotation)sensor.getField("position_chnaged"); float[] pos_value = new float[3]; float[] orient_value = new float[4]; position.getValue(pos_value); orientation.getValue(orient_value); JFileChooser chooser = new JFileChooser(); if (chooser.showSaveDialoy(this) != JFileChooser.APPROVE_OPTION) return; File out_file = chooser.getSelectedFile(); try { FileWriter fos = new FileWriter(out_file); PrintWriter pw = new PrintWriter(fos); pw.print("pos: " pw.print(pos_value[0]); pw.print(" " pw.print(pos_value[1]); pw.print(" " pw.print(pos_value[2]); pw.println(); pw.print("orient: " pw.print(orient_value[0]); pw.print(" " pw.print(orient_value[1]); pw.print(" " pw.print(orient_value[2]); pw.print(" " pw.print(orient_value[3]); pw.println(); fos.close(); } catch (IOException ioe) { System.out.println("File output problem: "+ ioe.getMessage() +".\n"+ ioe.getStackTrace()); } }
The Shape containing the box must be wrapped in an
Group node that also contains a ProximitySensor node.
The proximity sensor is what we use to fetch the feedback about the user's position
and orientation, relative to its position in the world. The proximity sensor must
have a size associated with it, otherwise it will not generate any output. In this
case we have picked a reasonable size of a 100 unit box. You should select a size
that is appropriate to the world you are in.
The other addition to the file is the need to include a profile or component
addition to include the ProximitySensor node. The node does not exist
in the Interactive profile, so we either need to extend to the
Immersive profile, or add an extra level of component. To keep the
download size of the applet down, we choose to add the additional component level
rather than profile. The existing interactive JARs that have been built include
the ProximitySensor node full implementation already, so that means
a minimal change to your existing code from the basic example.
<Scene>
<Group>
<ProximitySensor DEF='PROXIMITY_SENSOR' size='100 100 100' />
<Shape>
<Appearance>
<Material DEF='material' diffuseColor='0.0 1.0 0.0'></Material>
</Appearance>
<Box />
</Shape>
</Group>
</Scene>
|
[
Xj3D Homepage |
Xj3D @ Web3d |
Screenshots |
Dev docs |
Dev Releases |
Contributors |
Getting Started
]
Last updated: $Date: 2008-09-23 01:56:18 $ |