I Added Realistic Physics to Minecraft -- 3D Rigid Body Physics Engine Datapack for Minecraft 1.20

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back Seth bling here it's been about four and a half years since I posted my last Minecraft Creation but I'm back baby this is a rigid body physics engine and it is not a mod this is a data pack I wrote for vanilla Minecraft that simulates the motion rotation collisions and physics of rigid cubes and however hard you think it was to implement this I can assure you it was harder before I get into what it took to write this first let me show you what it can do once you put the data pack into your world's data pack folder you can rightclick to create a new Cube and left click to smack the cube around these cubes will collide with each other and the world terrain you can control the size of new cubes with/ scoreboard Players set Global Cube size 6,000 and that'll make new cubes spawn with a diameter of 6 meters or whatever size you want you can change gravity with/ scoreboard Players set Global gravity 10 the default is 49 which corresponds to 0.049 m per tick squared which is the same as Earth's gravity SL scoreboard Players set Global friction zero will turn off friction the default friction is 800 which is pretty high similar to rubber tires on pavement the variable that controls how bouncy collisions are is called restitution SL scoreboard player set Global restitution zero will prevent things from bouncing setting it to 1,000 will make cubes perfectly elastic which basically makes them bounce around forever each Cube has its own mass and rotational inertia or actually inverse mass and inverse rotational inertia setting SL scoreboard Players set collider one in mass and invr int to 100 will make a cube barely respond to punches and setting them to 2,000 will make the cube a little touchy each Cube has a buoyancy value the default is 200 setting it will change whether it syns or floats I've never ridden or even really played around much with a 3D physics engine before so to start things off I read a textbook game physics engine development by Ian Millington I read it in about a week then started implementing it it's a great book but do look out for some sign errors in the querian multiplication formulas on page 158 they may lead to some expected Behavior as I said this physics engine is not a mod but rather it's a data pack a data pack is a ZIP file that contains a bunch of MC function files which are just plain text files that each contain a series of Minecraft console commands it's really like its own little low-level programming language I'd say it's more powerful than assembly in some ways and less powerful in others one of the difficulties is that the only way to perform arithmetic using Minecraft commands is to repurpose the scoreboard which only supports 32-bit integers that's right there's no floating Point numbers in order to do all of the calculations necessary to run this physics engine I had to use fixed Point math basically each number is scaled up by a factor of 1,000 the number 2.53 would be represented by the integer 2530 and there's no built-in math libraries Matrix math I had Implement that myself square root I implemented it so you might be asking what all did I actually write myself and how much of this behavior is just built into Minecraft allow me to show you this is called a block display entity and it was added to Minecraft about a year ago what's special about these is that they have something called a render transform which you can edit directly via Minecraft commands a render transform is a 4x4 Matrix that stretches SKS and translates a block in this Matrix there are 12 different values you can edit and here I'm animating one value at a time along the diagonals you can see the Matrix elements that control stretching in each of the X Y and Z directions here on the side you can see the elements that control translation in those Dimensions just linear movement back and forth the rest of the elements control skewing which is kind of hard to describe but you can see here what it means so over here I've written a data pack that modifies one element at a time we start with an untransformed Cube and apply very specific values to each element of the transform Matrix one at a time until finally the cube is rotated and also scaled up by a factor of 1.5 and that shows you exactly where Minecraft's built-in contributions to the physics engine end block display entities don't have any form of collision with Minecraft terrain or with other entities the only built-in function is to literally just display a transformed block and I had to set all the transform values I had to do everything else we can use the Save State function of my data pack to really dig into what it takes to run this physics engine every time you hit a cube it makes a Save State and we can load that save state with this command by using slash tick freeze we can slow everything down and look at the physics simulation frame by frame the actual physics of moving cubes isn't really so complicated there's linear momentum the tendency for an object to continue moving in the same direction and angular momentum the tendency for an object to keep rotating about the same axis where things start to really get complicated is when two objects Collide and really most of my time on this project was spent on the Collision detection engine for cubes there are two main kinds of collisions there's collisions between a corner of one Cube and the face of another as seen here and then then there's collisions between the edges of two cubes figuring out which parts of the cubes are colliding where exactly they're touching what their relative velocities are at that point and what directions they should bounce off of each other comprises the bulk of the code of the physics engine for me it involved learning a lot about 3D geometry which I found out I don't have a lot of intuition for every time I thought I could fudge some math it always immediately turned into some Physics breaking bug so I learned the hard way that I had to be really rigorous about how I implemented all of the math and again all of this had to be done with three decimal points of precision it was a huge task but luckily over the years I've been building up a compiler to help me get the job done and along with the release of this video I'm making this compiler open source on GitHub this is called CB script and it's a transpiled language I started working on back when I used to actually use command blocks for everything that's where CB comes from it's a higher level language that makes it much easier to manage a big project like this it's what I use to write my Atari 2600 emulator and in fact you can find all of the CB script source code for that emulator on my GitHub as well in theory you could compile this physics data pack for yourself if you can get the code working on your machine just don't expect any tech support from me to give you an example here's the code that detects collisions between cubes and the world geometry for a software engineer like me this code is much easier to understand to work with than MC function files it's got things like for Loops if else blocks and arithmetic Expressions where each of those require multiple very verbose commands to implement by hand with Minecraft commands all of this CB script code is compiled down to the 4,950 commands that comprise the data pack there are some limits to this version of the data pack left and right clicking will only work in single player although the cubes themselves will run just fine in multiplayer once you spawn more than about six cubes you can really start to run into performance problems cubes have a hard time resting on top of each other and usually they're unstable in this configuration if you whack a cube into a corner hard enough you can get it to clip into the wall this happens because of the way opposing forces on opposite sides of the cube can cancel each other out if the situation is just right most blocks will be treated as full cubes even things like tall grass or flowers these problems are all solvable but I didn't need to solve them yet in order to publish this video as I use this physics engine for more applications I'm sure I'll start solving them as necessary before I close out the video I want to give a couple of shout outs to some other related projects YouTuber mysticat is at least partially responsible for getting me to come back to YouTube by showing me some cool display enti Tech and they worked with me to get set up with my physics engine to build one of those stupid mobile games where you pull the pins to solve the puzzle they published a video about it you should go check it out after this one very shortly after block display entities were added to the game Pat boox wrote a server mod that does something very similar to this since this runs in Java he was able to make use of an existing rigid body physics Library called rayon written by team Lite it's got much better performance and probably way fewer bugs than my engine and I also want to shout out barf who is writing a physics engine data pack based on Point Mass Aggregates rather than rigid bodies this type of physics engine can allow for much better performance at the cost of some physical accuracy as always there's a download link for the data pack in the video description you can also find links to my GitHub which contains my CB script compiler as well as those other physics projects in there I definitely plan to use this physics engine for some really cool Minecraft creations so do keep a lookout on my channel for more content that's about it thanks for [Music] watching
Info
Channel: SethBling
Views: 395,616
Rating: undefined out of 5
Keywords: Minecraft, Physics Engine, Rigid Body, Datapack, CBScript, Physics
Id: DhCBCudKJTs
Channel Id: undefined
Length: 10min 18sec (618 seconds)
Published: Sat Mar 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.