Selecting trim lines

The internal routines that allow the user to select a trim line have recently been rewritten in version 1.0.07. This article describes how it works now.

How hard can it be to click on something with your mouse right? This is done in every website or game. Clicking on buttons, clicking on items. Easy, out of the box functionality.

Selecting a trim line
Selecting trim lines in rough conditions

Alas, not in Sailaway. The boat is constantly moving, the trim lines are thin, close together and even overlapping each other sometimes. Using the default game engine technology to click on an object that is underneath the mouse pointer would make it really hard to select a specific line that is only a few pixels wide while sailing on a stormy sea.

Instead, a special routine is needed that figures out which line or interactive object is underneath the mouse pointer and highlight it. This means a lot of computations and therefor the whole process needs to run in a background thread to complicate things even further.

Before the thread is started, the location of the mouse pointer, the transformation matrices of the camera, the boat and each trim line is stored, because these are is likely to move during the time it takes to do the background computations.

First an imaginary ray is drawn from the mouse pointer in the viewing direction. Since direct hits on trim lines on a stormy sea are difficult to accomplish, we need to compute the distance between the trim line and the imaginary view ray.

A trim line is not a straight line in the mathematical sense. It is a mesh made up out of several triangles. For each triangle the distance between the camera and the center of the triangle is taken. This distance is then used to take a single point on the imaginary view ray. Next, the distance between that point (that is directly underneath the mouse pointer at the same distance as the trim line triangle) and each of the 3 sides of the triangle is computed. This is done for all the triangles of the trim line to find the closest distance.

Each line stores it’s own “under the mouse pointer” weight value which is gradually decreased every time the routine is started and increased with the reverse distance to the cursor. The closer the distance of the trim line to the imaginary view ray, the higher this weight value becomes.

After all trim lines have been processed this way, all of them have updated weight values. And all the routine has to do now is highlight the trim line with the highest weight value.

Because of this approach, trim lines no longer need to be exactly targeted by the mouse to light up and there is no absolute true or false hit flag anymore. All trim lines are evaluated and the one that has been closest to the moving mouse pointer on the moving boat during the past few computation rounds is selected.