gotnull

Part 2 of 13 in Ghostveil

Turnstiles that turn the way you push

Ghostveil's turnstiles turn the way you push them, and come in shapes you can see turning.

The turnstile from the first night had two problems. It only ever turned clockwise, whichever side you pushed it from, and because it was a plus, a quarter turn put an arm back on every square an arm had just left, so you couldn't see it move. Over the next week I fixed both.

Each turnstile now has an orange pivot you can't walk onto, and I can put brown arms on any of its four sides. Here are two: an L with arms above and to the left of the pivot, and the cross:

Turnstile(Vector2(10, 5), [Vector2(-1, 0), Vector2(0, -1)]),
Turnstile(Vector2(18, 9), [
  Vector2(0, -1),
  Vector2(0, 1),
  Vector2(-1, 0),
  Vector2(1, 0),
]), // Cross +

I made a 20 by 15 test board with four L turnstiles, four T turnstiles, the cross and a short wall, and wrote every square's coordinates on it.

A 20 by 15 grid with coordinates in every square: a green player near the top, a short grey wall, and nine turnstiles with orange pivots and brown arms in L, T and cross shapes

I'm the green square.

Which way it turns

The rule I wanted is that the arm you push moves the way you push it. In the code that's a small table:

final rel = _getArmGridPos(pushedArm) - pivotGrid;

final isClockwise = switch (pushDirection) {
  "up" => rel.x < 0,
  "down" => rel.x > 0,
  "left" => rel.y > 0,
  "right" => rel.y < 0,
  _ => true,
};

Those four cases match the sign of a cross product. Call the pushed arm's offset from the pivot r\mathbf{r} and the direction of the push d\mathbf{d}; the two-dimensional cross product is the same sum that gives the turn of a push on a door:

τ=rxdy−rydx\tau = r_x d_y - r_y d_x

Here rxr_x and ryr_y are how far the pushed arm sits across from the pivot and below it, dxd_x and dyd_y are the push's step across and down, and τ\tau (tau) is the result. With yy counting down the screen, τ>0\tau > 0 means clockwise and τ<0\tau < 0 means anticlockwise. Pushing the top arm of an L to the right gives r=(0,−1)\mathbf{r} = (0, -1) and d=(1,0)\mathbf{d} = (1, 0), so τ=0⋅0−(−1)(1)=1\tau = 0 \cdot 0 - (-1)(1) = 1 and the arms turn clockwise. Push the same arm to the left and τ=−1\tau = -1:

Once the direction is known, each arm's offset turns through a quarter, one way or the other:

final rotated =
    isClockwise ? Vector2(-rel.y, rel.x) : Vector2(rel.y, -rel.x);

A push moves you two squares, through where the arm was and out the other side. If the landing square is the pivot or another arm, nothing moves. The turn also fails if an arm would land on a wall or on the player. Here's the L in the middle of the top row, in the game: a push to the right turns the L clockwise, a push straight back turns it back again, and a push down on its other arm turns it anticlockwise.

The square you land on never gets checked for walls. The L beside the wall turns clockwise when I push its left arm up from below, and that drops me on top of the wall.