www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: Irony on December 27, 2012, 12:02:57 pm

Title: Confused about SimpleVectors
Post by: Irony on December 27, 2012, 12:02:57 pm
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?
Title: Re: Confused about SimpleVectors
Post by: EgonOlsen on December 27, 2012, 12:19:41 pm
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.
Title: Re: Confused about SimpleVectors
Post by: Irony on December 27, 2012, 12:36:03 pm
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. ?
Title: Re: Confused about SimpleVectors
Post by: EgonOlsen on December 27, 2012, 08:36:25 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 (http://jpct.de/download/beta/jpct_ae.jar)
Title: Re: Confused about SimpleVectors
Post by: Irony on December 28, 2012, 09:20:30 pm
how cool is that?
works beautifully
thank you!