I uploaded a jar with a few formulas I find useful. A couple of these are already available in the FastMath package, but I included for completeness. A few bezier formulas for floats, vectors and ColorRGBAs, calculating reflection vectors, closest points on the circumference of a circle, angle between two quaternion rotations and the altitude of an isosceles triangle given the base and opposite angle.

That last one I find useful for perspective camera positioning if I want to make sure that a certain amount of world units is visible to the camera. For instance if you wanted to create a 3d UI you might do something like:

``````int screenWidth = settings.getWidth();
int screenHeight = settings.getHeight();
double fieldOfView = 49.134;
double altitude = GMath.camAmplitude(fieldOfView, screenWidth);
Camera camera = cam.clone();
camera.setFrustumPerspective(fieldOfView, screenWidth / (float)screenHeight, altitude - 200, altitude + 200);
camera.setLocation(new Vector3f(screenWidth / 2f, screenHeight / 2f, altitude));
camera.setRotation(new Quaternion(0f, 1f, 0f, 0f));
``````

Available at http://1337atr.weebly.com/gmath.html

2 Likes

For your last use-case, you might be able to do a much simpler calculation by looking in the projection matrix for the camera. I use this all the time to figure out “how far away from this do I need to be to have it in view”. I use it in the SimArboreal Editor to position my camera for the tree atlas.

Lines of code and comments here:

Essentially, m11 is the magic value:
float m11 = camera.getViewProjectionMatrix().m11;

``````// In the projection matrix,  should be:
//      (2 * Zn) / camHeight
// where Zn is distance to near plane.
``````

And so if you want to know how far away you need to be from some object for its ‘half size’ to fit on screen. It’s something like:
float dist = m11 * size;

I’m not sure your solution would work in my scenario since m11 is based on the distance to the near plane, but in my scenario the near plane is unknown because the near plane is calculated from the altitude.

In your scenario the camera is already setup and you need only position it so some object fits within its viewable area. In my scenario the camera is not yet setup and needs to have it’s viewable area created so that it covers a particular portion of the world.

In my scenario you don’t know where the near and far planes need be until you know the altitude.