# Move a cube between 2 different Vector locations

Hello all.

I am trying to learn jMonkeyEngine, and I have now learned to move my cube geometry with the move() method.

[java]
public void simpleUpdate(float tpf) {
//geom is an object variable storing my cube, and cubeSpeed is storing a constant speed value for the cube movement
geom.move( tpf * cubeSpeed, 0.0f, 0.0f );
}
[/java]

However, this approach make the cube move along the x azis infinite. My question is then this.

How can i make use of linear interpolation, to make the cube move between 1 vector3f location (start position), to another vector3f location (end position), and then stop at the end position??

I have tried to use FastMath.interpolateLinear( scale, startVector3, endVector3 ), but I can’t get the box, to move, when I insert this into the move() method… So how can I make my cube move from start position to end position, and then stop at the end position??

move() is relative. If you want linear interpolation then you should abandon move since you no longer want to be moving relative, you want to be moving some interpolation between two points.

@pspeed said: move() is relative. If you want linear interpolation then you should abandon move since you no longer want to be moving relative, you want to be moving some interpolation between two points.
I am not sure what you mean, how can I then do it, can you provide an example of some simple code? I have also tried searching for it on the internet, but i can't find any examples using JME 3.

one possibility would be Paths & MotionControl.
However that might be “overkill” depending on what you goal is.

I guess there is a simpler solution using some Math / FastMath command.

@martin-rohwedder said: I am not sure what you mean, how can I then do it, can you provide an example of some simple code? I have also tried searching for it on the internet, but i can't find any examples using JME 3.

This really is kind of a simple thing, though.

[java]
Vector3f someStartPosition…
Vector3f someEndPosition…
float interp = 0.0;

on update:
interp += speed * tpf;
mySpatial.setLocalTranslation(new Vector3f().interpolate(someStartPosition, someEndPosition, interp);
[/java]

…until interp is 1.0.

2 Likes
1 Like

Hi all

Thanks for the help. It seems that I have solved the problem, by doing this.

[java]
//Object variables
private Geometry geom;
private Vector3f startPos = new Vector3f(0,0,0);
private Vector3f endPos = new Vector3f(20,0,0);
private float interp = 0.0f;
private float mySpeed = 2.0f;

public void simpleUpdate(float tpf) {
interp += mySpeed * tpf;
Vector3f currentPos = geom.getLocalTranslation();

``````if ( currentPos.distance(endPos) &gt; 0.1f ) {
geom.setLocalTranslation(new Vector3f().interpolate(startPos, endPos, interp));
}
``````

}
[/java]

Though how can I make it move for a constant speed for each WU it comes across?? now when I increase the distance between startPos and endPos, my cube just goes there faster. How can I make the cube go to the end position at the same speed as it would when the end position was closer??

Calculate the distance between the start and end using
[java]
float distance = startPos.distance(endPos);
[/java]
and factor that into your interp progress like so:
[java]
interp += (mySpeed/distance) * tpf;
[/java]

2 Likes
@sgold said: Calculate the distance between the start and end using [java] float distance = startPos.distance(endPos); [/java] and factor that into your interp progress like so: [java] interp += (mySpeed/distance) * tpf; [/java]
Incredible... It works like a dream. Thank You!!
1 Like

This:
currentPos.distance(endPos) > 0.1f

Is unnecessary since as distance approaches 0 interp will approach 1.0.

Could just have easily have said:
interp < 1.0

Though in either case you may overshoot your target slightly.

Edit: actually, even worse. In your approach you may overshoot the target and keep going if some frame is long enough.