Usk Productions Blog

Preggo Game Jam progress 2023-05-07

Posted by Usk Productions on .

A weekend of failed math

I spent all weekend trying to figure out how to make the perspective look right when scrolling the scenery in flying levels. It is much, much harder than I expected.

The problem of getting a plane into perspective is basically the problem of mapping coordinates from a rectangle onto a trapezoid. My initial approach was this:

  1. Create two sets of four points, with the first set describing the corners of the visible play area in top-down view, and the second set describing the corners of the trapezoidal perspective view.
  2. Determine where the vanishing point on the perspective view should be via simple algebra.
  3. Write a function that calculates the apparent scale of any position based on top-down coordinates and the width ratio of trapezoid.
  4. Write a function that maps the vertical component of the top-down view to the vertical component of the perspective view via linear interpolation, and maps the horizontal component by scaling the offset from the center of the screen using the scale function.
  5. Also use the scale function to resize sprites.

This almost worked. Objects move in the correct direction in perspective. But the distances are all wrong. Really, the vertical grid spacing should be much larger when you're closer to the "camera".

I tried to come up with a function to do this mapping correctly, but it's way over my head and I can't afford to keep spending time on it. Apparently the correct solution involves Homography, but I'm just not grokking it.

As a rough approximation of the correct mapping, I'm cubing the y position before passing it to the lerp function. It's not correct, and you can tell that the trees seem to slow down as they approach the camera, but it's the best I've managed to do.

As you can see in the video, there's an infinite field of trees that rushes below you. The red lines are there so I can verify the trees are moving away from the vanishing point correctly.

Tres also have collisions enabled when they're near the bottom of the screen, so you'll take damage if you get too close.

Still a very long way to go.