Creating a Planet class

As we build our Solar System, it will be useful to abstract out a Planet class to be used for each planet.

Planets have a number of different attributes that define their unique characteristics in addition to their texture resource IDs. Planets have a distance from the Sun, size (radius), and an orbital speed. Planets all orbit around the Sun as their origin.

  • The distance will be its distance from the Sun measured in millions of kilometers.
  • The radius will be the planet's size in kilometers (actually in millions of kilometers, to be consistent).
  • Rotation is the rate at which the planet rotates about its own axis (one of its days).
  • Orbit is the rate at which the planet rotates about the Sun (one of its years). We will assume a perfectly circular orbit.
  • TexId is the resource ID of the texture image for the planet.
  • origin is the center of its orbit. For planets, this will be the Sun's transform. For a moon, this will be the moon's planet.

The Solar System is a really big thing. The distances and radii are measured in millions of kilometers. The planets are really far apart and relatively small compared to the size of their orbits. The rotation and orbit values are relative rates. You'll note that we'll normalize them to 10 seconds per Earth day.

From these attributes, a planet maintains two transforms: one transform for the planet itself and another transform that describes its location in orbit. In this way, we can rotate each planet's separate parent transform which, when the planet is at a local position whose magnitude is equal to the orbital radius, causes the planet to move in a circular pattern. Then we can rotate the planet itself using its transform.

For the Moon, we'll also use the Planet class (yeah, I know, maybe we should have named it HeavenlyBody?) but set its origin as the Earth. The moon does not rotate.

In your app (for example, app/java/com/cardbookvr/solarsystem/), create a Java class and name it Planet. Add variables for its attributes (distance, radius, rotation, orbit, orbitTransform, and transform), as follows:

public class Planet {
    protected float rotation, orbit;
    protected Transform orbitTransform, transform;

    public float distance, radius;

Define a constructor that takes the planet's attribute values, initializes the variables, and calculates the initial transforms:

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

    public void setupPlanet(float distance, float radius, float rotation, float orbit, Transform origin){
        this.distance = distance;
        this.radius = radius;
        this.rotation = rotation;
        this.orbit = orbit;
        this.orbitTransform = new Transform();
        this.orbitTransform.setParent(origin, false);
        
        transform = new Transform()
            .setParent(orbitTransform, false)
            .setLocalPosition(distance, 0, 0)
            .setLocalRotation(180, 0, 0)
            .setLocalScale(radius, radius, radius);
    }

The constructor generates an initial transform for the planet and adds a Sphere component with the given texture.

On each new frame, we will update the orbitTransform rotation around the Sun (year) and the planet's rotation about its own axis (day):

    public void preDraw(float dt){
        orbitTransform.rotate(0, dt * orbit, 0);
        transform.rotate(0, dt * -rotation, 0);
    }

We can also provide a couple of accessor methods for the Planet class's transforms:

    public Transform getTransform() { return transform; }
    public Transform getOrbitransform() { return orbitTransform; }

Now, let's take a look at the geometry of our Solar System.

..................Content has been hidden....................

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