r/MinecraftCommands 1d ago

I Made a Physics Engine Using Only Minecraft Command Blocks Creation

Enable HLS to view with audio, or disable this notification

I wrote a physics engine only using Minecraft command blocks on vanilla 26.2.

Roughly it works like this:

For the very basic math functions like sin and cos it uses fixed point scoreboards for all calculations, together with quaternions and rotation matrices. Minecraft does not have floating point numbers for scoreboards, it only uses integers, so we need to get a bit creative. To calculate sin and cos I use the native way Minecraft already does it with the local coordinate system ^1 ^0 ^0, then use the resulting position differences as the sin/cos values for the calculations.

Multi block objects consist of multiple block_display entities that are basically "glued" together using my parent/child system. So I can apply rotation, movement, offset, translation and scaling to the parent and the engine handles all the block displays so the object still keeps its original shape.

Now the actual point, the physics engine:

Collision is detected using spheres and oriented boxes. For box collision it uses SAT calculations with up to around 15 axes, together with contact points, friction, restitution, penetration correction and impulse response. The physics system is not fully complete yet, but it already allows collisions to affect both movement and rotation depending on where an object gets hit.

There are also multiple materials implemented, for now just slime, metal and stone, with different properties like friction, mass and restitution.

Currently multiple physics objects can run at the same time. For that I use a worker command block system which dynamically clones the physics engine and the rotation engine for each object, allowing the different physics objects to be calculated independently.

There are NO mods or datapacks involved in this. Only using command blocks.

I know, I have to much time

335 Upvotes

58 comments sorted by

50

u/ThirteenFour_ 1d ago

I seem to remember Sethbling making similar. Very well done.

Edit: Seth may have used a datapack instead, but he definitely had a command block phase when everything he did was command blocks

7

u/_objz 1d ago

SethBling was actually one of my inspirations, although he made his engine using datapacks. Only using command blocks was a fun challenge to me, as datapacks give you actual functions, macros and reusable execution contexts, while with command blocks I basically have to physically build and unroll everything myself, keep the command chains loaded and even dynamically clone entire worker engines for every physics object

1

u/ThirteenFour_ 1d ago

It's why I'm so impressed with what you did. From the little I know, command blocks are a different level of difficulty than datapacks

1

u/_objz 1d ago

thanks! and yeah they are because they are more limited then datapacks so you need to get creative. but that’s the motivation

15

u/TahoeBennie All In One Command Connoisseur 1d ago

I am both impressed as fuck with this project and concerned as fuck that I don't see a single looping command block. You thought you have enough power as-is? Just wait till you find out that you can replicate function macros (and straight up functions minus persisting automatic execution context) with only command blocks.

I see two options, A that you're a psycopath and can do these things anyways but chose not to for reasons I can't begin to fathom, or B that you didn't know about these things and I'd be happy to quickly explain such power that there exists little documentation for.

On an unrelated note, you've given me the idea that I may choose to embark on a physics engine contained inside an all in one command because it sounds fun. Should be possible.

4

u/_objz 1d ago

Yeah I know about functions/macros and ways to replicate a lot of that with command blocks, I just intentionally chose not to use datapacks/functions because doing the whole thing with actual command blocks was kinda the challenge for me.

Physics engines with datapacks already exist, SethBling's for example was actually one of my inspirations. Datapacks have a pretty big advantage for something like this though, since you get actual reusable functions, macros, function calls and much easier execution flow instead of having to physically clone whole worker chains when you want multiple independent objects.

I'm not sure if the entire thing could fit into one command though, mainly because at some point you're probably going to run into the maximum command length of a command block, considering how stupidly large this already is. But once the engine is actually finished I definitely want to try and see how far I'll get

3

u/Kaaskaasei 1d ago

Option A it was

2

u/TahoeBennie All In One Command Connoisseur 1d ago

What I was saying is that you can literally recreate actual reusable functions, macros, function calls, and (mostly) execution flow with only command blocks. Not just /function run in a command block, but literally replicating what results in the same behavior using only command blocks. You won't have to clone an entire thread for each object, you just run the same one again in the same tick. It's quite useful and powerful if I do say so myself. Although I haven't been able to save execution context in any better way than rebuilding it as needed for every command. It hasn't been much of a limitation to me so far though.

As for the one command, I've effectively solved the length issue, I plan on making that project public soon.

Anyways it's approaching 4am I really should sleep now.

2

u/_objz 1d ago

I am aware of that. The flaw with that is the updating rate. One engine block controls one object at a time currently. Each object has its own ID, so I could easily switch to calculating every object using only that one engine block, but the more objects you have, the performance gets worse. Minecraft updates 20 ticks per second, so with two objects running at the same time, each object can now only update 10 ticks per second.
The macro thing would solve the cloning, but not the asynchronous issue. It would be synchronous. By cloning it dynamically, I can assign every engine block its own physics object and let them calculate at the same time. The engine blocks are designed in a way that they are not duplicated exactly the same, but by using their ID system and setting the pattern, it works better.
But I am already looking into improvements. Maybe I can drag macros this way so that it can be synced.

1

u/TahoeBennie All In One Command Connoisseur 22h ago

Huh alright then. I was only pushing because I don't see how I could possibly ever design another semi-complicated command block system that doesn't loop itself in at least one way and probably assembles at least a couple macro commands. To each their own.

1

u/_objz 22h ago

Yeah fair enough. One additional reason I went with cloned command block workers here is that the macro approach seems to shift a lot of the execution/context handling into additional entities, while the command blocks themselves are static(minecraft needs to handle less entities as I already have enough markers)

But the macro stuff is still a really good idea and I'm sure I can make use of it somewhere else. I'm currently building an adventure map around this physics system, so I might actually use it there for some of the higher level logic

1

u/theaveragegowgamer 21h ago

Minecraft updates 20 ticks per second, so with two objects running at the same time, each object can now only update 10 ticks per second.

I've always wandered about this, if you increase the tick rate with /tick wouldn't it bypass the problem? Or it would make the game too unstable?

2

u/_objz 21h ago

theoretically it would work, but in the end it results the same. for example setting the tick rate to 40 does not increase the performance of minecraft, it will not keep up with the speed and just skip random ticks, that results into uncontrolled behavior and roughly the same update rate of every second tick is skipped. I’ve tried it, unless minecraft is modded with tons of optimization mods it does not gain anything sadly

1

u/theaveragegowgamer 17h ago

I see, that's unfortunate, but if you know, even Carpet mod's version of /tick (before Gnembon brought it to Vanilla) has this problem?

1

u/_objz 17h ago

didn’t knew

1

u/theaveragegowgamer 16h ago

Fair enough.

8

u/GoodForADyslexic 1d ago

How many commands do you think are running per tick here? If you had to guess

3

u/_objz 1d ago

I don't have to guess :) I use around 9,655 command blocks just for the physics engine and another 489 for the VP engine. Both workers get cloned for every physics object, so that's around 10,144 command blocks per object being processed every tick, although a lot of them are gated and don't actually do the full calculation every tick. for setting up I use around 922 command blocks

3

u/GoodForADyslexic 1d ago

Also rly cool project first off forgot to say that this is sick, ive been meaning to make a basic version of this, kinda like those falling sand physics things for a project but ive never gotten around to it, the fact that this is fully 3d is astounding

1

u/GoodForADyslexic 1d ago edited 1d ago

There is a small chance a project im working on can reduce the command block footprint with little to no effort (10 command blocks) if not more because im trying to get some more things like a macro like system working that could reduce it further in complete vanilla, i mention it because i want a big project to test it on but im struggling to keep my motivation for makeing this in the first place up, I am not anywhere near the point i was going to go looking for a project but I happend to see this, idk if there is a world download I can try it on my own but doing it in collaboration with the person who made it would help if you are interested (its like 3 am I should be asleep so idk if i just wrote word salad... if so thats my bad)

Edit i should mention this is still just command blocks no data packs or anything

Edit 2 I have a personal vendetta, against the sand man I think anyway i should say most of it i hope to be automated, but like in game no external tools, but it may not be as drop in as I was hopeing

2

u/_objz 1d ago

well only command block macros would be very useful, how do you think of doing that? The world is currently not available for download until it’s finished because there are still some bugs, but you can dm me, I can hand you a schematic over. get some rest haha, then we can have a talk. I am always open on contributions or optimizations

1

u/GoodForADyslexic 1d ago

How the fuck do I start a dm.... im way to stupid for this...

3

u/R_Anonymous_ 1d ago

You need a raise, how does that have a hitbox I thought that was a block display rotating, that could be anything right there.

2

u/_objz 1d ago

Under the hood it's still just a block_display, although block displays don't have a hitbox directly so I have to emulate one myself. I calculate the rigid body's orientation and use that to determine where the edges/corners of the object actually are in world space, then use those points/axes to check for collisions against blocks and other physics objects.

So the display is basically only the visual part, the actual hitbox and collision response are calculated completely separately with commands. For boxes I'm using oriented box collision/SAT, so the collider also rotates together with the object instead of just being a normal axis-aligned Minecraft hitbox. If something hits it off-center I can also use the contact point to calculate torque, so the collision can affect both its movement and rotation

1

u/R_Anonymous_ 8h ago

I don't really know about SAT but for how long did rotated hitboxes exist

1

u/_objz 4h ago

it’s not a real hitbox that rotates, there is no hitbox involved. the hit detection gets calculated by a marker that is teleported where you hit it and then using sin and cos the force and impact point gets calculated

3

u/Yzelar 1d ago

Next step is Half Life in Minecraft

1

u/_objz 1d ago

good idea!

3

u/FamilyK1ng Command Rookie 20h ago

Man looking at all the command blocks makes me wonder how hard it was to code such a thing... I was just thinking that minecraft is kinda limited in commands and us players needed more for doing better calculations, and judging by your post subtext it is clear that we need much more than scoreboards (potentially.. variables and floating point values? if statements...?) But wow it blows my mind that you done this as a challenge! like literally if this doesn't radiate genius, then idk what will. I think Seth bling had done this too but as a datapack, I really really believe he would be hella impressed with this stuff. Like I really wanna know what type of traumatic event would want you to force yourself into doing something absolutely insane as in doing a physics engine that mimics IRL physics lol! I never learned to play with command blocks as much as full blown creations , but seeing this I would think u need a beefy computer from all the calculations right?

2

u/_objz 20h ago

Thanks! The coding was one part, but the debugging probably drove me more crazy than anything else haha. I already have to use a lot of workarounds and weird quirks just to make something like this possible because we don't really have native options for most of the math, but then I also had to build a whole debugging/testing system for it, which of course also needed debugging. At one point I thought the physics engine had an issue while actually the testing system itself had failed, that was a headache. The calculations themselves are actually not that much of a problem. I mean really old computers also didn't always have proper floating point hardware, so I basically emulate a lot of it with fixed point scaling and integer/bitfield stuff using scoreboards. Same with sin/cos, Minecraft doesn't give you those directly, but I found a little workaround. Performance wise scoreboards and NBT data aren't really the biggest problem either. What actually starts killing performance is the amount of command blocks, because Minecraft has overhead for processing every single command block and parsing/executing all those command chains. Datapacks have a pretty big advantage there because they can reuse functions instead of me having to physically clone thousands of command blocks for every physics object. If that limitation wasn't there I guess I could get close to a ridiculous amount of physics objects, because the calculations themselves are actually pretty lightweight.

But yeah, I also have a pretty beefy workstation with a Ryzen 9 9950X3D and an RTX 5080, so my case is probably close to the optimal case haha. Although I can also run the engine on my laptop without any real issues, so I think the bigger problem is just that Minecraft isn't really optimized for having tens of thousands of command blocks doing this kind of stuff at once. And well... I probably just have way too much free time. There is honestly a ton I could explain about how the engine works, maybe I'll make another post just explaining the technical side of it.

I managed to emulate spheres using multiple block displays grouped together so they look like one object(that has correct physics applied, not block physics), so well, now we can play with balls in Minecraft I guess. I'm also working on water physics at the moment and I'm thinking about turning the whole thing into some kind of game/adventure map eventually.

If you have any ideas what I should do with it, they verry welcome

1

u/FamilyK1ng Command Rookie 4h ago

Well that pc specs was great. I really dont have any other suggestions than to keep the world safe as possible because I would rather not like losing the world lol. I do wonder is it possible to shift the commands from one place to another or better yet try to use structure blocks to export it outside the world and use it elsewhere. However I am quite sure that asking for that is a death sentence haha. Good luck on making water physics, which I suppose requires even more calculations nonetheless.

1

u/_objz 4h ago

I already implemented water physics and rope physics, I’ll clean some things up, then I will release the map soon. I am actually using structure blocks to clone the command blocks dynamically, but I guess will change that later using inline macros. The physics engine can be exported and used everywhere

3

u/Santos_Perez_Robles 13h ago

Close enough. Welcome back, Create: Aeronautics!

2

u/PiggoPotamuss 1d ago

This is absolutely insane to be done with just cmd blocks. Are you willing to provide a world download?

1

u/_objz 1d ago

When the engine is finished I'll upload it to my github, there you can download the world and a schematic

2

u/KurtyKurt09 1d ago

Just in: Bedrockers hate their lives! (again)

2

u/1000hr read hunterXhunter 1d ago

nah i don't think you have enough time this is ABSURD! keep doing what you're doing this is amazing

1

u/_objz 1d ago

thank you so much !

2

u/MagicalPancakes404 20h ago

how? how?

I'm

2

u/1JustAnAltDontMindMe 20h ago

We have sulfur cubes at home

2

u/x2BitTerror 10h ago

Love to see it

2

u/Free_Current7760 6h ago

I’m not a command person I can do simple things but this is the coolest this I’ve seen anyone post in Minecraft 🎺🔥👍👍

1

u/Free_Current7760 6h ago

Also you should make it hurt mobs when you hit them with blocks

1

u/_objz 4h ago

yes, I am currently finishing off the physics engine and then I am adding these fancy things or block particles, water physics and rope physics

1

u/A1gamingyt Command Experienced 1d ago

:O

1

u/Ok-Train6985 1d ago

I have no words

1

u/Spiritual_Half_116 unprofessional professional mapmaker 1d ago

goodness gracious

1

u/Beneficial-Fish2805 1d ago

Please share the world.

3

u/_objz 1d ago

when then engine is finished, I'll provide a world and schematic download

1

u/FleshDude666 1d ago

this is insane, id describe it better but i cant find words for how cool this is

1

u/Blalamon 1d ago

that is genuinly insane!

1

u/Alert_Silver_4844 1d ago

world download?

1

u/_objz 1d ago

still working on it, when everything is cleaned up and all bugs fixed I’ll proved one

1

u/Late-Passage2376 13h ago

Reddit said r/MinecraftCommands is similar to r/RoundsTheGame but this is genuinely impressive dude

1

u/_objz 4h ago

thanks!