Search in sources :

Example 1 with CollisionPlane

use of io.xol.chunkstories.physics.CollisionPlane in project chunkstories by Hugobros3.

the class Camera method computeFrustrumPlanes.

private void computeFrustrumPlanes() {
    Vector3f temp = new Vector3f();
    // Init values
    float tang = (float) Math.tan(toRad(fov / 2.0));
    float ratio = (float) viewportWidth / (float) viewportHeight;
    float nh = 0.1f * tang;
    float nw = nh * ratio;
    float fh = 3000f * tang;
    float fw = fh * ratio;
    float rotH = rotationY;
    float rotV = rotationX;
    float a = (float) ((180 - rotH) / 180f * Math.PI);
    float b = (float) ((-rotV) / 180f * Math.PI);
    Vector3f lookAt = new Vector3f((float) (Math.sin(a) * Math.cos(b)), (float) (Math.sin(b)), (float) (Math.cos(a) * Math.cos(b)));
    Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
    lookAt.cross(up, up);
    up.cross(lookAt, up);
    lookAt.add(new Vector3f((float) this.position.x, (float) this.position.y, (float) this.position.z));
    // Create the 6 frustrum planes
    Vector3f Z = new Vector3f((float) this.position.x, (float) this.position.y, (float) this.position.z);
    Z.sub(lookAt);
    Z.normalize();
    Vector3f X = new Vector3f();
    up.cross(Z, X);
    X.normalize();
    Vector3f Y = new Vector3f();
    Z.cross(X, Y);
    Vector3f nearCenterPoint = new Vector3f((float) this.position.x, (float) this.position.y, (float) this.position.z);
    temp = new Vector3f(Z);
    temp.mul(0.1f);
    nearCenterPoint.sub(temp);
    Vector3f farCenterPoint = new Vector3f((float) this.position.x, (float) this.position.y, (float) this.position.z);
    temp = new Vector3f(Z);
    temp.mul(3000f);
    farCenterPoint.sub(temp);
    // Eventually the fucking points
    Vector3f nearTopLeft = vadd(nearCenterPoint, vsub(smult(Y, nh), smult(X, nw)));
    Vector3f nearTopRight = vadd(nearCenterPoint, vadd(smult(Y, nh), smult(X, nw)));
    Vector3f nearBottomLeft = vsub(nearCenterPoint, vadd(smult(Y, nh), smult(X, nw)));
    Vector3f nearBottomRight = vsub(nearCenterPoint, vsub(smult(Y, nh), smult(X, nw)));
    Vector3f farTopLeft = vadd(farCenterPoint, vsub(smult(Y, fh), smult(X, fw)));
    Vector3f farTopRight = vadd(farCenterPoint, vadd(smult(Y, fh), smult(X, fw)));
    Vector3f farBottomLeft = vsub(farCenterPoint, vadd(smult(Y, fh), smult(X, fw)));
    Vector3f farBottomRight = vsub(farCenterPoint, vsub(smult(Y, fh), smult(X, fw)));
    cameraPlanes[0] = new CollisionPlane(nearTopRight, nearTopLeft, farTopLeft);
    cameraPlanes[1] = new CollisionPlane(nearBottomLeft, nearBottomRight, farBottomRight);
    cameraPlanes[2] = new CollisionPlane(nearTopLeft, nearBottomLeft, farBottomLeft);
    cameraPlanes[3] = new CollisionPlane(nearBottomRight, nearTopRight, farBottomRight);
    cameraPlanes[4] = new CollisionPlane(nearTopLeft, nearTopRight, nearBottomRight);
    cameraPlanes[5] = new CollisionPlane(farTopRight, farTopLeft, farBottomLeft);
    // cache that
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                corners[i * 4 + j * 2 + k] = new Vector3f();
            }
        }
    }
}
Also used : Vector3f(org.joml.Vector3f) CollisionPlane(io.xol.chunkstories.physics.CollisionPlane)

Aggregations

CollisionPlane (io.xol.chunkstories.physics.CollisionPlane)1 Vector3f (org.joml.Vector3f)1