MovingBlocks / TeraMath

Experimental standalone math and stuff. Automation category: Terasology Library
Apache License 2.0
13 stars 11 forks source link

LookAt Function for Quat4 #44

Open pollend opened 8 years ago

pollend commented 8 years ago

I think a lookat function would be really useful for Quat4. The basic use would be for having an object point in the direction of another entity.

https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/37141607-lookat-function-for-quat4?utm_campaign=plugin&utm_content=tracker%2F1949271&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F1949271&utm_medium=issues&utm_source=github).
skaldarnar commented 8 years ago

The Quat4 class already has shortestArcQuat. Does that already do what you need?

The method could use some more Javadoc for sure, especially on what it actually computes. Maybe something along these lines: http://eigen.tuxfamily.org/dox/classEigen_1_1QuaternionBase.html#ac35460294d855096e9b687cadf821452

pollend commented 8 years ago

I manage to produce something similar with using shortestArcQuat two times. one for the rotating in the horizontal and one for the vertical plane. A lookat function is probably not necessary at all since you can reproduce it. Might be a nice little helper function.

Quat4f horizontalRotation =Quat4f.shortestArcQuat(Vector3f.north(),new Vector3f(tangent).setY(0).normalize());
Quat4f verticalRotation = Quat4f.shortestArcQuat(new Vector3f(tangent).setY(0).normalize(),new Vector3f(tangent));
verticalRotation.mul(horizontalRotation);
location.setLocalRotation( verticalRotation);