Issues with general purpose of SimpleVector Class

Started by gameCloner, February 16, 2013, 03:18:59 PM

Previous topic - Next topic

gameCloner

Hi Helge and the jpct community.

first of all i would excuse me caus im no an expert poster, so i ask you to be kind in teaching me some.

im trying to code a ball game like tilt. im posting it soon if i can omn the forum.

i came across some code errors


SimpleVector copy = globalPos;

updateGlobalPos();  // or updateGlobalPos(globalPos)


this code affects copy as well. I understood i can not assign vectors as strings. To me it could be better to have a clone() and a equals() method in class.
example 2:

float maxKnot = 12.f;
public void interpolate(SimpleVector p1, SimpleVector p2){
SimpleVector delta = new SimpleVector(p1.x,p1.y,p1.z);
delta.sub(p2);
for ( int i = maxKnot ; i >0 ; i--){
SimpleVector temp= new SimpleVector(delta.x,delta.y,delta.z);
temp.scalarMul( i  / maxKnot);
temp.add(p2);
points.add(temp);
}
}

to me that should look like this:

public void interpolate(SimpleVector p1, SimpleVector p2){
      p1.sub(p2);
      for ( int i = maxKnot ; i >0 ; i--){
SimpleVector delta = p1.clone().scalarMul( alfa ).add(p2);
                points.add( delta );


it could be far better if we can add vectors the math style, as in c++ or as vertexShaders code.
   

EgonOlsen

#1
I don't get what you mean with your first example? You want to assign the content of globalPos to copy? Or create a copy of it? You can do both in several ways:


SimpleVector copy=new SimpleVector(globalPos);

..or...

SimpleVector copy= SimpleVector.create(globalPos);

..or to set it:

copy.set(globalPos);



equals() is implemented for SimpleVector.

You can't add SimpleVector (or anything except primitives and strings in Java) in math style, because there's no operator overloading in the language (thank god...).

I see your point, but if i would make all methods return the instance to concatenate their calls, nobody would be able to distinguish if that call creates a new instance or just returns "this". As it is now, if a method of SimpleVector returns a SimpleVector, you can be sure that it's a new instance. There are variants for add and sub called calcSub and calcAdd that return a new instance instead of working on "this".





gameCloner

You ve been clear.
I still cannot code as I usually do.
but I can bear with, every other one can bear with that, so I.

Thanks and be well.

EgonOlsen

Just extend SimpleVector and add your way of doing it.