Vectors

ok… I’ve been cracking my head this day on this but I couldn’t find any solution, so please help me!



Imagine you have a radius-vector (the one that goes from the beginning of coordinates (0;0;0) and ends in some point (x;y;z). Wiki. ).



The coordinates of it’s point are known in 3d space, let’s say they are (x1;y1;z1). The beginning of it is in the (0;0;0), because it’s radius vector.



Now I need to “make it longer”  by “a” points, and find it’s new coordinates. If represent it in 2d space, schematically it can be so:




If the coordinates before it's prolongation are known (x1;y1;z1) and I need to make it longer by "a" points, how do I find it's new coordinates after prolongation (x?;y?;z?)????

I draw a picture representing it in 2d space, but I need to get formulas for 3d space. Maybe in jME there's some kind of function for this?

The only solution I see it to calculate the angle of this vector and sin/cos to find new vector's projections on each  coordinate axe...

heh, as usual, time is progress :slight_smile:



The solution is simple.


public static Vector3f IncreaseVectorBy(Vector3f base, float i)
    {
        // base - base vector that needs to be prolonged by "i" points
        Vector3f rez = new Vector3f();
        float vlen = (float) Math.sqrt(base.x*base.x + base.y*base.y + base.z*base.z);
        float ab = vlen + i;

        rez.x = ab * base.x / vlen;
        rez.y = ab * base.y / vlen;
        rez.z = ab * base.z / vlen;
       
        return rez;
    }


You could try this, using the jme length and multiplication methods so you don't have to do the donkey work yourself :



public static Vector3f IncreaseVectorBy(Vector3f base, float i)
{
        float length = base.length();
        return base.mult((length + i) / length);
}


hm lol, that spam bot even said somthing partly helpfull  :-o :-o



To change the lenght of a vector multiply each element with the scalar.

In case of a one unit vector (normalize()) this leads to a vector with the lenght of the scalar.

Empire Phoenix said:

hm lol, that spam bot even said somthing partly helpfull  :-o :-o

To change the lenght of a vector multiply each element with the scalar.
In case of a one unit vector (normalize()) this leads to a vector with the lenght of the scalar.


By the way of "normalize()" what is the difference from "normalizeLocal()"?

Pratically I was trying to solve the inverse of the problem proposed on the thread, that is finding a point on a vector having only its distance from the origin:


            Vector3f vOrigin = new Vector3f();
            Vector3f vTarget = node.getLocalTranslation();
           
            Vector3f direction = vTarget.subtract(vOrigin);
            [b]direction.normalizeLocal();[/b]
            Vector3f mul = direction.mult(distance);
            Vector3f position = vOrigin.add(mul);



I am really more than a beginner with math vectors so I don't understand why using "normalizeLocal()" all works as expcted, while using "normalize()" the position moves more far...

normalize() returns the normalized vector, as a new Vector3f object.

normalizeLocal() changes the values of the existing Vector3f, to be normalized.



So if you used direction.normalize() in that code, it wouldn't do anything - nothing gets done with the return value so the next line would multiply the original value of direction.



The way you would do it with that method is condense the two lines into one:


Vector3f mul = direction.normalize().mult(distance);

PS You will find the same convention with many of the other methods. eg. add() and addLocal(), mult and multLocal().



So if you wanted ugly condensed code and a bunch of temporary objects, you could do the whole lot in one line:



Vector3f position = vOrigin.add(node.getLocalTranslation().subtract(vOrigin).normalize().mult(distance));



Doesn't make much sense to do so there, but since sometimes you want to keep the original vector and sometimes not, both versions come in handy.
Alric said:

PS You will find the same convention with many of the other methods. eg. add() and addLocal(), mult and multLocal().

So if you wanted ugly condensed code and a bunch of temporary objects, you could do the whole lot in one line:


Vector3f position = vOrigin.add(node.getLocalTranslation().subtract(vOrigin).normalize().mult(distance));



Doesn't make much sense to do so there, but since sometimes you want to keep the original vector and sometimes not, both versions come in handy.


Thanks a lot, also for having correct my mistaken use of normalize at all  :D