Generating 2d levels

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
these platforms are placed randomly but building this is not as simple as it looks there's actually a structure to the randomness here's what it looks like when the platforms are placed completely at random with no structure at all the first example is much better for our game but to understand why we need to look at why we're using Randomness at all the game I'm making will require you to play the same level multiple times as you get better and as you upgrade your character so to keep things fresh we want to add a degree of variability however there is a design trade-off if we don't use Randomness we would be able to handcraft a thoughtful level that perfectly meets player capacity great for the first run but it would get dry after some time pure Randomness is just chaos we need to take some of that thoughtful design and apply it to the algorithm that places the platforms let's start with the version of just pure Randomness and keep improving it until we get something that makes our game fun this journey ended up going much deeper than I was expecting first we want to make sure our game is even possible that means when we generate a new platform we want to place it somewhere that the player can actually reach in our current algorithm we fail this almost immediately right now we're just generating X and Y coordinates for the new platform and there's no guarantee that our player can get there let's improve this by placing each new platform within a Zone around the previous platform this way we know the player is able to reach it but how big is this Zone and more importantly what is the shape our player can jump a certain distance up and a certain distance across so you might think that the next platform needs to be inside a circle around the previous one but this turns out not to be the case the vertical velocity of our player is not affected by the horizontal velocity of the player that is how high we can jump it's not affected by whether we're moving side to side this is a similar concept as a classic Game Dev mistake in top down games where people accidentally program it so that you can walk diagonally much faster than up or across for us in vertical 2D however we actually want this because we don't want a player to jump just because they're moving we can see the shape we're looking for is not a circle perhaps we need a rectangle well this is sort of true but it misses the fact that once we get to the top Corner we can keep moving to the side while we're falling actually what we find is that the shape of the Zone around a platform that our player can reach totally depends on the code and how we have written the jump function so for my character it looks like this there are two parabas going out from each of the sides of the platform and then this top part is also filled in before we get carried away with the mathematics of how to generate a point within this shape uh we're not actually solving the problem for two reasons number one we don't want the player going down and backwards we want the player to keep progressing forwards and upwards and number two my character's movement is going to change a lot not only through development but also through the game as you unlock double jump and dash and so forth so the true shape that the player can reach is hideously complex and I don't want to change the algorithm every single time there's a new upgrade or change to the code we're going to need something more generic let's go back to our Circle and scale it down so that we know that it fits within our larger more complex shape and try to improve it from there as I mentioned before we want our player to progress so let's split our Circle diagonally in half and only generate platforms on this side of the circle this whole time we've been worrying about platforms being generated too far away but we also don't want them too close let's cut a circle out of our shape in the middle and this is the final shape that we're going to use now we need to generate X and Y coordinates inside this shape and it turns out it matters how you pick them so we need to pick two numbers X and Y such that together they describe a point in this region if we just pick any two numbers at random then we find that they fall on a square there is a technique where we just accept this fact and if we generate a point inside the shape then job done but if we generate one outside of the shape then we try again and if we get it wrong again then we keep trying until we get a point inside the shape but this approach is quite messy and slow the calculation to determine whether the point is inside our shape is quite expensive and we don't know how many times we need to do it there's also the possibility that we just keep missing and this would create an infinite Loop that would break our game my next thought was why don't we pick the numbers one at a time so first we pick let's say x and then we calculate what is the allowed range for y based on X now this will work but we're going to need to talk about distribution the problem is this method doesn't pick points evenly across our shape and we can see this by looking at the fact that half of the time we're going to pick an x value to the left of this line here and half of the time we're going to pick in this other region it doesn't matter what Y is uh we're going to get half of our results in this much smaller region than in this bigger region here we can fix this with some frankly quite interesting mathematics but there's a better way to do this and it's using polar coordinates what we do is we pick an angle and a distance from the center and we pick those separately and then convert that into an XY coordinate that way we can have the distance within the range that we want uh and we can have the angle within the range that we want now this algorithm also introduces a bias but it's actually a bias that we La if we do the same analysis as before where we cut the shape in half we can see that yes a smaller half will get just as many points as the larger half but if we think about that differently we'll see that any new platform has got an equal chance of being any distance away from our starting point the other platform there is yet another benefit to this method which is that the angle and the distance are both meaningful gameplay values that means we can change them at any time and it will result in meaningful change in the game play that we actually want and this is it
Info
Channel: Nick Brooking
Views: 2,531
Rating: undefined out of 5
Keywords:
Id: wux3roxJu9s
Channel Id: undefined
Length: 6min 11sec (371 seconds)
Published: Sat Jul 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.