使用 Javafx AnimationTimer 时如何获得瞬时 fps 速率
How to get the instantaneous fps rate when using Javafx AnimationTimer
我正在制作一个根据物理定律运行的游戏:即 F = ma。游戏中的物体,根据它们所受的力改变它们的速度和位置。
我正在使用 AnimationTimer class。
游戏是二维的。
我有一个 class 球:
public class Ball extends javafx.scene.shape.Circle {
int m; //mass
int vX, vY; // velocity componenets
void feelForce (int forceX, int forceY) {
vX += forceX * t/m;
vY += forceY * t/m;
setCenterX (getCenterX() + vX * t);
setCenterY (getCenterY() + vY * t);
}
}
我面临的问题是我不知道如何定义t。每秒帧数在整个运行时可能会有所不同,我想知道如何找到该值,每次调用 feelforce()
。
public void start(Stage stage) throws Exception {
//have overridden the constructor in class Ball. Had not shown it to keep it short
Ball player = new Ball(400.0, 300.0, 30.0, Color.TOMATO);
Pane main = new Pane(player);
Scene scene = new Scene(main);
stage.setScene(scene);
stage.show();
AnimationTimer gamePlay = new AnimationTimer() {
public void handle(long now){
player.feelForce(Math.random() * 10, Math.random() * 10);
// I use MouseEvents to get force values, but have used random numbers here, for brevity
}
};
gamePlay.start();
}
如果这不是 possible/efficient 使用 AnimationTimer,使用另一个 class 的解决方案也将不胜感激。
P.S:我使用 AnimationTimer 而不是 Timeline 的原因是不需要固定速率。
传递给handle(...)
方法的值是一个时间戳,以纳秒为单位。因此,您可以计算自上次调用 handle(...)
以来经过的时间量:
AnimationTimer gamePlay = new AnimationTimer() {
private long lastUpdate = -1 ;
public void handle(long now){
if (lastUpdate == -1) {
lastUpdate = now ;
return ;
}
double elapsedSeconds = (now - lastUpdate) / 1_000_000_000.0 ;
lastUpdate = now ;
player.feelForce(Math.random() * 10, Math.random() * 10, elapsedSeconds);
// I use MouseEvents to get force values, but have used random numbers here, for brevity
}
};
然后相应地更新 feelForce(...)
方法:
public class Ball extends javafx.scene.shape.Circle {
int m; //mass
int vX, vY; // velocity componenets
void feelForce (int forceX, int forceY, double t) {
vX += forceX * t/m;
vY += forceY * t/m;
setCenterX (getCenterX() + vX * t);
setCenterY (getCenterY() + vY * t);
}
}
我正在制作一个根据物理定律运行的游戏:即 F = ma。游戏中的物体,根据它们所受的力改变它们的速度和位置。
我正在使用 AnimationTimer class。
游戏是二维的。
我有一个 class 球:
public class Ball extends javafx.scene.shape.Circle {
int m; //mass
int vX, vY; // velocity componenets
void feelForce (int forceX, int forceY) {
vX += forceX * t/m;
vY += forceY * t/m;
setCenterX (getCenterX() + vX * t);
setCenterY (getCenterY() + vY * t);
}
}
我面临的问题是我不知道如何定义t。每秒帧数在整个运行时可能会有所不同,我想知道如何找到该值,每次调用 feelforce()
。
public void start(Stage stage) throws Exception {
//have overridden the constructor in class Ball. Had not shown it to keep it short
Ball player = new Ball(400.0, 300.0, 30.0, Color.TOMATO);
Pane main = new Pane(player);
Scene scene = new Scene(main);
stage.setScene(scene);
stage.show();
AnimationTimer gamePlay = new AnimationTimer() {
public void handle(long now){
player.feelForce(Math.random() * 10, Math.random() * 10);
// I use MouseEvents to get force values, but have used random numbers here, for brevity
}
};
gamePlay.start();
}
如果这不是 possible/efficient 使用 AnimationTimer,使用另一个 class 的解决方案也将不胜感激。
P.S:我使用 AnimationTimer 而不是 Timeline 的原因是不需要固定速率。
传递给handle(...)
方法的值是一个时间戳,以纳秒为单位。因此,您可以计算自上次调用 handle(...)
以来经过的时间量:
AnimationTimer gamePlay = new AnimationTimer() {
private long lastUpdate = -1 ;
public void handle(long now){
if (lastUpdate == -1) {
lastUpdate = now ;
return ;
}
double elapsedSeconds = (now - lastUpdate) / 1_000_000_000.0 ;
lastUpdate = now ;
player.feelForce(Math.random() * 10, Math.random() * 10, elapsedSeconds);
// I use MouseEvents to get force values, but have used random numbers here, for brevity
}
};
然后相应地更新 feelForce(...)
方法:
public class Ball extends javafx.scene.shape.Circle {
int m; //mass
int vX, vY; // velocity componenets
void feelForce (int forceX, int forceY, double t) {
vX += forceX * t/m;
vY += forceY * t/m;
setCenterX (getCenterX() + vX * t);
setCenterY (getCenterY() + vY * t);
}
}