The Coolest Trick: Frame of Reference

Just after we create the moon, we create the moon’s orbit, which is just an empty 3D object—similar to what we did back in Chapter 3, Project: Making an Avatar. Then we add the orbit to Earth:

 
var​ moon_orbit = ​new​ THREE.Object3D();
 
earth.add(moon_orbit);

Adding the moon_orbit to Earth like that means that it’s centered on Earth. And, no matter where Earth goes, this moon_orbit object stays with Earth.

It probably doesn’t seem like it, but this is a crazily important trick in 3D programming. So important, in fact, that it gets a fancy name: frame of reference. Our moon_orbit is a new frame of reference. To see the power of frame-of-reference thinking, we add the moon to the moon_orbit and move it ...

Get 3D Game Programming for Kids now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.