JavaFX 3D Tutorial #9 – Moving Light Animation

JavaFX 3D Moving lights

In the last chapter, we saw how to add lights to the 3D scene. In this chapter, we will see how a moving light works.

You can add as many lights as you need. To animate the light sources, you can use predefined animations from javafx.animation package like TranslateTransition, RotateTransition etc or custom animations can be applied. In this example, I will write a custom rotation animation to get the following effect.

JavaFX Light Animation

Moving Light Animation

We can create the rotation effect for the light source using rotateProperty. The important thing here is to specify the axis of rotation. We have the freedom to rotate the object in 360 degrees. In this example, I am rotating light source about X Axis.

AnimationTimer will be called during each frame rendered. We can update the transform properties of objects to create smooth animation. In this example, I have increased rotation angle of light source by 1 degree during each frame.

You can speedup the animation by increasing the value ‘1’ to a higher one. Animation speed can be reduced by reducing the value.

private Node[] prepareLightSource() {
    //Create point light
	pointLight.getTransforms().add(new Translate(0, -50, 100));
	//Set axis of rotation

	//Create a small sphere to locate the light source (Light source is invisible)
	Sphere sphere = new Sphere(2);
	//Attach sphere transforms to point light. So they move/rotate together

	//Return lights
	return new Node[]{pointLight, sphere};

public void start(Stage primaryStage) {
	Box box = prepareBox();

	SmartGroup group = new SmartGroup();
	//Add light and sphere

	Camera camera = new PerspectiveCamera(true);

	Scene scene = new Scene(group, WIDTH, HEIGHT);


	initMouseControl(group, scene, primaryStage);

	primaryStage.setTitle("Genuine Coder");

	//Create animation timer
	AnimationTimer timer = new AnimationTimer() {
	  public void handle(long now) {
		//Increase rotation angle by 1 degree during each frame.
		pointLight.setRotate(pointLight.getRotate() + 1);
	//Start animation automatically during startup


Visit JavaFX 3D Course Index Page

Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.