K
K
krembrule20162019-01-25 11:15:09
Java
krembrule2016, 2019-01-25 11:15:09

How is a smoothly moving camera implemented in 3D space?

Hello! Happy Friday everyone!
The question arose - how to implement a smoothly moving camera in 3D space, which would be controlled by the navigation keys?
Until recently, I used this code and was satisfied.

scene.setOnKeyReleased(e -> codes.clear()); // очищаем коллекцию, когда отпускаем кнопку
        scene.setOnKeyPressed(e -> {
            if(acceptedKey.contains(e.getCode()))
            {
                codes.add(e.getCode());
                if(codes.contains(KeyCode.UP))
                {
                    ourcamera.setTranslateZ(ourcamera.getTranslateZ()+1);
                }
                if(codes.contains(KeyCode.DOWN))
                {
                    ourcamera.setTranslateZ(ourcamera.getTranslateZ()-1);
                }
                if(codes.contains(KeyCode.RIGHT))
                {
                    ourcamera.setTranslateX(ourcamera.getTranslateX()+1);
                }
                if(codes.contains(KeyCode.LEFT))
                {
                    ourcamera.setTranslateX(ourcamera.getTranslateX()-1);
                }
            }
        });

This approach has disadvantages - if we increase the coordinate by more than 1, then the camera will jump.
Then I thought I could try TranslateTransition(). I will not show the implementation with this canoe, because I think this is the wrong approach and the floating camera is implemented somehow differently. But there the trick is that you need to use stop() to start the next animation, which means that the camera movement between different types of animations will be interrupted.
In general, what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EVGENY T., 2019-01-25
@krembrule2016

You will have to introduce concepts: speed / acceleration of the camera, friction force. Plus physics for the 6th grade. Then the camera will smoothly accelerate and slow down.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question