Xj3D Browser Applet Example of Viewpoint Tracking

HTML Modifications

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.

Java Code Modifications

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");
        add(b, BorderLayout.SOUTH);


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];


        JFileChooser chooser = new JFileChooser();

	if (chooser.showSaveDialoy(this) != JFileChooser.APPROVE_OPTION)

        File out_file = chooser.getSelectedFile();
	try {
            FileWriter fos = new FileWriter(out_file);
            PrintWriter pw = new PrintWriter(fos);
            pw.print("pos: "
            pw.print(" "
            pw.print(" "

            pw.print("orient: "
            pw.print(" "
            pw.print(" "
            pw.print(" "


	} catch (IOException ioe) {
		System.out.println("File output problem: " + ioe.getMessage() + ".\n" + ioe.getStackTrace());

X3D Modifications

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.

    <ProximitySensor DEF='PROXIMITY_SENSOR' size='100 100 100' />
        <Material DEF='material' diffuseColor='0.0 1.0 0.0'></Material>
      <Box />

Source Files

[ Xj3D Homepage | Xj3D @ Web3d | Screenshots | Dev docs | Dev Releases | Contributors | Getting Started ]
Last updated: $Date: 2008-09-23 01:56:18 $