Author Topic: Confused about SimpleVectors  (Read 2599 times)

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Confused about SimpleVectors
« 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?
« Last Edit: December 27, 2012, 12:10:38 pm by Irony »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Confused about SimpleVectors
« Reply #1 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.

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Re: Confused about SimpleVectors
« Reply #2 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. ?
« Last Edit: December 27, 2012, 12:39:56 pm by Irony »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Confused about SimpleVectors
« Reply #3 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

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Re: Confused about SimpleVectors
« Reply #4 on: December 28, 2012, 09:20:30 pm »
how cool is that?
works beautifully
thank you!