Java Code Examples for com.jme3.math.FastMath#atan()

The following examples show how to use com.jme3.math.FastMath#atan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlyByCamera.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Zoom the camera by the specified amount.
 *
 * @param value zoom amount
 */
protected void zoomCamera(float value){
    // derive fovY value
    float h = cam.getFrustumTop();
    float w = cam.getFrustumRight();
    float aspect = w / h;

    float near = cam.getFrustumNear();

    float fovY = FastMath.atan(h / near)
            / (FastMath.DEG_TO_RAD * .5f);
    float newFovY = fovY + value * 0.1f * zoomSpeed;
    if (newFovY > 0f) {
        // Don't let the FOV go zero or negative.
        fovY = newFovY;
    }

    h = FastMath.tan( fovY * FastMath.DEG_TO_RAD * .5f) * near;
    w = h * aspect;

    cam.setFrustumTop(h);
    cam.setFrustumBottom(-h);
    cam.setFrustumLeft(-w);
    cam.setFrustumRight(w);
}
 
Example 2
Source File: StaticCamera.java    From OpenRTS with MIT License 6 votes vote down vote up
protected void zoomCamera(float value){
    // derive fovY value
    float h = cam.getFrustumTop();
    float w = cam.getFrustumRight();
    float aspect = w / h;

    float near = cam.getFrustumNear();

    float fovY = FastMath.atan(h / near)
              / (FastMath.DEG_TO_RAD * .5f);
    fovY += value * 0.1f;

    h = FastMath.tan( fovY * FastMath.DEG_TO_RAD * .5f) * near;
    w = h * aspect;

    cam.setFrustumTop(h);
    cam.setFrustumBottom(-h);
    cam.setFrustumLeft(-w);
    cam.setFrustumRight(w);
}
 
Example 3
Source File: FlyByCamera.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void zoomCamera(float value){
    // derive fovY value
    float h = cam.getFrustumTop();
    float w = cam.getFrustumRight();
    float aspect = w / h;

    float near = cam.getFrustumNear();

    float fovY = FastMath.atan(h / near)
              / (FastMath.DEG_TO_RAD * .5f);
    fovY += value * 0.1f;

    h = FastMath.tan( fovY * FastMath.DEG_TO_RAD * .5f) * near;
    w = h * aspect;

    cam.setFrustumTop(h);
    cam.setFrustumBottom(-h);
    cam.setFrustumLeft(-w);
    cam.setFrustumRight(w);
}
 
Example 4
Source File: AbstractCameraController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected boolean doToggleOrthoPerspMode() {

        float aspect = (float) cam.getWidth() / cam.getHeight();
        if (!cam.isParallelProjection()) {
            cam.setParallelProjection(true);
            float h = cam.getFrustumTop();
            float w = cam.getFrustumRight();
            float dist = cam.getLocation().distance(focus);
            float fovY = FastMath.atan(h) / (FastMath.DEG_TO_RAD * .5f);
            h = FastMath.tan(fovY * FastMath.DEG_TO_RAD * .5f) * dist;
            w = h * aspect;
            cam.setFrustum(-1000, 1000, -w, w, h, -h);
            return true;
        } else {
            cam.setParallelProjection(false);
            cam.setFrustumPerspective(45f, aspect, 1, 1000);
            return false;
        }
    }
 
Example 5
Source File: LodControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
    BoundingVolume bv = spatial.getWorldBound();

    Camera cam = vp.getCamera();
    float atanNH = FastMath.atan(cam.getFrustumNear() * cam.getFrustumTop());
    float ratio = (FastMath.PI / (8f * atanNH));
    float newDistance = bv.distanceTo(vp.getCamera().getLocation()) / ratio;
    int level;

    if (Math.abs(newDistance - lastDistance) <= distTolerance) {
        level = lastLevel; // we haven't moved relative to the model, send the old measurement back.
    } else if (lastDistance > newDistance && lastLevel == 0) {
        level = lastLevel; // we're already at the lowest setting and we just got closer to the model, no need to keep trying.
    } else if (lastDistance < newDistance && lastLevel == numLevels - 1) {
        level = lastLevel; // we're already at the highest setting and we just got further from the model, no need to keep trying.
    } else {
        lastDistance = newDistance;

        // estimate area of polygon via bounding volume
        float area = AreaUtils.calcScreenArea(bv, lastDistance, cam.getWidth());
        float trisToDraw = area * trisPerPixel;
        level = numLevels - 1;
        for (int i = numLevels; --i >= 0;) {
            if (trisToDraw - numTris[i] < 0) {
                break;
            }
            level = i;
        }
        lastLevel = level;
    }

    spatial.setLodLevel(level);
}
 
Example 6
Source File: LodControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void controlRender(RenderManager rm, ViewPort vp){
    BoundingVolume bv = spatial.getWorldBound();

    Camera cam = vp.getCamera();
    float atanNH = FastMath.atan(cam.getFrustumNear() * cam.getFrustumTop());
    float ratio = (FastMath.PI / (8f * atanNH));
    float newDistance = bv.distanceTo(vp.getCamera().getLocation()) / ratio;
    int level;

    if (Math.abs(newDistance - lastDistance) <= distTolerance)
        level = lastLevel; // we haven't moved relative to the model, send the old measurement back.
    else if (lastDistance > newDistance && lastLevel == 0)
        level = lastLevel; // we're already at the lowest setting and we just got closer to the model, no need to keep trying.
    else if (lastDistance < newDistance && lastLevel == numLevels - 1)
        level = lastLevel; // we're already at the highest setting and we just got further from the model, no need to keep trying.
    else{
        lastDistance = newDistance;

        // estimate area of polygon via bounding volume
        float area = AreaUtils.calcScreenArea(bv, lastDistance, cam.getWidth());
        float trisToDraw = area * trisPerPixel;
        level = numLevels - 1;
        for (int i = numLevels; --i >= 0;){
            if (trisToDraw - numTris[i] < 0){
                break;
            }
            level = i;
        }
        lastLevel = level;
    }

    spatial.setLodLevel(level);
}
 
Example 7
Source File: CameraHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * This method converts the given structure to jme camera. Should be used form blender 2.5+.
 * 
 * @param structure
 *            camera structure
 * @param sceneStructure
 *            scene structure
 * @return jme camera object
 * @throws BlenderFileException
 *             an exception is thrown when there are problems with the
 *             blender file
 */
private CameraNode toCamera250(Structure structure, Structure sceneStructure) throws BlenderFileException {
    int width = DEFAULT_CAM_WIDTH;
    int height = DEFAULT_CAM_HEIGHT;
    if (sceneStructure != null) {
        Structure renderData = (Structure) sceneStructure.getFieldValue("r");
        width = ((Number) renderData.getFieldValue("xsch")).shortValue();
        height = ((Number) renderData.getFieldValue("ysch")).shortValue();
    }
    Camera camera = new Camera(width, height);
    int type = ((Number) structure.getFieldValue("type")).intValue();
    if (type != 0 && type != 1) {
        LOGGER.log(Level.WARNING, "Unknown camera type: {0}. Perspective camera is being used!", type);
        type = 0;
    }
    // type==0 - perspective; type==1 - orthographic; perspective is used as default
    camera.setParallelProjection(type == 1);
    float aspect = width / (float) height;
    float fovY; // Vertical field of view in degrees
    float clipsta = ((Number) structure.getFieldValue("clipsta")).floatValue();
    float clipend = ((Number) structure.getFieldValue("clipend")).floatValue();
    if (type == 0) {
        // Convert lens MM to vertical degrees in fovY, see Blender rna_Camera_angle_get()
        // Default sensor size prior to 2.60 was 32.
        float sensor = 32.0f;
        boolean sensorVertical = false;
        Number sensorFit = (Number) structure.getFieldValue("sensor_fit");
        if (sensorFit != null) {
            // If sensor_fit is vert (2), then sensor_y is used
            sensorVertical = sensorFit.byteValue() == 2;
            String sensorName = "sensor_x";
            if (sensorVertical) {
                sensorName = "sensor_y";
            }
            sensor = ((Number) structure.getFieldValue(sensorName)).floatValue();
        }
        float focalLength = ((Number) structure.getFieldValue("lens")).floatValue();
        float fov = 2.0f * FastMath.atan((sensor / 2.0f) / focalLength);
        if (sensorVertical) {
            fovY = fov * FastMath.RAD_TO_DEG;
        } else {
            // Convert fov from horizontal to vertical
            fovY = 2.0f * FastMath.atan(FastMath.tan(fov / 2.0f) / aspect) * FastMath.RAD_TO_DEG;
        }
    } else {
        // This probably is not correct.
        fovY = ((Number) structure.getFieldValue("ortho_scale")).floatValue();
    }
    camera.setFrustumPerspective(fovY, aspect, clipsta, clipend);
    return new CameraNode(null, camera);
}