Life as Clay

Orbital motion is a pretty flower

leave a comment »


I was tired last night and therefore unable to spend the time thinking about what I really wanted to achieve. I tackled the problem this morning — I didn’t end up with exactly what I wanted (that will take more work), but I did end up with a functioning orbital system. The pathway shown here is similar to what the path of an object that is orbiting another object that is orbiting a larger object would take. In other words, this should look similar to what the path that the moon takes around the sun looks like (since the moon orbits Earth..) but with a slower period.

simple moon orbit

But, hey… why stick with boring white when you can display colors? Here are a few variations, in color:

color orbit 1

color orbit 2You may notice what looks like a hollow core on the last one. I followed the orbit of the colored ball with the orbit of a smaller black ball and it left the trail. Incidentally, the trail is visible because the transparency of the colored ball is at 40% or so.

The fun part is changing the orbital period…

the whole system

faster periodfast color periodHere’s the code that I used for the last one. Click here to see it in motion.

int centerX = 200;
int centerY = 200;
float ballX;
float ballY;
int ballRadius = 20;
int radius = 100;

// For the orbital period of the "Earth"
float angle = 0;
float speed = 0.01;

// For the orbital period of the "moon"
float passedAngle = 0;
float passedSpeed = 0.3;

void setup() {
  size(400,400);
  background(0);
  fill(255);
  stroke(255);
  smooth();
  frameRate(60);
}

void draw(){

  // Determines the orbiting position of the "Earth"
  ballX = centerX + sin(angle) * radius;
  ballY = centerY + cos(angle) * radius;

  // Calls the function to create the "Earth" and "Moon"
  drawAtom(ballX,ballY,ballRadius,passedAngle);
  // drawAtom(ballY,ballX + 50,ballRadius);

  angle += speed;
  passedAngle += passedSpeed;

}

void drawAtom(float ballX, float ballY, int radius, float angle) {
  float newBallX;
  float newBallY;

  newBallX = ballX + sin(angle) * radius;
  newBallY = ballY + cos(angle) * radius;

  // Creates the invisible "Earth"
  noStroke();
  noFill();
  ellipse(ballX,ballY,1,1);

  // Creates the "moon"
  fill(random(255),random(255),random(255),random(255));
  ellipse(newBallX, newBallY,10,10);

  // Try adding another "moon"!

}
Advertisements

Written by Clay

October 30, 2009 at 14:07

Posted in Code, Processing

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: