I was given a processing.js example of how to orbit an object around another object by a user on gamedev.stackexchange here.
Essentially, in the example, an origin or center is established along with the cooridinates of the orbiting point:
// Logic vars
PVector origin;
float orbitRadius = 75.0;
PVector orbitVelocity;
PVector orbitPoint;
float t = 0.0;
float fps = 60.0;
float constDT = 1 / fps;
Then, they are initialized in the setup:
void setup() {
size(400, 400);
frameRate(fps);
/*central point */
origin = new PVector(width / 2.0, height / 2.0);
/*first point and velocity of orb around center*/
orbitVelocity = new PVector(0.0, /*-orbitRadius*/orbitRadius);
orbitPoint = new PVector(origin.x + orbitRadius, origin.y);
}
And then the cooridinates of the orbiting point are updated each time step:
void update(float dt) {
/*update orbit of first orb using velocity*/
PVector accelerationTowardsOrigin = PVector.sub(origin, orbitPoint);
orbitVelocity.add(PVector.mult(accelerationTowardsOrigin, /*dt*/constDT));
orbitPoint.add(PVector.mult(orbitVelocity, /*dt*/constDT));
}
(I have omitted the drawing code as it is not relevant.) I have been attempting to update the code to add additional orbiting ellipses, which can be seen here. I have been able to successfully add a second point along orbit around the central point by adding these additional variables
/*Logic vars for second point on circle*/
PVector orbitPoint2;
PVector orbitVelocity2;
float circAngle;
float xcoord_along_circle;
float ycoord_along_circle;
and adding this to setup:
//Added in setup
/*second orbiting point for second orb*/
/*Calculate second point along circle*/
circAngle = 90.0;
xcoord_along_circle = orbitRadius * cos(circAngle) + origin.x;
ycoord_along_circle = orbitRadius * sin(circAngle) + origin.y;
/*second point and velocity of orb around center*/
orbitVelocity2 = new PVector(xcoord_along_circle,orbitRadius);
orbitPoint2 = new PVector(xcoord_along_circle, ycoord_along_circle);
However, when I update the cooridinates of the second ellipse each point it results in a stretched out, elongated orbit:
/*update orbit of second orb using velocity*/
PVector accelerationTowardsOrigin2 = PVector.sub(origin,orbitPoint2);
orbitVelocity2.add(PVector.mult(accelerationTowardsOrigin2,/*dt*/constDT));
orbitPoint2.add(PVector.mult(orbitVelocity2, /*dt*/constDT));
I believe I am doing everything correctly, and replicating the necessary steps. How can I correct this so that the orbit is not distorted?