Suggestion

I've just found a bug in my code that can easily be avoided by a small change in the JME code. I created to cameras and assigned them the same initial position (that is the same Vector3f object). It took me a while to discover that JME doesn't clone provided vectors. In my opinion JME should clone everything since it's undocumented that JME will change your input vectores.

Do you agree or is there a specific reason that vectors aren't cloned? If you don't agree with me I suggest that it should be added to the javadoc which methods alters their input parameters.



S

Currently most methods dealing with vectors in the scenegraph operate the way you described - using references not copies. This allows you to do e.g.

someSpatial.getLocalTranslation().set( 23, 2, 2 );


to assign a new position to a spatial without creating a new vector object (and thus without creating garbage).

I agree that this may be confusing. I also would like to change that - mostly because we would have proper encapsulation and a good way for change detection then. But it would require major API changes and thus break almost all existing applications. That's why it was postponed again and again...

I understand that it's a good thing to work with inner objects from outside using for instance getLocationTranslation. But would it do any harm if you changed setLocalTranslation to make a copy of the Vector3f coming from outside? Right now the following code will make two spatials tied together forever:



Spatial sp1 = ...;
Spatial sp2 = ...;
sp1.setLocalTranslation(sp2.getLocalTranslation());



This could lead to a lot of confusion - especially for inexperienced JME users like myself  :D

It could. But for the same performance reasons this is not done currently. Unfortunately some people even use this as a feature. (that's a reason why it would break apps if it's changed)

It's not likely to change (if it does) until a major version shift…  like 1.0.

Honestly, I prefer it the way it is.  For example, I can do things like this:

Vector3f v = new Vector3f(0.0f, 0.0f, 0.0f);
spatial.setLocalTranslation(v);
v.set(5.0f, 0.0f, 0.0f);


I can create a Vector3f and make changes to it that work themselves out in anything I've set it to. This is nice not only for reducing resource usage, but also very nice if you are making lots of changes to the positioning of an object (and you are able to abstract the object that the Vector3f belongs to).

Why not just do something like:

spatial.setLocalTranslation(v.clone());



Is that such a pain?

I think his point was regarding it not being properly documented.

I was responding to this:


sthorsen said:
But would it do any harm if you changed setLocalTranslation to make a copy of the Vector3f coming from outside?

Okay, nice to know  :slight_smile: Now I know how to handle things so I will stop complaining and continue coding  :smiley: Thanks for all the input.

Yes my point was that I think it should be documented if it couldn't be changed. And since it seems impossible to change at the moment I think documentation will be just fine. It's no problem to clone your own vectors as long as you're aware of what goes on behind the scenes. But I still don't think it's nice software design to require people to know about inner workings of the API.

You don't have to clone either.



sp1.getLocalTranslation().set(sp2.getLocalTranslation());