I was stumped while developing my game for many hours because I was settings nodes to translation of Vector3f.ZERO and they would sometimes disappear! I realized that they were getting local translations that were not (0,0,0) but I thought that was impossible! Turns out, in some other code, I was setting vertices to Vector3f.ZERO and not using .clone(), which eventually moved Vector3f.ZERO if I happened to move those vertices elsewhere.
PLEASE make the constants in Vector3f be “static final” and not just “static” to save others from this headache. This is Java 101 stuff.
@admazzola said:
I was stumped while developing my game for many hours because I was settings nodes to translation of Vector3f.ZERO and they would sometimes disappear! I realized that they were getting local translations that were not (0,0,0) but I thought that was impossible! Turns out, in some other code, I was setting vertices to Vector3f.ZERO and not using .clone(), which eventually moved Vector3f.ZERO if I happened to move those vertices elsewhere.
PLEASE make the constants in Vector3f be “static final” and not just “static” to save others from this headache. This is Java 101 stuff.
Thank you!
Andrew
Making them final would not change this. It’s java 101 stuff to know that.
Well, with Java 101 you would know that making it final wouldn’t change anything, as Vector3f is mutated in place, while final is only protecting reassignment of the field itself.
Said that, in my version of jme3 it is
public final static Vector3f ZERO = new Vector3f(0, 0, 0);
so it is already final (which still doesn’t help).
I thought to google the actual source right after typing this. I did find that it actually said final, so I tried deleting this post but that did not work. Interestingly, changing all of the Vector3f.ZERO calls to Vector3f.ZERO.clone() did indeed solve my problem so I have no idea what the heck the deal with all of this is. Very strange.
@admazzola said:
I thought to google the actual source right after typing this. I did find that it actually said final, so I tried deleting this post but that did not work. Interestingly, changing all of the Vector3f.ZERO calls to Vector3f.ZERO.clone() did indeed solve my problem so I have no idea what the heck the deal with all of this is. Very strange.
Thanks
It’s not strange. Before you were modifying shared references and now you aren’t. Java provides no way to make an object’s values “constant” from an external reference. The “final static” only controls the reference itself… not the values in the object.
Note: whenever you add something subtly insulting such as “This is Java 101 stuff.” to a post, you increase your chance of being wrong by about 1000x. The universe is not nice in that respect.
Yeah I don’t know why I was being passive aggressive I still don’t understand the whole ‘external reference’ thing but this isn’t the subforum to discuss it.
public static final Vector3f MY_CONSTANT = new Vector3f(1,1,1);
Can’t do:
MY_CONSTANT = new Vector3f(0,0,0);
Can do:
MY_CONSTANT.x = 0;
Or:
MY_CONSTANT.set(0,0,0);
Only the “pointer” is constant… not the thing it points to. Java has no such concept of ‘const’ like C++ does… and it would really complicate the language, anyway.
‘Normally’ you make most of your classes immutable and then there is no such issue - but unfortunately jme3 cannot affort it everywhere due to performance reasons. So we have to be careful instead…