Bug in Track function of Trackball

Topics: Developer Forum, User Forum
Sep 21, 2009 at 9:26 AM

The Track() function in Trackball.cs is used to transform the camera/scene with the mouse. The rotation is calculated using the mouse coordinates and a ProjectToTrackball() function.

The problem occurs when the cross product of the previous position vector with the current position vector equals 0,0,0. In that case, the quaternion will have no rotation axis and the constructor new Quaternion() will throw an exception.

Example: _previousPosition = {10,0,0} and currentPosition = {1,0,0} -> CrossProduct = {0,0,0}


Solution: Skip the rotation calculation if the cross product == {0,0,0}

My Track() function:

protected void Track(Point currentPosition)
    Vector3D currentPosition3D = ProjectToTrackball(
        EventSource.ActualWidth, EventSource.ActualHeight, currentPosition);

    Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D);

    // If the cross product = 0,0,0 the quaternion cannot be calculated
    if (!axis.Equals(new Vector3D()))
        double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D);
        Quaternion delta = new Quaternion(axis, -angle);

        // Get the current orientantion from the RotateTransform3D
        //AxisAngleRotation3D r = _rotation; // This line is never used
        Quaternion q = new Quaternion(_rotation.Axis, _rotation.Angle);

        // Compose the delta with the previous orientation
        q *= delta;

        // Write the new orientation back to the Rotation3D
        _rotation.Axis = q.Axis;
        _rotation.Angle = q.Angle;

    _previousPosition3D = currentPosition3D;

Sep 30, 2009 at 12:33 AM

That is a good fix, thanks - I added it to my copy :)