Flying RGB Being created with PShape

Hello folks!

Some code I dug up from my archives:

// Project: Flying RGB Being
// Author:  GLV
// Date:    2019-03-23
// Update:  2025-01-11

// Updates:
// 2018-09-29
// 2018-06-27
// 2025-01-05 Cleaned it up a bit for gallery post.

// References:
// https://en.wikipedia.org/wiki/Pendulum
// https://en.wikipedia.org/wiki/Pendulum_(mathematics)
// https://en.wikipedia.org/wiki/Gravity

// Global variables variables for now

float angle3;

public void settings()
  {
  size(500, 300, P3D);
  }

public void setup()
  {
  noStroke();
  colorMode(RGB, 1);
  }

public void draw()
  {
  background(0);
  translate(width/2, height/2-20, -20);

  //float yAngle = map(mouseX, 0, width, -TAU/4, TAU/4);
  //float xAngle = map(mouseY, 0, height, -TAU/4, TAU/4);
   
  //rotateZ(yAngle); 
  //rotateX(xAngle);
  
  translate(30, -20, 0);
  rotateX(-TAU/16);

 pushMatrix(); 
  rotateY(-TAU/4);
  rotateX(TAU/4);
  
  
  int div = 1000;
  //rotateZ((frameCount%div)*TAU/div);
  
  seq_01();
  RGBeingUpdate02();
 popMatrix();
 
  // For making movie 
  //if (angle3<360)
  //  saveFrame("/data/###.png");
  //if (angle3==359) 
  //  exit(); 
  }

//**************************************************************************************************
float thRGB1, thRGB2, thRGB3;
String s = "closed";
float scale = 40;

//**************************************************************************************************
//Starting from PI/16 and dropping!

float aVelocity1 = 0;
float aAcceleration1 = 0;

float angle1 = TAU/16;
float damping1 = 1;      //.999 works  //default MUST be 1 or it does not move.
float radRGB = 1;        //normalized for now

// Sine wave modulates
void RGBeingUpdate02()
  {
  //thRGB1 = angle1;
  thRGB1 = 0.5*sin(thRGB3);
  angle3 = int(degrees(thRGB3));
  println("angle2", angle3 );
  }

// Angular velocity modulates
void RGBeingUpdate01()
  {
float gravity = 1f/600;                                // as g increases it oscillates faster!
//radRGB = 1;                                          // pendulum; normalized for now
  float g = -1 * gravity/radRGB;

  // Formula we worked out for angular acceleration
  aAcceleration1 = g*sin(angle1);                      // y = rsinθ, x = rcosθ

  // Standard dt angular motion algorithm
  aVelocity1 += aAcceleration1;                        //aV = aV*dt
  angle1 += aVelocity1;                                //th = aV*dt

  //Apply some damping.
  aVelocity1 *= damping1;

  thRGB1 = angle1;
  }

//**************************************************************************************************

float x1, y1, z1;
float x2, y2, z2;
float x3, y3, z3;
float x4, y4, z4;
float x5, y5, z5;
float x6, y6, z6;

void seq_01()
  {
  scale(scale);          // Used vertices with 1 so had to scale; may rethink this late
  //noStroke();          // noStroke(); otherwise it scales default stroke
  RGBCube01();           // Creates RGB cube

  // Counters
  thRGB3 = thRGB3 + TWO_PI/(360);
  if (thRGB3 >= TWO_PI)
  thRGB3 = 0;
  }

void RGBCube01()
  {
  //top - head of RGBeing
  x2 = +1;                        // Rotate along this axis
  y2 = +1 + 2*cos(TAU/16);        // Pick furthest vertices to rotate
  z2 = -1 + 2*sin(TAU/16);

  //bottom
  x5 = +1;
  y5 = +1 + 2*cos(1*thRGB1);
  z5 = -1 + 2*sin(1*thRGB1);

  //right - right wing
  x3 = +1 + 2*cos(1*thRGB1);
  y3 = +1;
  z3 = -1 + 2*sin(1*thRGB1);

  //left - left wing
  x4 = +1 + 2*cos(1*thRGB1);
  y4 = +1;
  z4 = -1 + 2*sin(1*thRGB1);

  //front
  x1 = +1;
  y1 = -1 + 2*sin(-TAU/4-1*thRGB1);    //close  y = yo + rcos(theta), y is -ve because of y direction down, -2 or -theta same result
  z1 = +1 + 2*cos(-TAU/4-1*thRGB1);    //close  z = zo + rsin(theta)

  // back
  // No change!

  // top
 beginShape();
  fill(0, 0, 1);
  vertex(x2*-1, y2*-1, z2*+1);
  fill(1, 0, 1);
  vertex(x2*+1, y2*-1, z2*+1);
  fill(1, 0, 0);
  vertex(    1,    -1,     -1);
  fill(0, 0, 0);
  vertex(   -1,    -1, -   1);
 endShape(CLOSE);

  // bottom
 beginShape();
  fill(0, 1, 1);
  vertex(x5*-1, y5*+1, z5*+1);
  fill(1, 1, 1);
  vertex(x5*+1, y5*+1, z5*+1);
  fill(1, 1, 0);
  vertex(1, 1, -1);
  fill(0, 1, 0);
  vertex(-1, 1, -1);
 endShape(CLOSE);

  //right
 beginShape();
  fill(1, 1, 1);
  vertex(x3*+1, y3*+1, z3*+1);
  fill(1, 1, 0);
  vertex(   +1, +1, -1);
  fill(1, 0, 0);
  vertex(   +1, -1, -1);
  fill(1, 0, 1);
  vertex(x3*+1, y3*-1, z3*+1);
 endShape(CLOSE);

  //left
 beginShape();
  fill(0, 1, 0);
  vertex(   -1, 1, -1);
  fill(0, 1, 1);
  vertex(x4*-1, y4*+1, z4*+1);
  fill(0, 0, 1);
  vertex(x4*-1, y4*-1, z4*+1);
  fill(0, 0, 0);
  vertex(   -1, -1, -1);
 endShape(CLOSE);

  //back
 beginShape();
  fill(0, 1, 0);
  vertex(-1,  1, -1);
  fill(1, 1, 0);
  vertex( 1,  1, -1);
  fill(1, 0, 0);
  vertex( 1, -1, -1);
  fill(0, 0, 0);
  vertex(-1, -1, -1);
 endShape(CLOSE);

  // front2
 beginShape();
  fill(0, 1, 1);
  vertex(   -1,  1+y5-1,       1+z5-1);
  fill(1, 1, 1);
  vertex(    1,  1+y5-1,       1+z5-1);
  fill(1, 0, 1);
  vertex(x1*+1,  y1*-1+(y5-1), z1*+1+z5-1);
  fill(0, 0, 1);
  vertex(x1*-1,  y1*-1+(y5-1), z1*+1+z5-1);
 endShape(CLOSE);
  }
  
void mousePressed()
  {
  saveFrame("test.png");
  }

RGB Being

This code makes a good case for why it is important to comment!
Comments reflect what I did at the time and may need to be scrutinized and updated.

Have fun!

:)

2 Likes