Skip to content

Instantly share code, notes, and snippets.

@cdfisher
Created October 28, 2025 20:02
Show Gist options
  • Select an option

  • Save cdfisher/5386ff1094c7a2347c99c0708db67cf5 to your computer and use it in GitHub Desktop.

Select an option

Save cdfisher/5386ff1094c7a2347c99c0708db67cf5 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class Orbiter : MonoBehaviour
{
public GameObject Sun;
public GameObject Earth;
public GameObject Moon;
private Vector3 sunPos;
private Vector3 earthPos;
private Vector3 moonPos;
private Vector3 earthPosOld;
private Matrix4x4 earthOrbitM;
private Matrix4x4 earthRotM;
private Matrix4x4 earthTransM;
private Matrix4x4 moonRotM;
private Matrix4x4 moonTransM;
private Matrix4x4 moonOrbitM;
private int earthDist = 10;
private int earthSpeed = 40;
private float earthAngle = 0f; // theta
private int moonDist = 4;
private int moonSpeed = 100;
private float moonAngle = 0f; // beta
/*
* Rotation about Y axis
*
* x' [ cos theta 0 sin theta 0 ] [x]
* y' = [ 0 1 0 0 ] [y]
* z' [-sin theta 0 cos theta 0 ] [z]
* 1 [ 0 0 0 1 ] [1]
*
* Translation
*
* x' [ 1 0 0 tx] [x]
* y' = [ 0 1 0 ty] [y]
* z' [ 0 0 1 tz] [z]
* 1 [ 0 0 0 1] [1]
*
* */
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
sunPos = Sun.transform.position;
// set up matrices
//earthTransform = Matrix4x4.Rotate(Quaternion.Euler(0, earthSpeed, 0));
earthRotM = Matrix4x4.Rotate(Quaternion.Euler(0, earthSpeed, 0));
//Earth.transform.position = new Vector3(0, 0, earthDist);
//Earth.transform.position = Matrix4x4.Translate(new Vector3(0, 0, earthDist)) * new Vector4(0, 0, 0, 1);
//Moon.transform.position = Matrix4x4.Translate(new Vector3(0, 0, moonDist)) * Matrix4x4.Translate(new Vector3(0, 0, earthDist)) * new Vector4(0, 0, 0, 1);
}
void Update()
{
earthAngle += earthSpeed * Time.deltaTime;
moonAngle += moonSpeed * Time.deltaTime;
// set new earth position
/*earthPos = Earth.transform.position;
earthPosOld = earthPos;
earthTransM = Matrix4x4.Translate(earthPos);
earthPos = earthRotM * earthTransM * earthPos;
Earth.transform.position = earthPos;*/
earthTransM = Matrix4x4.Translate(new Vector3(0, 0, earthDist));
earthRotM = Matrix4x4.Rotate(Quaternion.Euler(0, earthAngle, 0));
earthOrbitM = earthRotM * earthTransM;
//earthPos = earthRotM * earthTransM * new Vector4(0, 0, 0, 1);
earthPos = earthOrbitM * new Vector4(0, 0, 0, 1);
//Earth.transform.position = earthPos;
// set new moon position based on new earth position
//Moon.transform.Translate(new Vector3(-earthPosOld.x, 0, -earthPosOld.z));
//Moon.transform.position = Matrix4x4.Rotate(Quaternion.Euler(0, moonSpeed, 0)) * Moon.transform.position;
//Moon.transform.Translate(new Vector3(earthPos.x, 0, earthPos.z));
/* moonTransM = Matrix4x4.Translate(new Vector3(0, 0, moonDist));
moonRotM = Matrix4x4.Rotate(Quaternion.Euler(0, moonAngle, 0));
Moon.transform.position = Matrix4x4.Translate(earthPos) * moonRotM * moonTransM * new Vector4(0, 0, 0, 1);*/
moonTransM = Matrix4x4.Translate(new Vector3(0, 0, moonDist));
moonRotM = Matrix4x4.Rotate(Quaternion.Euler(0, moonAngle, 0));
moonOrbitM = moonRotM * moonTransM;
moonPos = Matrix4x4.Translate(earthPos) * moonOrbitM * new Vector4(0, 0, 0, 1);
Earth.transform.position = earthPos;
Moon.transform.position = moonPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment