Getter/Setter methods for attribute fieldOfView in the class Camera.java in jme 3.4

Hello everyone,
would it be possible to add the getter and setter methods for the attribute fieldOfView in the Camera.java class to make it easier to read and write in the next release of jme 3.4?

  • public void setFieldOfView(float fov) {}
  • public float getFieldOfView() {}

something like that so you don’t have to use a wrapper class like in the example?

public class MainCamera {
    
    private Camera cam;
    private float fieldOfView;
    private float near;
    private float far;
    
    /**
     * Initialize the application camera to a
     * 45 degree fov, 0.1 near plane, and 1000 far plane.
     * 
     * @param cam
     */
    public MainCamera(Camera cam) {
        this(cam, 45, 0.1f, 1000); // 45 is the default JME fov
    }

    /**
     * Initialize the specified camera to the specified parameters.
     * 
     * @param cam
     * @param fov
     * @param near
     * @param far
     */
    public MainCamera(Camera cam, float fov, float near, float far) {
        this.cam = cam;
        this.fieldOfView = fov;
        this.near = near;
        this.far = far;
        resetCamera();
    }
    
    public void setFieldOfView(float f) {
        if (this.fieldOfView == f) {
            return;
        }
        this.fieldOfView = f;
        resetCamera();
    }

    public float getFieldOfView() {
        return fieldOfView;
    }

    public void setNear(float f) {
        if (this.near == f) {
            return;
        }
        this.near = f;
        resetCamera();
    }

    public float getNear() {
        return near;
    }

    public void setFar(float f) {
        if (this.far == f) {
            return;
        }
        this.far = f;
        resetCamera();
    }

    public float getFar() {
        return far;
    }
    
    private void resetCamera() {
        float aspect = (float) cam.getWidth() / (float) cam.getHeight();
        cam.setFrustumPerspective(fieldOfView, aspect, near, far);
    }
}
1 Like

I see how something like this could be useful, but “the devil is in the details”.

What should the new methods do if someone sets cam.setParallelProjection(true), either before or after instantiating the MainCamera?

1 Like

If I understand correctly, it looks like the original Camera.java code already contains the solution and sets

    // Camera is no longer parallel projection even if it was before
    parallelProjection = false;

when you call the setFrustumPerspective method

1 Like

Will the field of view always measured vertically, in degrees? Most angles in JME are measured in radians. Also, for some applications it might make more sense to measure the field of view horizontally.

Apparently the setFrustumPerspective method provides as input a value in degrees on the vertical axis.

If it helps, I can give you an example of how the fov is modified in a typical third person shooter game. In several tutorials I have noticed that typically when the player activates the aim with the firearm the fov varies.

private class PlayerWeaponControl extends AbstractControl implements ActionListener {

	...
	
	private MainCamera _MainCamera;
	public float nearClipPlane = 0.01f;
	public float farClipPlane = 100f;
	public float defaultFOV = 60;
	public float aimFOV = 50;
	public float aimingSpeed = 8f;

	private boolean isAiming;
	
	@Override
	public void setSpatial(Spatial sp) {
		super.setSpatial(sp);
		if (spatial != null) {
		
			...

			_MainCamera = new MainCamera(camera, defaultFOV, nearClipPlane, farClipPlane);
			logger.log(Level.INFO, "Initialized");
		}
	}


	@Override
	protected void controlUpdate(float tpf) {
		
		...
		
		updateFOV(tpf);
	}
	
	
	private void updateFOV(float tpf) {
		if (isAiming) {
			float fov = FastMath.interpolateLinear(tpf * aimingSpeed, _MainCamera.getFieldOfView(), aimFOV);
			_MainCamera.setFieldOfView(fov);
		} else {
			float fov = FastMath.interpolateLinear(tpf * aimingSpeed, _MainCamera.getFieldOfView(), defaultFOV);
			_MainCamera.setFieldOfView(fov);
		}
	}
	
}

Normal fov = 60

Aim fov = 50

Go ahead and open an issue or PR at GitHub. If we hustle, we can get this included in v3.4

3 Likes
3 Likes

Thanks, @capdevon.

Anyone want to take ownership of this issue and open an pull request?

I can go ahead and do it. Seems easy enough.

2 Likes
5 Likes

thanks to you @sgold and @Markil3 :wink:

2 Likes