Hello,
I need to move a node in a grid, you can only move to an adjacent cell, currently my logic is as follows:
if (! Node.getLocalTranslation (). equals (nodeNewPosition)) {
Node.setLocalTranslation (nodeNewPosition);
}
Obviously when you run the application, my node jumps between cells, I need to improve it so that it moves at a constant speed between cell and cell, any ideas ?.
That actually is a basic vector geometry stuff.
Considering you wan’t to move from A to B with speed X, the direction vector C would be B - A, then you normalize C (making it a vector with length of 1) and multiply it by X.
So something like D = B.substract(a).normalize().mult(X);
And then you set your node translation to A + D
In your case you will actually have to check if the result vector’s, say, D’s, length is greater than distance between B and A, and if it’s true then you just set the node translation to B
Vector3f a = from
Vector3f b = to;
Vector3f c = b.subtract(a);
float x = 5f;
Vector3f d = c.normalize().mult(x*tpf);
if(d.length()>a.distance(b)){
d = c;
}
Node.setLocalTranslation(a.add(d));
x should be multiplied with tpf to decouple the movement from the framerate, otherwise its will not run at a constant speed. The hello tutorials (press F1 in the SDK) show how to do this.