[SOLVED]Spatial.worldToLocal() makes Spatial to jump between points

Hey there fellow monkeys!

I have a problem I can’t figure out on my own, so I decided to ask you.

Trying to positon a spatial in local space by converting a vector from worldspace to local space. But my spatial “vibrates” and has a doppelganger.

I’m creating a world builder application and I noticed that if I have a location vector and I try to convert it to local space coordinates with Spatial.worldToLocal() it results in a strange behaviour. The spatial starts to jump between this location and another location and it changes its location between these two so fast that it looks like that there is two spatial on the scene. The vibration comes from the fact that the spatial is not on the same place in every frame.

I made a simple aplication to reproduce the problem. See below.

Am I doing something wrong? I didn’t find any material regading the usage of worldToLocation. As I know, it translates a world location to the spatials local space. Am I using it right?

The example:

package game.dev;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;

public class MathGame extends SimpleApplication {
private AmbientLight al;
private DirectionalLight dl1;
private DirectionalLight dl2;
private Spatial cubeRed;

private Vector3f worldLocation = new Vector3f(2f, 2f, 2f);

public static void main(String[] args) {
    MathGame app = new MathGame();

public void simpleInitApp() {
    this.al = new AmbientLight();

    this.dl1 = new DirectionalLight();
    this.dl1.setDirection(new Vector3f(1, -1, 1).normalizeLocal());
    this.dl1.setColor(new ColorRGBA(1f, 203f / 255f, 46f / 255f, 1f).mult(0.7f));

    this.dl2 = new DirectionalLight();
    this.dl2.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
    this.dl2.setColor(new ColorRGBA(1f, 223f / 255f, 128f / 255f, 1f).mult(1f));


    cubeRed = getNewCube(assetManager, ColorRGBA.Red, 1f);


public void update() {

    // This is the line where I have the problem.
    // The location is in world coordinates, and I want to convert it to the spatials model coordinates.
    // If I leave it like this, my cubeRed spatial jumps between (2f, 2f, 2f) and (0f, 0f, 0f) locations.
    cubeRed.setLocalTranslation(cubeRed.worldToLocal(worldLocation, null));

 * Returns a new cube as a spatial.
 * @param assetManager the assetManager
 * @param color Color of cube. If null, cube is cyan colored.
 * @param scale size of cube. If null, the default size is 0.4f.
 * @return
public static Spatial getNewCube(AssetManager assetManager, ColorRGBA color, Float scale) {
    if(scale == null) scale = 0.4f;
    if(color == null) color = ColorRGBA.Cyan;
    Box b = new Box(Vector3f.ZERO, scale, scale, scale);
    Geometry geom = new Geometry("Box", b);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", color);
    return geom;


Can you tell us what you was trying to do ?

foo.worldToLocal is going to give you a position relative to foo’s origin… so it doesn’t make sense to keep setting the same world point again because it will be relative to the last one you set and so on.

so yeah, I agree we need to know what you are actually trying to do… because the code is currently doing exactly what you’ve told it to.

Oh my god, I understand now. Somehow I overlooked that it will give the relative position. :chimpanzee_facepalm: :chimpanzee_cry: Everything is clear now. :chimpanzee_closedlaugh:

The only thing I wanted to do is to convert the cursor’s 3D position to a position in the spatial’s local space. And I did it the same way as in this code I showed.

Thank you guys! :smile: I won’t have nightmares anymore. :smiley: