我正在上一個學校項目,但不幸的是,我的導師不再工作了,所以我沒有人問。
目前,我正在設計一個小型系統,該系統應該能夠在現實世界中定位自己,它基本上是一臺安裝有PC的電動遙控車。這款車應該能夠在真實世界中導航並知道它在地圖上的位置。由於它需要一個很好的精度,所以GPS不是一種選擇(我能得到的最好距離是4米,就我能接受的範圍而言),並且以任何方式對車輪進行編碼對於我的預算來說都太昂貴,所以解決方法是將一個在這輛車下的鼠標,並將其反饋用作相對定位系統。
我的第一個想法是計算兩個瞬間(使用定時器)之間的像素距離差異,我甚至嘗試使用mouseMoved事件應用相同的原則,但問題仍然存在:如果我改變鼠標的速度,計算的距離也變化。
在這一點上,我沒有其他的想法,你認爲我的做法是錯誤的?JavaFx光標測量
public class Main extends Application {
public static void main(String args[]) {
launch(args);
}
private double cmToPixel = 1;
private int totalX;
private int totalY;
private Robot robot;
private int counter;
@Override
public void start(Stage primaryStage) throws Exception {
VBox pane = new VBox();
pane.setFillWidth(false);
pane.setMinWidth(200);
javafx.scene.control.TextArea textArea = new TextArea();
javafx.scene.control.TextArea logArea = new TextArea();
javafx.scene.control.TextArea debugArea = new TextArea();
pane.getChildren().addAll(textArea, logArea, debugArea);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.centerOnScreen();
robot = new Robot();
robot.mouseMove((int) (Screen.getPrimary().getBounds().getWidth()/2), (int) (Screen.getPrimary().getBounds().getHeight()/2));
scene.setOnMouseMoved(e -> {
double deltaX = e.getX() - Screen.getPrimary().getBounds().getWidth()/2;
double deltaY = Screen.getPrimary().getBounds().getHeight()/2 - e.getY() ;
totalX += deltaX;
totalY += deltaY;
textArea.appendText((totalX/cmToPixel) + " - " + (totalY/cmToPixel) + "\n");
debugArea.appendText(deltaX+" - "+deltaY+"\n");
logArea.appendText("Center: ["+(int) (Screen.getPrimary().getBounds().getWidth()/2)+";"+(int) (Screen.getPrimary().getBounds().getHeight()/2)+"] cursorPosition: "+e.getX()+" - "+e.getY()+"\n");
robot.mouseMove((int) (Screen.getPrimary().getBounds().getWidth()/2), (int) (Screen.getPrimary().getBounds().getHeight()/2));
});
primaryStage.show();
primaryStage.setFullScreen(true);
}}
非常感謝您的幫助!
利瑪竇Formenti
編輯:如果你想複製我的結果,只是標註在一張紙上兩條線,並嘗試以不同的速度運行這些線之間的鼠標,同時注意在程序
剛剛嘗試過你的建議,但沒有任何改變。你確定這可能是問題嗎?如果是這樣,我會調查更多 –
這至少是你的一個問題。當然,我還沒有看到更多。我想知道你想如何處理汽車的旋轉。 – mipa
旋轉由數字指南針處理,不太難;禁用加速後情況不會改變......我將在此期間深入研究此解決方案 –