Java Reference
In-Depth Information
(Barrilleaux, 2001). Figure 7 shows the constructor that can be used as well as the code
segment in an accompanying processStimulus method for converting 2D mouse positions
to 3D coordinates in the virtual 3D world.
The transformation of the 2D mouse position to a 3D location in the virtual world is
an important process in picking. This is because what is shown on the computer display is
really a degenerated 2D view of the underlying 3D world. The former is often referred to as
the viewing plane, and the mouse position is always registered with respect to this plane.
The visual objects rendered in a Java 3D scene are nevertheless 3D in nature and the
user is actually picking 3D objects. A transformation of the 2D mouse position to a 3D
coordinates in the virtual universe will therefore need to be done in many applications, as
illustrated in lines 17 to 23 in the code segment in Figure 7.
Other useful information that may be needed in picking the correct 3D objects are the
direction of the picking shape and the virtual world coordinates of the starting point, as
denoted by C in the perspective projection in Figure 7. The former is obtained from lines 26
and 27, while the latter is obtained from subtracting the mouse pick point from the starting
point coordinates. Lastly, line 30 normalizes the direction vector found.
poInt and ray pIckInG shape
Rather than using the default PickShape object for picking, it is often necessary to cre-
ate customized PickShape objects for the purpose of picking in various scenarios. As an
example, in a 3D room environment, it will be more appropriate have a PickShape object
to pick only objects that are nearby instead of something far away.
Technically, the class of PickShape objects is used together with PickTool in defining a
picking operation. Starting from this section, we will proceed to discuss the various types
of PickShape objects that can be employed.
PickRay is the most basic picking shape, and will pick objects in the same way as a
penetrating ray of radiation. Essentially, the picking ray extends into the scene infinitely
in a specific direction and any object intersected by this ray will be picked.
The code segment and result snapshots in Figure 8 illustrate how PickRay can be used
to pick an object and change its color and at the same time print out a relevant message.
The program segment first obtains and then transforms the local mouse and eye positions
from the viewing plane to 3D world coordinates. The normalized ray direction that projects
indefinitely into the scene is then calculated, the closest intersected objects is retrieved,
and its properties is modified to give rise to a change in color. Note that in order for the
program to run properly, the appropriate picking capabilities must be set.
Apart from PickRay, another basic PickShape is PickPoint. As its name implies, using
PickPoint corresponds to using a single point for picking. This, of course, may not be very
user-friendly, as it will be difficult to pick objects at exact 3D locations with a mouse that
Search WWH ::

Custom Search