Hello all,
After watching a mini-documentary on Super Mario Galaxy’s physics engine, I decided that I wanted to attempt to incorporate some of the more advanced game design mechanics from Nintendo’s games into Flowlab, both for personal use and further pushing the limits of the engine’s capabilities for public gain. Thus, after hours of consulting an engineer, a college textbook, and a @Arianllyn who is an expert in computer science, I finally put together this mathematically complex but fundamentally simple example. All of the code is in a single block! Please do feel free to use this code in any of your projects, with or without accreditation, although I would appreciate the plug. Additionally, if you have any recommendations to what I can do to optimize and further enhance this project, let me know! The player code with some modification should work for projectiles as well, which I may make an example for in the future. If you just want to take a gosh darned peek already go right on ahead, but if you plan to use this in a project please read below for a description of how the code works.
Note: I did my best to make the links digestible in case you wanted to really understand the concepts.
The first big hurdle of this project was how to strip away the movement from the X and Y axis and transition it to rotational velocity. This was done (after a lot of research and help) by using Cosine and Sine. These two functions, given the angle of the object in relation to the rotational center in Radians, can tell you how much force from angular motion goes to X and how much goes to Y. Sounds complicated, but literally all you need to do is to plug in the angle and you get the result instantaneously. The output is in a one-digit (0 to 1) range, which makes the next step exceedingly easy. All you do is multiply the output by how fast you want to go and it will move the character accordingly. Doing the opposite (swapping the cosine and sine) will move the character up and down in relation to the center of rotation.
This is both where I cloned my game to work on the next step separately and where I hit my second wall. You see, up to this point there had been one major issue throughout the project: I only had at my disposal velocity, impulse, and motor blocks. Velocity completely overrules the two latter movement options, and as using a motor for movement led to excessive issues, as anyone moderately experienced knows, I chose to look for an alternative. This is where @Arianllyn recommended to me to use precursory variables. In the previous builds, moving had been directly linked to the velocity block. With in-between variables, I could tell it “When you press D, move right, but before you do add gravity!” We eventually settled on using one variable for horizontal movement in relation to the center of gravity and one for moving further and closer (named Around Velocity and Down Velocity in the code, respectively). Using this, I could give developers all sorts of flexibility with the system, from making bounce pads to creating rocket launchers to allowing knockback from weapons! The only downside, which I am currently looking in to figuring out how to change, is that as the player uses his own velocity, conventional projectiles and other physics-enabled objects have little effect on it. Anyways, this has been a long enough monologue. Do enjoy, and I would love to receive feedback of any sort!