13. 2 OBJECT PICKING
Although spatial transformation plays a leading role in UI development, it is played
out mostly behind the scenes. Object picking also plays a leading role, but it is positioned
center stage in full view of the user right under his mouse's nose. Any time
mouseover feedback is generated in the scene or a drag operation is performed, picking
is required, whether it is for first-, second-, or third-person control, or for navigation
or manipulation. In a POCS, 3D UI, picking is the process of translating the 2D mouse
position on the display screen into an in-scene object selection, a hit, and optionally
the 3D position of the hit on the object. The process requires casting a ray into the
scene through the mouse position and determining which objects, if any, are hit.
Picking can serve other needs besides those in the UI, and Java 3D can support
them. In situations where the application needs to determine the relationship among
objects in the scene, ray-based picking can determine the objects that lie in a particular
direction and their distance. Such in-scene picking can be used in active relational
feedback, such as limiting the length of a skirt to the nearest underlying object or measuring
the distance between two objects like a tape measure. Other forms of in-scene
picking might use a volume instead of a ray, such as to determine which objects are
nearest the target object. Variations of volume-based picking could be used to deter-mine
the approximate visibility of an object in the scene, which is needed because Java
3D provides no native capability for this sometimes-essential operation. The following
discussion will focus on ray-based UI object picking, but keep in mind that the same
basic operation is useful for many other 3D application needs.
13.2.1 Picking overview
Picking, in Java 3D, is a process supported by a number of cooperating classes. There
is no single "pick" class, method, or event trigger for the general case. Picking occurs
in several stages, with the earlier stages getting you close, and the later stages providing
spatial precision to the hit determination. The following is an overview of the full
precision process, which is the one that best supports the UI techniques described in
this book.
- Throw a pick ray from the user's virtual eyeball, through the mouse position in
the view's display plane, and on into the scene. This is shown in figure 13.2.
- Intersect the pick ray with the bounds surrounding the shape objects (leaf scene
graph nodes) in the scene, sorting the hits from closest to farthest.
- Test each hit candidate, starting with the closest, for intersection between the pick ray
and the candidate's actual geometric shape (defined by a geometry array).
- If no shape is hit, or the hit object owning the shape is not a designated target
object, quit the process without a real hit.
- Determine the hit point on the hit object using the distance of the hit ray to the
point of intersection.
The picking process and the issues surrounding it can be technical. You may want
to refer back to this list and to figure 13.2 occasionally to maintain your bearings as
you read through the following sections.
13.2.2 Picking quality
Java 3D offers the designer plenty of features and flexibility for performing object
picking. Translating this from marketing to engineering terms, you as a designer have
some serious tradeoffs to make that affect the quality and performance of picking. As
with many aspects of UI design, one principle reigns supreme: Picking needs to be
precise enough to maintain an intuitive WYSIWYG effect, but not so much so that
poor performance rates make it unusable. Before diving into the details of the Java 3D
picking process, let's consider some of the picking quality issues.
Figure 13.2 The pick ray starts at the eye position and projects through the mouse
cursor into the scene.
Continuous versus discrete picking
In general, object picking occurs continuously. As the mouse moves, the pick status is
continuously updated. Continuous picking is the basis of mouseover feedback, such as
for interaction highlighting, and world-relative control inputs, such as during a WRM
drag. A more restrictive form of mouse picking performs hit testing only when a
mouse button is pressed or released. Such discrete picking is often used in 3D applications
because it is easier to implement and much less demanding on the designer and
the system. It is also generally inferior to continuous picking because any feedback it
provides to the user occurs after the fact; by the time the user presses the mouse but-ton
to attempt a drag, any feedback the pick provides is too late to be of much benefit.
For example, the object may not be draggable, in which case the user's drag action
is simply wasted effort; or the drag operation may be the wrong kind, in which case
the user is required to undo it, which can interrupt the user's task and concentration.
Discrete picking is also incapable of providing feedback during a drag, such as for
WRM control or highlighting of drag-and-drop targets.
Discrete picking is the form provided by the Sun utilities. The UI framework,
on the other hand, uses continuous picking throughout, but it pays a price for such
extravagance. Unlike earlier 3D platforms such as VRML, Java 3D does not seem very
well tuned for continuous precision picking. Part of this may be the efficiency of
Sun's implementation, but a more significant factor seems to be the Java virtual
machine's garbage collector (GC), which causes moving objects to stutter during
intensely interactive operations, such as continuous picking. Still, the advantages of
continuous picking are many and its performance with Java and Java 3D can only
improve with future releases.
In part 4, discrete picking is first demonstrated in the OverEnabling example in
section 19. 1. 4, and continuous picking is first demonstrated in the MultiShaping
example in section 20.1.6.
Geometry versus bounds picking
As mentioned in the picking overview previously, Java 3D offers two levels of spatial
precision in its picking process. The first level performs hit testing against the geometric
bounds associated with the eligible target shapes, and the second level uses
shape geometry intersection. Bounds-based picking is generally faster but can be
imprecise, depending on how close the fit is between the bounds and the shape of its
host leaf node. The speedup difference between the two is noticeable, especially if
large numbers of eligible targets are in the scene, although GC stutter is still an issue.
Geometry picking assures the highest quality of picking. With it, what the user
sees, the user gets— precisely. With bounds picking the user can place the cursor over
an object to start a drag only to have another object highlight and move. Such behavior
in the UI is unexpected, confusing, and definitely not intuitive. As such, the frame-work
uses a slightly watered-down version of full-precision geometry picking, exclusively,
and pays the price in performance. As with continuous picking, the hope is that
future releases of Java 3D will offer picking that is better tuned for high quality use.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|