Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active December 29, 2024 23:20
Show Gist options
  • Save KrabCode/b13ddc424474fd642b51aeab35f665f0 to your computer and use it in GitHub Desktop.
Save KrabCode/b13ddc424474fd642b51aeab35f665f0 to your computer and use it in GitHub Desktop.
Install on Android with APDE.
float sunSize = 100;
float planetRadiusStep = 180;
float planetSize = 45;
int count = 4;
int brightColor;
int darkColor;
int orbitColor;
int bgColor;
ArrayList<Float> offsets = new ArrayList<Float>();
float mouseOffset = 0;
float mouseSpeed = 0;
float mouseSpeedTarget = radians(0.1);
float mouseSpeedLimit = radians(5);
void setup() {
fullScreen(P3D);
smooth(4);
brightColor = color(200);
darkColor = color(50);
orbitColor = color(100);
bgColor = color(12);
}
void draw() {
// orbit gap ellipse with P3D requires this hint()
hint(DISABLE_OPTIMIZED_STROKE);
mouseSpeed = constrain(mouseSpeed,
-mouseSpeedLimit,
+mouseSpeedLimit
);
mouseSpeed = lerp(mouseSpeed,
mouseSpeedTarget,
0.05);
mouseOffset += mouseSpeed;
background(bgColor);
noStroke();
blendMode(BLEND);
strokeWeight(5);
strokeCap(SQUARE);
translate(width*.5, height*.5);
noStroke();
fill(brightColor);
ellipse(0, 0, sunSize, sunSize);
for (int i = 0; i < count; i++) {
float r = sunSize + planetRadiusStep*1.5 + i * planetRadiusStep;
float theta = (mouseOffset + offset(i));
float x = r * .5 * cos(theta);
float y = r * .5 * sin(theta);
stroke(orbitColor);
noFill();
ellipse(0, 0, r, r); // orbit circle
pushMatrix();
translate(x, y);
rotate(theta);
noStroke();
fill(bgColor); // orbit gap
ellipse(0, 0, planetSize * 2, planetSize * 2);
fill(brightColor);
arc(0, 0, planetSize, planetSize, HALF_PI, HALF_PI+PI);
fill(darkColor);
arc(0, 0, planetSize, planetSize, -HALF_PI, HALF_PI);
popMatrix();
}
}
float offset(int index) {
if (index >= offsets.size()) {
offsets.add(random(75, 150));
}
return offsets.get(index);
}
void mouseDragged() {
float cx = width /2;
float cy = height /2;
float thisAngle = atan2(cy-mouseY, cx-mouseX);
float prevAngle = atan2(cy-pmouseY, cx-pmouseX);
float angleDelta = thisAngle - prevAngle;
angleDelta = angleModulo(angleDelta);
if (abs(angleDelta) > abs(mouseSpeed)) {
mouseSpeed = lerp(angleDelta, mouseSpeed, 0.5);
}
}
float angleModulo(float x) {
if (x > 0) {
x = TAU - (x%TAU);
} else {
x = x % TAU;
}
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment