Move object smoothly from Vector3 a to Vector3b

Hey guys, I was wondering if there is a built in feature that I could use to move objects smoothly from one location to an other (Start slowly then speed up, before reaching point B slow down again)

If there isn’t, can someone point me in the right direction where I should start reading about this

Regards

One approach, use linear interpolation of the positions but feed them with a sin/cos function to get a nice curve.

In pseudo code, decide how long you want it to take (2 seconds, say)…
Then every frame:
[java]
// Accumulate the current time (started at 0)
time += tpf;

// Figure out how far we are in our total duration
float part = time / duration;

// Turn that part into +/0 HALF_PI
float rads = -FastMath.HALF_PI + FastMath.PI * part;

// Convert it to the sine range +/- 1
part = FastMath.sin(rads);

// Convert -1 to 1 range to 0-1
part = (part + 1) * 0.5f;

Vector3f current = new Vector3d().interpolate(beginVec, finalVec, part);

spatial.setLocalTranslation(current);
[/java]

Something like that.

That will give you a nice slow start and slow end but fast middle like:

For a more dynamic approach (for example if destination is moving) you could do a velocity, acc, max speed, type approach.

Modify the velocity towards 0 or towards max speed based on distance from target. Velocity could be a simple speed in which case you always move directly towards or a vect3f which would allow nicer curving towards…however be aware in the latter case that it will tend to “orbit” and you will need some damping logic as well.

to add another thing into the mix, you can also use the MotionPath of the Cinematics system which i believe supports easing

@wezrule said: to add another thing into the mix, you can also use the MotionPath of the Cinematics system which i believe supports easing
I don't think to just move objects one should use the cinematics system.. Its really not hard moving stuff on the screen directly, its one of the most basic things to do in a game. If you depend on external animation classes to do that, thats just going to make your code more complicated. Been there, done that in my very first jME2 games... just move them manually ;)

It worked great for garnaout tho, so I gotta recommend it

3 Likes
@wezrule said: It worked great for garnaout tho, so I gotta recommend it
1 Like

I ended up using something similar that pspeed suggested, thanks so much for your help