Fine tuning the Earth

If you're a space geek, you might be thinking that there are a few things we could do to our Earth model. For one, we should add the night view texture. (Mars and the other planets don't need one because their cities shut off all their lights at night.) Also, the Earth is slightly tilted on its axis. We can fix that.

The night texture

First, let's add the night texture. To do this, let's make an Earth Java class a subclass of a Planet. Right-click on your Java solarsystem folder, select New | Java Class, and name it Earth. Then, start defining it like this:

public class Earth extends Planet {

    public Earth(float distance, float radius, float rotation, float orbit, int texId, int nightTexId, Transform origin) {
        super(distance, radius, rotation, orbit, origin);
        transform.addComponent(new Sphere(texId, nightTexId));
    }
}

This requires that we add a new constructor to the Planet class, which omits texId, since the Earth constructor creates the new Sphere component, this time with two textures, textId and nightTexId.

In Planet.java, add the following code:

    public Planet(float distance, float radius, float rotation, float orbit, Transform origin){
        setupPlanet(distance, radius, rotation, orbit, origin);
    }

Now, in MainActivity, let's create an Earth separately from the other planets. In setupPlanets, modify the loop to handle this case:

        for(int i = 0; i < distances.length; i++){
            if (i == 2) {
                planets[i] = new Earth(
                        fudged_distances[i] * DISTANCE_FACTOR,
                        radii[i] * SCALE_FACTOR,
                        rotations[i] * DEG_PER_EHOUR,
                        orbits[i] * DEG_PER_EYEAR * fudged_distances[i] / distances[i],
                        texIds[i],
                        R.drawable.earth_night_tex,
                        origin);
            } else {
                planets[i] = new Planet(

Axis tilt and wobble

Among all its greatness, like all nature and mankind, the Earth is not perfect. In this case, we're talking about tilt and wobble. The Earth's axis of rotation is not exactly perpendicular to the orbital plane. It also suffers from a slight wobble as it rotates. We can show this in our virtual model.

Modify the Earth class constructor to read as follows:

    Transform wobble;

    public Earth(float distance, float radius, float rotation, float orbit, int texId, int nightTexId, Transform origin) {
        super(distance, radius, rotation, orbit, origin);

        wobble = new Transform()
                .setLocalPosition(distance, 0, 0)
                .setParent(orbitTransform, false);
        
        Transform tilt = new Transform()
                .setLocalRotation(-23.4f,0,0)
                .setParent(wobble, false);
        
        transform
                .setParent(tilt, false)
                .setLocalPosition(0,0,0)
                .addComponent(new Sphere(texId, nightTexId));
    }

Now, the Earth's rotation on each frame is against this wobble transform, so give Earth its own preDraw method, as follows:

    public void preDraw(float dt){
        orbitTransform.rotate(0, dt * orbit, 0);
        wobble.rotate(0, dt * 5, 0);
        transform.rotate(0, dt * -rotation, 0);
    }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset