Marking Earth

The following program is similar to the previous one but for a few differences. First, it includes a red tether connecting the sun’s and earth’s centers. Second, whenever you click the Go button, a marker is planted on earth’s surface where it intersects this tether at click time. Lastly, I’ve include an earth camera for a second view of our favorite spinning planet.

Marked Earth

We’re preparing for the next page which visualizes the Sun’s helical path over Earth’s surface. To that end, we first place simple markers (spheres) along this path. Whenever you click the Go button, this function gets called:

function () {
    let p = markEarth(earth, orbitRad, earthRad);
    let args = {color: getRandomColor()};
    earth.add(createSphere(p, args));
}

The call to markEarth on line 2 returns the marker’s position p on earth’s surface in earth’s local coordinate system. Here earth is the mesh representing Earth. Adding a new sphere to this mesh (line 4) at position p locates it in earth’s local space. (Recall that Mesh is a subclass of Object3D; it is a type of scene graph node and as such can have its own child nodes defined in its coordinate system.)

Function markEarth takes three inputs: the earth, the radius of earth’s orbit, and the earth’s radius. It returns the marker’s position in the earth’s local coordinate system.

function markEarth(earth, orbitRad, earthRad) {
    let earth_world_pos = earth.localToWorld(new THREE.Vector3(0, 0, 0));
    let s = 1 - (earthRad / orbitRad);
    let marker_world_pos = earth_world_pos.multiplyScalar(s);
    let marker_local_pos = earth.worldToLocal(marker_world_pos);
    return marker_local_pos;
}

Function markEarth performs a sequence of conversions between coordinate systems using methods defined in the Object3D class which our earth object inherits. Operation of the function by line number:

[line 2] Obtain the position of earth’s center in world space. This maps earth’s center in local coordinates (given by (0,0,0)) to world coordinates.

[line 3] Consider the line segment connecting the sun’s center to the earth’s center. Here we compute the fraction s along this line segment at which it intersects the earth’s surface.

[line 4] We compute the marker’s position in world coordinates. Since the sun is positioned at the origin of world space, the coordinates of every point in world space defines a vector from the sun to that point. So to obtain the marker’s position in world space, we scale (shorten) the vector from the sun to the earth by factor s.

[line 5] We ask the earth to convert the marker’s world coordinates to the earth’s own local coordinates.