New Vector3f.orthogonal() method?

Hi,



I just came across the problem of needing an orthogonal vector to a given one and found some code from the Apache math library. One typical use-case is the creation of a 3D reference system, given a vector. One could create one per orthogonal(), the other per cross() and voila… The code assumes the vector to be normalized!!



Would this be an addition to the core?



[java]public Vector3f orthogonal() {



float threshold = 0.6f;

if ((x >= -threshold) && (x <= threshold)) {

float inverse = 1 / FastMath.sqrt(y * y + z * z);

return new Vector3f(0, inverse * z, -inverse * y);

}

if ((y >= -threshold) && (y <= threshold)) {

float inverse = 1 / FastMath.sqrt(x * x + z * z);

return new Vector3f(-inverse * z,0, -inverse * x);

}

float inverse = 1 / FastMath.sqrt(x * x + y * y);

return new Vector3f(inverse * y, -inverse * x,0);

}[/java]

I’m curious what you needed it for. Back in the days of rotation matrixes I used to find myself needing something like this but with Quaternions I rarely do. Plus, this method’s orthogonal is pretty random… usually when one is forming a new basis they want to be more specific than just looking in a particular direction. After all, you could be looking in that direction but upside down or something.



Because while I’ve rarely/occasionally needed to from new orthogonal basis vectors… I’ve never needed it to be based on some unpredictable random ortho.



So, can you describe your use-case? Maybe there is a better more predictable and accurate way to accomplish the same thing.

Well my use-case is actually different. I wanted to go with a beam of a certain radius through a 3D grid. And while I am running along that beam I sample around those 2 orthogonal vectors to check if I hit something along the road. It is of course not perfect since I could miss something but its fast and works “good enough”

@Dodikles said:
Well my use-case is actually different. I wanted to go with a beam of a certain radius through a 3D grid. And while I am running along that beam I sample around those 2 orthogonal vectors to check if I hit something along the road. It is of course not perfect since I could miss something but its fast and works "good enough"


In your case, I think it would be better to project multiple beams. Much less work overall, I think... for you and the computer.
1 Like

I suggest to check teh hpysics system and use a sweeptest witha sphere, as that is probably way more efficiently, due to the broadphase.

@EmpirePhoenix said:
I suggest to check teh hpysics system and use a sweeptest witha sphere, as that is probably way more efficiently, due to the broadphase.


When I hear "3D grid", I automatically think "cube world". So my answer may not have been best if that's not the case.

For a grid-aligned search, though, (as in for detecting hits in a block world) a custom search is better than these as it's pretty straight forward to perform better. And multiple "rays" in this case are still better than a sweep test.

Hmm, maybe you are right and I should go with your approach :wink: