Here is a ChaseCameraAppState that rotates around the target spatial's local axis

My game is a space game and I wanted a chase camera that rotates around the target spatial’s local horizontal and vertical plane. The problem with the default ChaseCameraAppState is that it rotates around the world horizontal and vertical space and it’s Y plane is not locked to the target spatial. So I overloaded 2 methods and created a ChaseCameraAppState that rotates around the target spatial’s local planes regardless of the target’s world orientation and is locked to the target spatial’s local Y axis. Here is the code … enjoy

ChaseCameraAppState chaseCameraAppState = new ChaseCameraAppState()
	{
		@Override
		public void setTarget(Spatial targetSpatial)
		{

			super.setTarget(targetSpatial);
			rotateCamera();
		}

		@Override
		protected void rotateCamera()
		{
			if(spatial == null)
				return;
			verticalRotation = FastMath.clamp(verticalRotation, minVerticalRotation, maxVerticalRotation);
			TempVars vars = TempVars.get();
			Quaternion rot = vars.quat1;
			Quaternion rot2 = vars.quat2;
			rot.fromAngleNormalAxis(verticalRotation, spatial.getWorldRotation().getRotationColumn(0).normalize());
			rot2.fromAngleNormalAxis(horizontalRotation, spatial.getWorldRotation().getRotationColumn(1).normalize());
			rot2.multLocal(rot);
			target.setLocalRotation(rot2);
			vars.release();
		}

		@Override
		public void update(float tpf)
		{
			if(spatial == null)
			{
				throw new IllegalArgumentException("The spatial to follow is null, please use the setTarget method");
			}

			target.setLocalTranslation(spatial.getWorldTranslation());

			camNode.lookAt(target.getWorldTranslation(), spatial.getWorldRotation().getRotationColumn(1).normalize());

			target.updateLogicalState(tpf);
			target.updateGeometricState();
		}
	};
3 Likes

For code formatting, this post might help you:

1 Like

Fixed. Thanks for the heads up.