Hey there fellow monkeys!
I have a problem I can’t figure out on my own, so I decided to ask you.
TL;DR
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.
DESCRIPTION
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.
QUESTION
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();
app.start();
}
@Override
public void simpleInitApp() {
this.al = new AmbientLight();
this.al.setColor(ColorRGBA.White.mult(2));
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));
rootNode.addLight(al);
rootNode.addLight(dl1);
rootNode.addLight(dl2);
cubeRed = getNewCube(assetManager, ColorRGBA.Red, 1f);
rootNode.attachChild(cubeRed);
}
@Override
public void update() {
super.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);
geom.setMaterial(mat);
return geom;
}
}