Skip to content

Instantly share code, notes, and snippets.

@Morpholux
Last active February 5, 2019 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Morpholux/cd08b9ddb9a72d612144627d5aa6192b to your computer and use it in GitHub Desktop.
Save Morpholux/cd08b9ddb9a72d612144627d5aa6192b to your computer and use it in GitHub Desktop.
Use of PVector to make an object rotate at constant speed around an origin point
// Constant circular orbit
PVector pos, zAxis;
float radius = 200, force = 10;
void setup() {
size(600, 600);
background(0);
noStroke();
pos = new PVector(radius, 0);
zAxis = new PVector(0, 0, -1); // for clockwise direction
// (0, 0, 1) for counter clockwise
}
void draw() {
background(0);
translate(width/2, height/2);
// origin
fill(100);
ellipse(0, 0, 10, 10);
// calculate tangential force at every new position
PVector tangent = pos.cross( zAxis );
tangent.normalize();
tangent.mult(force); // determine the speed of rotation
// update of position with tangential force
pos.add(tangent);
// remap position to radius of orbit
pos.normalize();
pos.mult(radius);
// drawing the rotating object
fill(255, 0, 0);
ellipse(pos.x, pos.y, 10, 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment