Confused about SimpleVectors

Started by Irony, December 27, 2012, 12:02:57 PM

Previous topic - Next topic

Irony

private static SimpleVector v4 = new SimpleVector();

Do you have any idea how this

public static synchronized SimpleVector something(SimpleVector p) {
  v4 = new SimpleVector();
  v4.set(p);

.... // do something with v4

return v4;
}


can, under certain circumstances, produce something different than that:

public static synchronized SimpleVector something(SimpleVector p) {
  v4.set(p);

.... // do something with v4

return v4;
}


Is this even possible?

EgonOlsen

Depends on what you do with v4 afterwards. In one case, you'll work with a newly created instance each time. In the second case, you always return the same instance. Depending on what you do with returned value, you might get some side effects.

Irony

#2
well, let's pretend for a moment that I just recently started with the whole Java thing :D
I solved it by

v.set(something(p));

instead of v = something(p);

outside of the method.

Sometimes being too deep within game logic lets you forget the most basic stuff.


post script:
Does the getXAxis() method create a new vector internally, and if yes, do you plan to add a parameterized version like getTranslation(SimpleVector) ? Any other ways to elimiate as much object creation as possible besides from not using calcAdd etc. ?

EgonOlsen

Quote from: Irony on December 27, 2012, 12:36:03 PM
post script:
Does the getXAxis() method create a new vector internally, and if yes, do you plan to add a parameterized version like getTranslation(SimpleVector) ? Any other ways to elimiate as much object creation as possible besides from not using calcAdd etc. ?
Added such methods to the latest beta: http://jpct.de/download/beta/jpct_ae.jar

Irony

how cool is that?
works beautifully
thank you!