我有一个草图,我在这里模拟重力:
float acceleration;
float velocity;
float location;
int time= millis();
boolean done = false;
void setup() {
size(200, 200);
acceleration = 0;
velocity = 0;
location = 0;
frameRate(30);
}
void draw() {
background(0);
fill(255);
acceleration = acceleration + .1;
gravity();
ellipse(10, location, 10, 10);
if (location > height && !done) {
float finished = millis() - time;
println(finished/1000.0);
done = true;
}
}
void gravity() {
velocity += acceleration * (1/frameRate);
location += velocity;
acceleration = 0;
}
我想我可以简单地将加速度乘以1 / frameRate,以使模拟运行相同,无论帧速率如何,但它不起作用。如何模拟不依赖于帧速率的重力或力?
答案 0 :(得分:0)
您需要将当前速度设为时间函数,而不是数字 画的框架。
之类的东西long start = <now>;
position = 0;
do { position = functionof(<now>-start); repaint(); sleep(10); }
然后实际重绘,无论何时发生,都使用当前位置。