Changing ImageView position based on angle of rotation

Game Development Asked on January 3, 2022

I am trying to make an ImageView change its position based on the direction it is facing.

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class Demo extends Application {

    public void start(Stage window) {
        window.setTitle("Changing ImageView position based on angle of rotation");

        int speed = 10; // You can modify the speed here

        ImageView imageView = new ImageView(new Image(""));

        imageView.setRotate(65); // This is where you can change the angle of rotation

        Group root = new Group();
        Scene scene = new Scene(root);

        final long[] lastNanoTime = {System.nanoTime()};

        new AnimationTimer() {
            public void handle(long currentTime) {
                double elapsedTime = (currentTime - lastNanoTime[0]) / 1000000000.0;
                lastNanoTime[0] = currentTime;

                imageView.setX(imageView.getX() + Math.sin(Math.toRadians(imageView.getRotate())) * speed * elapsedTime);
                imageView.setY(imageView.getY() + Math.cos(Math.toRadians(imageView.getRotate())) * speed * elapsedTime);


    public static void main(String[] args) {

The above code is based on the code I am using. You can adjust the degrees accordingly to see the issue. The ImageView does not move in the direction it is facing. Note: I found a random .png file online for the purpose of this demo. You can substitute with a different image in you own code in order to better visualize the problem.

The issue is, the ImageView does not move in the direction it is facing. When oriented vertically (0° or 180°) the ImageView moves backward, but when oriented horizontally (90° or 270°) it moves straight ahead like it should. When oriented diagonally, it moves somewhere in between.

What am I doing wrong and how do I fix it?

One Answer

The question was resolved on Stack Overflow. The y axis is inverted in JavaFX, so I need to use -cos(θ) instead.

Answered by qwerty on January 3, 2022

Add your own answers!

Related Questions

Convert a string to a gameobject name

1  Asked on December 1, 2021 by stormzy18


Jumping up quicker? (Paper Mario style jump)

1  Asked on December 1, 2021 by spottel


A* Pathfinding Finding One of Many Goals

1  Asked on November 29, 2021 by user6256


Wheel rotation for a raycast car in Unity?

1  Asked on November 29, 2021 by john-545


Unity “scrape away” effect

0  Asked on November 24, 2021 by darren-swords


Initial setting of camera and ppu to match playing table

1  Asked on November 21, 2021 by zibelas


LibGDX – Tilemap Wall Collision Detection Issues

2  Asked on November 20, 2021 by james-stoddern


Tilemaps not showing up on Android build

1  Asked on November 18, 2021 by jcorretjer


Unity: Sprite stretch issue

1  Asked on November 14, 2021 by tikeb


Unity UI, how to hide on play

2  Asked on November 12, 2021


Alternative path not working

1  Asked on November 12, 2021 by dsngl-tibor


Unity – Understanding the SRP batcher

2  Asked on November 12, 2021 by majs


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir