# Difference between revisions of "Object Mouse Trackball"

Brolingstanz (talk | contribs) m |
Brolingstanz (talk | contribs) m |
||

Line 9: | Line 9: | ||

The first step in generating a trackball is defining the ball itself, which is simply a sphere at the origin, | The first step in generating a trackball is defining the ball itself, which is simply a sphere at the origin, | ||

− | : | + | :<math>x^2 + y^2 + z^2 = r^2\,</math>, |

+ | |||

+ | where r is the radius of the sphere. | ||

Next we would like to center our sphere on the object we would like to rotate. To do this, we can project the object's center into screen space. | Next we would like to center our sphere on the object we would like to rotate. To do this, we can project the object's center into screen space. | ||

− | Let < | + | Let <math>O</math> be the projected object center. The equation for our sphere then becomes |

− | :(x - | + | :<math>(x - O_x)^2 + (y - O_y)^2 + z^2 = r^2\,</math>. |

− | This will place our sphere at the screen space origin, which, in OpenGL, is the lower left corner of the screen by default. We can put the origin amid screen with an additional translation of half the screen dimensions in either direction, < | + | This will place our sphere at the screen space origin, which, in OpenGL, is the lower left corner of the screen by default. We can put the origin amid screen with an additional translation of half the screen dimensions in either direction, <math>C</math>, giving the final form of our sphere as |

− | :(x - | + | :<math>(x - C_x - O_x)^2 + (y - C_y - O_y)^2 + z^2 = r^2\,</math>. |

Objects outside the view frustum could be culled via a sphere-frustum collision test, for example. | Objects outside the view frustum could be culled via a sphere-frustum collision test, for example. | ||

Line 26: | Line 28: | ||

[[Image:trackball_composite2.png|thumb|512px|right|Sphere and hyperbolic sheet. The circle of intersection is in red.]] | [[Image:trackball_composite2.png|thumb|512px|right|Sphere and hyperbolic sheet. The circle of intersection is in red.]] | ||

− | [[Image:trackball_composite_side.png|thumb|512px|right|View of sphere and hyperbolic sheet in the plane of intersection. The profile curve is C<sup>1</sup> continuous at the crossover.]] | + | [[Image:trackball_composite_side.png|thumb|512px|right|View of sphere and hyperbolic sheet in the plane of intersection. The profile curve is C^<sup>1</sup> continuous at the crossover.]] |

As the mouse moves over the surface of our sphere it traces out a curve. Each position on this curve from the sphere's center is a vector, and any two consecutive vectors spans a plane through the center of the sphere. The normal of this plane is our rotation axis, and the distance traveled by the mouse between any two points implicitly determines the amount of rotation. | As the mouse moves over the surface of our sphere it traces out a curve. Each position on this curve from the sphere's center is a vector, and any two consecutive vectors spans a plane through the center of the sphere. The normal of this plane is our rotation axis, and the distance traveled by the mouse between any two points implicitly determines the amount of rotation. | ||

Line 32: | Line 34: | ||

In detail, as the mouse moves over the screen we first need to generate points on the sphere. That's a simple matter of evaluating our equation, | In detail, as the mouse moves over the screen we first need to generate points on the sphere. That's a simple matter of evaluating our equation, | ||

− | :(x - | + | :<math>(x - C_x - O_x)^2 + (y - C_y - O_y)^2 + z^2 = r^2\,</math>, |

+ | |||

+ | for a given mouse x and y. | ||

But what of points outside the sphere? Frustum culling restricts us to spheres at least partially visible, but we can still generate points outside of the sphere but within the screen's extent, which would generate complex results (square roots of negative numbers). This can be seen in the explicit form of the sphere, | But what of points outside the sphere? Frustum culling restricts us to spheres at least partially visible, but we can still generate points outside of the sphere but within the screen's extent, which would generate complex results (square roots of negative numbers). This can be seen in the explicit form of the sphere, | ||

− | :z(x,y) = | + | :<math>z(x',y') = \sqrt{r^2 - (x'^2 + y'^2)}</math>, |

− | + | where <math>x'</math> and <math>y'</math> are the centered mouse <math>x</math> and <math>y</math> shorthands from above. Wherever <math>x'^2 + y'^2 > r^2</math>, we're in trouble. | |

One nice way to get around this unpleasantness is to use a piecewise combination of surfaces in place of a single sphere. In this way we can switch to one surface in a region where things are awkward in another. One surface that works well in this case is the hyperbolic sheet given by | One nice way to get around this unpleasantness is to use a piecewise combination of surfaces in place of a single sphere. In this way we can switch to one surface in a region where things are awkward in another. One surface that works well in this case is the hyperbolic sheet given by | ||

− | :z(x,y) = | + | :<math>z(x', y') = \frac{r^2 / 2}{\sqrt{x'^2 + y'^2}}</math>. |

This surface has the shape of an inverted trumpet, with the mouth piece at infinity along the z axis and the horn opening flaring out to cover the xy-plane. With the sphere nestled snugly at its opening, we can transition very smoothly from a ball to a flaring horn defined everywhere except the origin. The crossover from sphere to sheet occurs at their intersection, which is defined by the circle | This surface has the shape of an inverted trumpet, with the mouth piece at infinity along the z axis and the horn opening flaring out to cover the xy-plane. With the sphere nestled snugly at its opening, we can transition very smoothly from a ball to a flaring horn defined everywhere except the origin. The crossover from sphere to sheet occurs at their intersection, which is defined by the circle | ||

− | : | + | :<math>x'^2 + y'^2 = \frac{r^2}{2}</math>. |

One might be tempted to simply ignore points outside the sphere altogether; but as the mouse approaches the perimeter of the sphere response can quickly become clumsy and unintuitive due to ∂z/∂x and ∂z/∂y approaching infinity near the silhouette edge. So in addition to avoiding negative numbers in our radical, the primary motivation for the transition is to make rotational falloff as graceful as possible: the farther the mouse ventures from the center of the sphere the less angular displacement is generated between any two mouse positions, which tapers off completely as the sheet flattens off to zero at infinity. | One might be tempted to simply ignore points outside the sphere altogether; but as the mouse approaches the perimeter of the sphere response can quickly become clumsy and unintuitive due to ∂z/∂x and ∂z/∂y approaching infinity near the silhouette edge. So in addition to avoiding negative numbers in our radical, the primary motivation for the transition is to make rotational falloff as graceful as possible: the farther the mouse ventures from the center of the sphere the less angular displacement is generated between any two mouse positions, which tapers off completely as the sheet flattens off to zero at infinity. | ||

Line 56: | Line 60: | ||

With quaternions in hand, object rotations are very straightforward. In fact, with our axis and angle from above, they're nigh automatic. Any two consecutive (mouse) positions on our surface are enough to generate two vectors, the cross product of which is the rotation axis, and the inverse cosine of the dot product of which is the angle. The basic procedure is as follows: | With quaternions in hand, object rotations are very straightforward. In fact, with our axis and angle from above, they're nigh automatic. Any two consecutive (mouse) positions on our surface are enough to generate two vectors, the cross product of which is the rotation axis, and the inverse cosine of the dot product of which is the angle. The basic procedure is as follows: | ||

− | :< | + | :<math> |

− | + | z(x', y') = | |

− | :< | + | \begin{cases} |

− | + | \sqrt{r^2 - (x'^2 + y'^2)} & x'^2 + y'^2 \le \frac{r^2}{2} \\ | |

− | :< | + | \frac{r^2 / 2}{\sqrt{x'^2 + y'^2}} & \mbox{otherwise} |

+ | \end{cases} | ||

+ | </math> | ||

+ | |||

+ | :<math>V_1 = \frac{(x'_1, y'_1, z(x'_1, y'_1))}{|(x'_1, y'_1, z(x'_1, y'_1))|}</math> | ||

+ | |||

+ | :<math>V_2 = \frac{(x'_2, y'_2, z(x'_2, y'_2))}{|(x'_2, y'_2, z(x'_2, y'_2))|}</math> | ||

+ | |||

+ | :<math>N = V_1 \times V_2</math> | ||

+ | |||

+ | :<math>\theta = \arccos{V_1 \cdot V_2}</math> | ||

+ | |||

+ | :<math>Q = (\cos{\frac{\theta}{2}}, \sin{\frac{\theta}{2}} N)</math> | ||

− | With each incremental mouse movement from (x | + | With each incremental mouse movement from <math>(x'_1, y'_1)</math> to <math>(x'_2, y'_2)</math> we apply (accumulate) the rotation <math>Q</math> and then unitize the result to ensure we retain a rotation. |

== Numeric drift and a snapper == | == Numeric drift and a snapper == |

## Revision as of 21:17, 14 April 2009

## Contents

## Introduction

The motivation behind the trackball (aka arcball) is to provide an intuitive user interface for complex 3D object rotation via a simple, virtual sphere - the screen space analogy to the familiar input device bearing the same name.

The sphere is a good choice for a virtual trackball because it makes a good enclosure for most any object; and its surface is smooth and continuous, which is important in the generation of smooth rotations in response to smooth mouse movements. Any smooth, continuous shape, however, could be used, so long as points on its surface can be generated in a consistent way.

## Having a ball

The first step in generating a trackball is defining the ball itself, which is simply a sphere at the origin,

- ,

where r is the radius of the sphere.

Next we would like to center our sphere on the object we would like to rotate. To do this, we can project the object's center into screen space. Let be the projected object center. The equation for our sphere then becomes

- .

This will place our sphere at the screen space origin, which, in OpenGL, is the lower left corner of the screen by default. We can put the origin amid screen with an additional translation of half the screen dimensions in either direction, , giving the final form of our sphere as

- .

Objects outside the view frustum could be culled via a sphere-frustum collision test, for example.

## Of mice and manipulation

As the mouse moves over the surface of our sphere it traces out a curve. Each position on this curve from the sphere's center is a vector, and any two consecutive vectors spans a plane through the center of the sphere. The normal of this plane is our rotation axis, and the distance traveled by the mouse between any two points implicitly determines the amount of rotation.

In detail, as the mouse moves over the screen we first need to generate points on the sphere. That's a simple matter of evaluating our equation,

- ,

for a given mouse x and y.

But what of points outside the sphere? Frustum culling restricts us to spheres at least partially visible, but we can still generate points outside of the sphere but within the screen's extent, which would generate complex results (square roots of negative numbers). This can be seen in the explicit form of the sphere,

- ,

where and are the centered mouse and shorthands from above. Wherever , we're in trouble.

One nice way to get around this unpleasantness is to use a piecewise combination of surfaces in place of a single sphere. In this way we can switch to one surface in a region where things are awkward in another. One surface that works well in this case is the hyperbolic sheet given by

- .

This surface has the shape of an inverted trumpet, with the mouth piece at infinity along the z axis and the horn opening flaring out to cover the xy-plane. With the sphere nestled snugly at its opening, we can transition very smoothly from a ball to a flaring horn defined everywhere except the origin. The crossover from sphere to sheet occurs at their intersection, which is defined by the circle

- .

One might be tempted to simply ignore points outside the sphere altogether; but as the mouse approaches the perimeter of the sphere response can quickly become clumsy and unintuitive due to ∂z/∂x and ∂z/∂y approaching infinity near the silhouette edge. So in addition to avoiding negative numbers in our radical, the primary motivation for the transition is to make rotational falloff as graceful as possible: the farther the mouse ventures from the center of the sphere the less angular displacement is generated between any two mouse positions, which tapers off completely as the sheet flattens off to zero at infinity.

## Sit and spin

So now that we can generate points for our surface anywhere on the screen, there's still the matter of generating a rotation from them. The standard way to do this is through the use of quaternions, which provide a very compact, convenient and robust representation for rotations. What makes quaternions particularly nice in this situation is the property that a normalized quaternion always represents a rotation -- as arbitrary, incremental rotations will be applied to our objects and in no particular order, the importance of this property can't be overstated.

With quaternions in hand, object rotations are very straightforward. In fact, with our axis and angle from above, they're nigh automatic. Any two consecutive (mouse) positions on our surface are enough to generate two vectors, the cross product of which is the rotation axis, and the inverse cosine of the dot product of which is the angle. The basic procedure is as follows:

With each incremental mouse movement from to we apply (accumulate) the rotation and then unitize the result to ensure we retain a rotation.

## Numeric drift and a snapper

While we can be sure our coordinate axes will remain orthogonal under rotation, due to limited floating point precision, we can't fully escape numeric drift in our calculations: After several arbitrary rotations, we can't guarantee that a reversal of the sequence will lead to our original basis exactly. To help stem the numeric tide, it's possible to snap the rotation axes to the standard axes wherever they're very close to one another. Since a quaternion can be readily converted to and from an axis-angle representation, it's possible to first convert the quaternion to an axis-angle, snap the axis (if very close to a standard axis), angle snap, then convert the result back to a quaternion for rotation. The angle snap must be performed in lockstep with the standard axis snap, otherwise a "stair casing" or rotational aliasing effect will ensue with each incremental rotation, the result of which is an ungainly bobble and teeter of the coordinate frame.

## Conclusion

The virtual trackball can provide an intuitive user interface for the interactive rotation of models in 3D.