Last active
February 5, 2019 17:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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