我在 gamedev.stackexchange 上获得了一个 processing.js 示例,说明如何让一个对象围绕另一个对象运行。
本质上,在示例中,原点或中心与轨道点的坐标一起建立:
// Logic vars
PVector origin;
float orbitRadius = 75.0;
PVector orbitVelocity;
PVector orbitPoint;
float t = 0.0;
float fps = 60.0;
float constDT = 1 / fps;
然后,它们在设置中初始化:
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);
}
然后每个时间步更新轨道点的坐标:
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));
}
(我省略了绘图代码,因为它不相关。)我一直在尝试更新代码以添加额外的轨道椭圆,可以在这里看到。通过添加这些附加变量,我已经能够成功地沿围绕中心点的轨道添加第二个点
/*Logic vars for second point on circle*/
PVector orbitPoint2;
PVector orbitVelocity2;
float circAngle;
float xcoord_along_circle;
float ycoord_along_circle;
并将其添加到设置中:
//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);
然而,当我更新第二个椭圆每个点的坐标时,它会导致一个延伸的、拉长的轨道:
/*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));
我相信我做的一切都是正确的,并复制了必要的步骤。我怎样才能纠正这个问题,这样轨道才不会扭曲?