Skip to content

Look API ​

Change how the camera turns with a controller.

Every frame, Controlify turns the player's camera from look sources, multiplied by look scalers:

  • A source adds to the turn. Controlify's sources are stick look, flick stick and gyro.
  • A scaler multiplies it, like slowing the camera down while zoomed in.

The Look API only handles controller look. Mouse look is untouched, so a mod that changes how the camera turns still handles the mouse itself, since it has to work without Controlify.

Scalers ​

Most addons only need a scaler. The example zooms the camera in while its zoom key is held, and slows controller look to match, so aiming feels the same zoomed in:

java
LookEvents.REGISTER.subscribeForever(registry -> registry.registerScaler(id("zoom"), controller -> (ctx, scales) -> {
	if (Zoom.isZooming()) {
		scales.rate(Zoom.FOV_MULTIPLIER, Zoom.FOV_MULTIPLIER);
		scales.direct(Zoom.FOV_MULTIPLIER, Zoom.FOV_MULTIPLIER);
	}
}));

Register scalers in LookEvents.REGISTER, subscribed from your entrypoint. The factory creates a scaler for each controller. Every frame, the scaler multiplies two kinds of contribution:

MethodMultiplies
rate(yaw, pitch)Rate contributions, like stick look
direct(yaw, pitch)Direct contributions, like gyro

Usually scale both. All scales multiply together, so the order scalers run in doesn't matter. A scaler can't tell sources apart: it scales every source's rate or direct contribution alike.

The total turn each frame is:

text
turn = rate × frame time × rate scales + direct × direct scales + scripted

Scripted contributions can't be scaled. Flick stick is scripted, so a flick always turns the camera to where the stick points, zoomed in or not.

Sources ​

A source adds its own contribution to the turn. The example lets the player steer a boat by tilting the controller like a steering wheel: the further it's tilted, the faster its source turns the camera, and the boat steers towards where its driver looks. Holding a tilt keeps turning, so it's a rate.

java
/// Tilting the controller like a steering wheel turns the camera while in a boat.
/// The boat steers towards where its driver looks, so this steers the boat.
public class TiltSteeringLook implements LookSource {
	/// Tilts smaller than this, in degrees, are ignored, so holding the controller roughly level doesn't turn.
	private static final double DEADZONE = 5.0;
	/// Degrees per second the camera turns for each degree of tilt, at full sensitivity.
	private static final double MAX_SENSITIVITY = 4.0;

	private final Controller controller;

	public TiltSteeringLook(Controller controller) {
		this.controller = controller;
	}

	@Override
	public void contribute(LookContext ctx, LookOutput out) {
		if (!(ctx.player().getVehicle() instanceof AbstractBoat)) {
			return;
		}
		this.controller.gyro().flatMap(GyroComponent::gravity).ifPresent(up -> {
			// Tilting the controller right makes "up" lean towards its left, the negative X axis.
			double tilt = Math.toDegrees(Math.asin(Math.clamp(-up.x(), -1.0, 1.0)));
			if (Math.abs(tilt) < DEADZONE) {
				return;
			}
			double steer = tilt - Math.copySign(DEADZONE, tilt);
			out.rate(steer * BoatSettings.tiltSensitivity * MAX_SENSITIVITY, 0);
		});
	}

	public static void registerEvents() {
		LookEvents.REGISTER.subscribeForever(registry -> registry.registerSource(id("tilt_steering"), TiltSteeringLook::new));
	}
}

Register a factory with registerSource(id, factory) in the same event, which creates one LookSource for each controller. Here the constructor is the factory.

contribute(ctx, out) is called every frame while controller look is being processed. Add to the turn with one of three kinds of contribution. Positive yaw turns right, and positive pitch looks down.

MethodUse forUnits
rate(yaw, pitch)A velocity, like a stick held in one directionDegrees per second. Controlify multiplies by the frame time.
delta(yaw, pitch)A physical displacement, like turning a gyroDegrees
scripted(yaw, pitch)A generated rotation that scalers mustn't change, like a flickDegrees

Contributions from every source add together, so the order sources run in doesn't matter either.

reset() is called when look processing is interrupted, like when a screen opens or the window loses focus. Drop any rotation still in progress there.

LookContext gives each frame's state:

FieldMeaning
controllerThe controller whose look is being processed
playerThe player being turned
dtThe frame time, in seconds
yaw, pitchThe camera's rotation at the start of the frame, in degrees

Keep a source self-contained, because a scaler can't tell which source a contribution came from. Each kind of input has its own sensitivity: Controlify has a stick look sensitivity and a gyro sensitivity, and each is applied inside its own source. There's no general look sensitivity, so a source for a new kind of input has a sensitivity setting of its own. Other behaviour that belongs to one source, like Controlify's gyro and stick look slowing down while aiming, goes inside that source too.

Physically turning the player's camera only happens with a controller. Use a source for how the player controls the camera, not for effects like camera shake, which should work the same with a mouse.

Controlify is an open-source Minecraft mod.