Camera moves by itself

I want the camera move by itself from given location to certain location at the beginning of the game and when it reaches certain location it gets fixed on that location.
Is it possible to make it?

yes it’s very possible, cam.setLocation (Vector3f) is your friend here. I assume you want to move it incrementally each frame, so look at FastMath.interpolateLinear(). There’s many examples of this scattered around, including some other ways of doing it

@wezrule
I have startscreen and hudscreen. I have put FastMath.interpolateLinear() with start vector and end vector position inside hudscreen initialize() method so that I expected that It would do some animation moving from start vector to end vector when user switch to hudscreen from startscreen. It didn’t happen though, am I missing the point?

yes you are, you have to update the scale variable each frame in the update loop.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_main_event_loop

I didn’t test this btw:
[java]
private boolean shouldMove = true;
private float count = 0;
private Vector3f start = cam.getLocation ().clone (); // cloned so that you don’t modify it by accident
private Vector3f end = cam.getLocation ().add (new Vector3f (4, 0, 0)); // Move 4 units in X direction in world space

public void simpleUpdate (float tpf) {
if (shouldMove) {
count += tpf * 0.1; //this will make it take 10 seconds to reach the end, from the the start
Vector3f newLocation = FastMath.interpolateLinear(count, start, end);
cam.setLocation (newLocation);
if(count>= 1) //then you reached the end
shouldMove= false;
}
}
}[/java]

Accumulate the scale variable using tpf (optionally multiplied by some factor, depending how long you want it to last). When the scale reaches 1 you have reached your destination. It shouldn’t extrapolate it passed that point even if the scale (count) is > 1, but I added a check just to be sure.

1 Like

@wezrule
Appreciate it. It worked.