Tuesday, July 16, 2024

Implementing Player Platformer Controls

Taxonomy of Virtual Spaces Part II

After determining the formulas to use to design a player avatar controller for 2-D platformer style gameplay, I need to actually create the code and determine what values to use. How fast should the avatar run? How high should they jump? How far can they jump? What about double-jumping, jump-dash, or other types of movement?

This project is focused on game spatiality, so the character controller is really just a means to an end. The controls should be intuitive, familiar, uncomplicated, and even archetypal. As a start, the obvious choice would be to emulate Super Mario Bros.

Start with Super Mario Physics


A Complete Guide to SMB's Physics Engine by Jdaster64 (link to archive.org)


How fast does Super Mario run? How high does he jump? How far does he jump? This information was collected by Jonathan Aldrich, a.k.a. Jdaster64. Aldrich has spent years meticulously compiling mathematical data about how many Nintendo games function. His A Complete Guide to SMB's Physics Engine document, linked above, gives information on how Super Mario and Luigi move in Super Mario Bros. and the Japanese version of Super Mario Bros. 2 (known in the USA as Super Mario Bros.: The Lost Levels).

I don't know about Aldrich's methodology for acquiring his data and it would be prohibitively difficult to verify every value he's posted. However, I only need a controller that feels authentic to the player, not an emulation with pixel-perfect accuracy. At a cursory examination, his numbers make sense, and I will certainly know from testing if the results are accurate.

Aldrich's distance values are measured using screen units that make sense from the structure of the NES game console. Each different unit of measure is 1/16 the size of the previous unit and each is recorded using a hexadecimal value. One "Block" is 16 pixels across (using the basic 16 x 16 tile size of the NES). One "Pixel" is about 1/240th of the height of the screen and 1/256th of the width of the screen (the NES displays this to fit in the 4:3 aspect ratio of a standard definition television, so the pixels are slightly wider than they are tall).

SMB also tracked fractions of pixels of distance, which allowed for very precise values for acceleration and speed. There are also "Subpixels" (1/16 of a pixel), "Sub-subpixels" (1/256 of a pixel), and even "Sub-sub-subpixels" (1/4096 of a pixel).

Velocity is measured as distance moved per frame, or in 1/60 of a second. Acceleration is the amount of velocity change in 1/60 of a second.

How Many Pixels per Meter?

Converting Aldrich's values to metric

Virtual spaces in the Unity engine are measure in meters, which are typically equal to our own real-world metric system. I reckon that one block should be equal to one meter. In SMB, Mario grows from about 1 block high to about 2 blocks high when becoming Super Mario. 1-meter and 2-meter are both height values within the realm of real-world human possibilities. In addition, it makes things easier to build a world environment where each block is one meter across.

I used Excel to convert blocks and pixels to meters and convert frames to seconds. Now I have the number values to set my velocities, distances, and acceleration values. Now I need a script to give everything structure.

Ultimate 2D Platformer Controller

Fortunately, I found a tutorial video (linked above) for a character controller that uses Unity's new Input System (which is a learning objective for me in this project) and is built using the same formulas I wrote about in my last post. The scripts seem well designed and the results are good so far. The jump values can easily be edited and include options like coyote time and hang time. There is a fair amount of extra functionality I will still need to add (such as different jump heights for walking and running), but this seems like the perfect structure to start with.

UPDATE:

If you follow the Ultimate 2D Platformer Controller tutorial linked above, be sure to watch part 2 of the tutorial here. The first part of the video cleans up and fixes a few bugs from the original video and, if you are interested, shows how to add wall slides, wall jumps, and mid-air dashes.

No comments:

Post a Comment

New Game Launched and Summer 2024 Research Review

Summer 2024 Review Taxonomy of Virtual Spaces Click here to play the new Taxonomy of Virtual Spaces - Expanded Edition interactive project...