# ORBITAL & DYNAMIC GRAVITY in FLOWLAB

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!

6 Likes

Nice work; it’s always great to see more people use trig.
One thing that could be good is if maybe planets were circles so that bouncing around them was easier?
Also if you want to make the gravity cumulative between all planets instead of exclusive perhaps you could add up all the forces on the player from the various planets’ gravitational pulls?
Those are just some ideas though; this is already a really neat accomplishment!

2 Likes

Thank you very much! I am very excited to continue work on this project. I was going to make the planets round, but I wanted to prove that this worked regardless of rough terrain. I will most likely add some round planets as well though.
We were considering using multiple orbitals and The Law of Gravity to allow for much more realistic gravity, and I intend upon pursuing this topic, but probably for a space simulation project rather than a copy-and-paste tech demo. The goal of this one was to simulate, at least partially, how Super Mario Galaxy movement works so as to enable other creators to make games with similar mechanics. However, I am absolutely going to attempt to try to add a system where the current velocity in relation to planar X and Y are kept alongside the rotational movement, so that you can do things like go into full orbit and smoothly transition between objects. Thanks for the feedback!

2 Likes

*now with circles

2 Likes

Disclaimer: I am not a computer science expert by any means. In fact, the vast majority of what I did to help meburningslime was just trig knowledge and stuff I learned in a Physics class, not at all related to computer science.

2 Likes

That said, I did help take the physics of how gravity works in real life and explain how to implement it into code, as he mentioned.

All that to say, even if you’re planning on making video games all your life, still pay attention in science class, kids!

4 Likes

Wow. This is real nice.

Hahaha.

I won’t be making any brick comments (I’m horrid at math), but I can still give some feedback.

The rotation is very well made. And it feels like real gravity rather than the planets just sucking the player to the planet’s orbit. I could honestly just play around with this for hours.

I would see the next challenge to be making differing gravity intensity based on the size with the same object. -Although, don’t get me wrong, I still have no idea what the heck is going on in that behavior editor.

2 Likes

Heh. A tip I definitely missed in my early years, lol.

1 Like

sin and cos my beloved

but also don’t they output between -1 and 1? -1 being one direction and 1 being the opposite.

1 Like

Yes, it’s between 1 and -1, which is why the player will move in the opposite direction on one side of the “planet” as it would on the other, because the -1 cancels it out. I think the point meburningslime was trying to make was that the number absolute value will be 1 or less, it won’t be like 2 or 5 or something.

2 Likes

We actually discussed doing something like that, possibly using the number of pixels in the planet and/or the density value on flowlabs. It could also just be set so that whoever makes the planet can set a mass value for each planet, affecting how strong the pull of gravity is. Mathematically it wouldn’t be too complicated since gravity is based off of Newton’s Law of Gravity I believe. However, that would be something to fully flesh out in the future.

2 Likes

I know that grazer made an example similar to this a while ago.

Flowlab Game Creator - Planet Gravity

I wasn’t sure if this project was inspired by it or maybe just coincidence, but this example is a bit outdated since it’s been around even before when I first joined. I would definitely like to see a more advanced version with smoother controls and movement.

2 Likes

No, I didn’t even know this existed!
At first I was terrified that I was outdone again, but actually using it shows that this example is pretty equivalent to my first prototype (linked above). If you move left or right, all gravity just ceases to exist, and although it is not as obvious (partially because of the camera and partially because he used a motor which is way jankier elsewhere) the character still behaves oddly when changing planets. It is rather fascinating to know that Grazer had the same idea though!

1 Like

Yeah! gravity doesn’t entirely cease to exist (the obvious thing is that if there was no gravity, theoretically you would go in a straight line off the screen, but also you will eventually spiral back to the planet). However, you will take much longer to fall if you’re moving than if you aren’t, unlike in meburningslime’s version. I suppose Grazer’s version could be more useful in certain games, but I think meburningslime’s version is more realistic.

2 Likes

This is way better than any gravity test so far!
The gravity actually feels like your getting pulled to the center instead of getting slinged around the planet. No matter where you jump and move around the plannet, you will always fall back down. I have never been able to get this right using motors or velocity, I’ve been trying to push grazer to added something like accumilitve velocity instead of absolute velocity but I’m not sure what exactly would be needed to get this effect a lot more simple with the behaviors we have in flowlab. But great work, this also gives me a chance to look more into this!

Also, here's your math simplified into 2 expressions:

4 Likes

Thank you very much! That velocity function would be rather incredible and I would love to have it implemented. It seems like simply creating internal variables for the math and making the velocity block have the option to either set, add, or set min (only set if movement is slower than current velocity) shouldn’t be too hard, but maybe he either has grander plans or has a lot on his plate. Either way, I’m sure you know more than me.

That code looks like it should work, thank you very much! I’ll let everybody know on here if I am able to make more progress with this. My next step I think is to take all the above code but make it into a velocity adder instead of a “setter” so that the transitions are smoother and things like moving platforms can effect the player (not that I would use that but it may be extremely helpful for someone using my code.) Thanks once again!

2 Likes

## Hey all, some exciting new updates!

First thing I did was implement JR’s compression of the code. It works perfectly and just a lot neater than the way I did it. I added a whole bunch of notes to try to compensate. It should be just barely comprehensible to the layman.
Next, I really wanted to fix two issues: The camera flipping and velocity magically changing directions when you change orbiting centers. This was not easy, but eventually I managed to make a fine-tuned system that takes the current velocity, divides it into a decmial-sized addition, and adds it to a new variable. This variable in essence stores all the previous velocities and adds them all together. This is then added to the current velocities (not the calculations, the actual X and Y velocities) to create a system that takes the entire system developed before and just adds another velocity factor to it. I also capped the stored velocity at 6, but this is arbitrary and just tailored for my example. It will greatly depend on what situation this system is used in for how strong the previous velocity should be. Because you get launched from previous velocities into the new orbit, the physics and more noticably the camera more smoothly transitions between two orbitals. It still isn’t perfect, but more on that later.
It seems like the orientation of the player is in relation to the top left, not the center, of the gravitational orbit block. I desire to fix this but am unaware of a solution that will work for all distances. Maybe this could lead me into creating a way to let the character roll and spin while still having accurate movement?
Finally, because the entire system is now gravity-dependent on most fronts, this allowed me to link player controls to gravity. You can no longer move or rotate when in space, but previous velocities still apply. This was a little janky to create, especially with the bundle output bug plaguing my existence, but it eventually ended up just swell.

There are numerous things which require further development, and I would greatly appreciate any comrades to brainstorm these with.
-Both the character and camera rotation still snap in place, which is especially evident on the larger planet in the example. I tried figuring out how to ease the change but to no avail.
-There are still instances in which jumping does not grant enough speed to transition the camera quickly to the other side, causing camera freakouts. However, the jump nearly always puts you to the correct planet now. Thus, I am thinking this is not something to fix, but rather to cover up, as the exact math is performing as accurate as can be without taking into account multiple gravitational pulls.
-I want to create a clone of this project once the development is finished in order to make easily accessible numbers. Rather than go through all the code meticulously, there could be a line of numbers with notes that clearly explain what changing each does (e.x. movement speed, carried velocity cap, gravitational strength) for extreme ease of use. No longer shall flowlabians be bound to two-dimensional gravity!
-Finally, I strongly desire, once all of the above is complete, to create another duplicate that is made to be extremely immersive and realistic. Planetary mass, distance, propulsion strength, mass of the player, and even potentially air resistance will all be accounted for. This will hopefully lead me into developing a micro-planetary exploration game where you take into account all the factors of movement before launching rockets to explore the stars. I am fostering a growing passion for space and this could be my perfect in on a unique game that the flowlab community has not seen the likes of ever before.

As always, thank you all for the support, critiques, and suggestions, and I look forward to the development process!

1 Like

2 Likes

Exciting updates most definitely! I love the addition of the little spaceship. And the trail is so smooth!
This just became 120% more enjoyable!

2 Likes

## BIG UPDATE

I just worked on the planetary game version for 6 hours, partially with @Arianllyn, and FINALLY MADE ORBITING PLANETS AND CUMULATIVE VELOCITY. YOU CAN ALSO NOW LAND ON MOVING PLANETS! The code now also works for non-player objects. I shall attempt to add this to the simple game example as well!
The code is extremely complex and I shall attempt to explain it when I post the full update explanation but I simply wanted to share the progress I made currently. (Also gravity is multiplied by 2 and larger objects give more gravity.)

3 Likes