GameMaker Studio 2: Rope Swing Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody after a vote by my patron sports we're gonna cover a topic that a lot of people have warned me to cover for a very long time and that's creating rope slings okay so I've got this little setup for a platformer here where I can clip just about anywhere and grapple onto that air of a space and it gives us a kind of worms like rope that we can kind of swing with I can kind of influence it left and right by sort of swinging in that direction I don't swing at all it just acts like a kind of regular pendulum it sort of eventually brings us to a halt here I can climb up down the rope if we go fast enough we go all the way over the top as well the source code for this will be available in the description for my patreon supporters and anyone willing to drop a few dollars to get instant access to that or you can just watch this video and I'm going to show you how to write all the code anyway so together most out of this video there's going to be a couple of things you'll want to have a reasonable grasp on beforehand okay and that's first of all just being able to make a platformer in the first place this is all very simple the other thing you want to know about is state machines at a very very basic level what I mean by a very basic level is literally for the player we have a regular state which is in moving around and jumping and all that kind of nonsense and we use a switch statement and it's coding and see here switch them and based on the state to determine whether or not to use the regular normal state code for running around and jumping or whether to use our swinging code okay and just being able to split our code into these two cases that's all you really need to understand if you don't know anything about this okay I do have a really good video on state machines you can go watch I'll be in the description as well so this is what a platformer looks like now it's all very straightforward just clicking anyway doesn't doesn't actually do anything yet we can just move around and jump we've got a little bit of preservation of momentum because we're doing acceleration and friction and stuff that changes based on whether or not we're in the air or in the ground rather than just setting our speeds okay so before we get started let's just take a look at the basics so this is my players step event at the top here we've got our input gathering and in here this might look a little bit different to you but it's just the same section as before where we work out what our horizontal speed and vertical speed should be based on Iowa input so whether we should be walking left or right or jumping it's a little bit different because it uses acceleration so our accelerating hate speed B speed by given amounts rather than just setting hate speed as we were before by saying if it's left walk four pixels left if it's right walk four pixels right we're actually accelerating by a small amount which isn't very different but it is marginally different to look at if you're not used to it okay and the other difference is that it's in a state machine so we've got a switch here or a variable called state and that state can be one of two things from an enum so in here it can be P state normal or pede state swing and in our normal state this is where we do all our running the jumping stuff selects doing the code that happens normally and then when we're in a swing state we'll have some code in here and that's going to handle all of our antral rope swinging when physics e nonsense okay and then under that switch statement so after I switch name is finished we do our classic fraction handling and collision code down here and that's all exactly the same as it usually is okay so that's our basic setup okay so that's all so let's now go over how we're actually going to do this rope swing in our normal state what we're gonna do is at the bottom we're going to check to see if we have clicked the mouse somewhere okay so a simple check on the mouse button and then if we have we'll change our state from being P state normal to P state our swing and then when we do that we're also going to store the position of wherever we clicked and determine the angle and length of the rope which will be the angle and distance from the point where we clicked to the player okay that line is all gonna work out this is obviously just a quickest setup for this as well how you want your grappling hooks trying to work in your own game is going to be very specific to you whether or not you want to fly to town to project Holograms on the walls I have only certain spots you can grapple on to whether you want it to instantly latch on or not that's all gonna be up to you that's a deal in your game I'm just here to show you the finger quotes physics of the pendulum more than anything else so let's set up this stuff in the state real quick and then we can actually get on to talking about the swing itself which is the intro thing sign pot so in this case statement for normal in our normal state just at the bottom here after we've calculated all of our assorted movement from moving left and right and jumping and there type it Moss Shack button press nd black I'm gonna make this a bit bigger just so that we can all definitely see what's going on in the video there we go oh maybe a couple more there we go we'll need to be sure so if we've pressed the left mouse button I'm gonna get the x coordinates of where we clicked on the create a variable called grapple x equals mouse at grapple y equals mounts y rope x equals x robe y equal Y so rope X and rope Y are going to be essentially our x and y position for a bird just the end of the rope the end of the player is on which is obviously when we first shoe it gonna be just exactly where the player is okay a couple more things we need to declare has a rope on little velocity that's gonna be how quickly the angle of us or a pendulum circle is how quickly we're swinging basically is gonna be 0 you can decide yourself if you want to carry across some momentum from what you already have and how exactly you want to do that that'll take some messing around but I'll leave that one for you guys to work out rope angle is going to equal point Direction grapple X grapple Y X Y okay so that's going to be the angle from wherever we are to wherever we clicked and then we also want to work out the distance so a new rope reflects equals point underscore distance again grapple axe grapple y x y Sammy come on and state of course to equal P state dot swing okay that's just simple you know my setup in the creative am you know P state normal and swing so that we can just easily distinguish between our two states you could just use Norton one as well if you don't know how he numbs work you could just write state equal one put one here for your case and 0 here for that case but enums are pretty easy so that's why I've decided use this so I once we've done that that's worked out everything we need in the actual normal state because once we click someone we worked out all the stuff we need to do with our rope and then we sent us to the swing states on the next step we're gonna follow what code is in here and skip all of this code okay okay so now that we're actually in our swing state what do we do this is the really the hard part right while there is maths out there for calculating like pendulums lots of formulas that kind of thing they're not all that practical or necessary for what we really want to achieve we can actually achieve what we want in a game context um with some really simple stuff that doesn't really have any physics to it at all ok we're just going to boil this problem down to its most basic components like how should a swinging pendulum behave visually what do you notice about a pendulum first of all they move in a circle right because they're limited to the length of the rope itself and they seem to accelerate fastest when the rope is at a right angle with gravity and slowest when it's parallel with gravity okay that's why I like it like it speeds up there and it like slows down as it gets into the middle slows down when it's at the top speeds up when it's at the sides okay that's how pendulums have hit behave so we can use that fact alone on how we observe pendulums just to be to create something that behaves closely enough to a real pendulum for pretty much any game context okay and we can do all that with just one line of code that's really cool that line is var rope angle acceleration equals negative not point to a bit of a magic number we'll come back that x D cars Open bracket rope and we'll close bracket semicolon and that's it okay that's the whole line so what does this line to rope acceleration is going to be the variable in which we store how quickly our player is accelerating along the angles of the circle that we know exists because we're pendulum right based on the length of the rope at any given time so how do we work out what that angular acceleration should be well what I'm going to do is just use cosign D cuz as opposed to cause just means that we're working in degrees rather than radians okay it doesn't change anything about the actual calculation itself and cosine if you might remember from some of my other videos if you watch them my other stuff or you might know in general is very similar to sign okay and what these things do is they're from trigonometry and maths and all you really need to know in case you're worried that we're gonna go into some massive stuff you don't understand is that cosine looks like this okay you put a number into cosine and it brings you back a number between 1 and -1 and along here you can see what numbers those are that go into it this is assuming we're working with degrees which we are we're using D cards to work with degrees you can also just regular cars uses radians which is a different thing you don't need to worry about but you can see at zero cosine returns one and you can see it bounces as we go up in numbers between 1 and minus 1 okay and then back to 1 again back to minus 1 and so on now what's important is cosine just happens to be the perfect arrangement for what we want to achieve we want to accelerate fastest when we are right angles with our pendulums Center and accelerate slowest when we are vertical when we're parallel with gravity now if you know how a circle and game maker or game development works or how degrees and handles and game development work they start at 0 degrees is the is facing to the right to everything motor pole facing to the right and then as we go up in degrees all the way to 90 90 is facing up 108 years left 270 is down and then 360 and 0 are both facing right again okay now bearing that in mind what we want to do when we are at zero degrees is essentially go backwards we want to apply negative numbers okay and count downwards in order to accelerate and look in the correct direction of motion that we want and when we are facing to the left however we want to increase our angle okay we want to go from 180 upwards towards 270 okay so we want to really be accelerating downwards luckily cosine letters do exactly this because when we are at and zero degrees we get a 1 okay when we put zero degrees into cosine we get a 1 back and when we put a hundred and eighty degrees into cosine we get a negative warm back now you might think wait that's the opposite of what you just said you wanted negative signs right well that's why in our line of code and GameMaker we simply multiply it by a negative number but I'll come back to that negative number and why it's pointing to on the 1 and a little bit if you just think about this for a second that means zero degrees when we're facing to the right we've got a number we've got one from cosine that we multiply by a negative number so we get negative nine point two as our acceleration so that's what we want heading down towards back towards 270 or negative ninety rather I guess and so that way and then when we are all the way the other side when we're at 180 degrees we can get minus 1 minus 1 times minus 9 pointing is gonna give us point not to again accelerating us toward 270 which is all we want and then importantly which were salat gives us our pendulum feel when we're at 90 degrees we just straight up or 270 which is straight down we have an acceleration of 0 so our velocity is not going to change at all okay which is what we want and then we if you add a little bit of dampening on top of that means we entry slow down at those points as well and it's great okay that's it that's why just this simple graph and this very simple function will get us exactly what we need from our pendulum in terms of this behavior ok just that one or not - one popping back and forth between them and importantly of the correct angles in the middle being a zero is just exactly what we want so I've chosen or point two here as long as it's a negative number you can use whatever you want here it's a complete magic number that I've used you might want to put something in instead like rope acceleration rate or something like that and put in an actual variable I'm just keeping all the numbers here so you can see very clearly for the sake of the tutorial how everything works okay I do that a lot but I feel the need to explain it every time I do it because it's important cuz magic numbers and not a good habit so do replace this with a variable if you want to they use this stuff so now that we know our acceleration should be we just kind of need to apply it and that's really all there is to it so I'm going to type rope angle velocity that's gonna be our actual speed as a result of this acceleration plus equals rope angle acceleration okay so we know our rope angle velocity is style four zero and we're gonna add whatever this is to it so I'm getting faster and faster and faster when we are at the right and when we're at the left and we're not gonna get faster when we're at the bomb or the top okay so we gain the most speed the quickest when we are at those positions and we don't gain speed when we're at top and bottom as we should so rope angle plus equal and more velocity okay so we take that velocity we just add it to our angle so our angle along this circle increases then rope angle velocity x equal or asterisk equal nor point ninety nine this is just a dampener so that when we are at the bomb and when we are at the top or actually just at any point in the circle we slowly reduce our velocity which will allow us to actually swing and slowly kind of come to a stop as well if we're not applying any other forces okay which is just to represent air friction right and just to me off your right you might not even want it depending on your game you might not want any dampening which is just just take that line out and then you don't you don't necessarily need it for this okay um so that's our rope angle increase but obviously this is just a number that's changing we're not actually positioning our player anywhere so that's how are we going to do it how do we move in a circle well hopefully you've maybe if you've seen some of my stuff before we're going to use our good old buddy length the X and length though why the absolute easiest way to position something in a circle if you know the distance from that circle which we do we know the rope length and we know the angle which is literally this okay so rope X because our x-coordinate at the end of the rope and you can call grapple X which is our x coordinate way of grapple two plus length to underscore X make sure you wrote X not Y rope going as the distance and rope angle as the direction okay simple as that and then rope y equal graph for y plus point the underscore Y make sure you write y that's very easy I make the mistake all the time is the right length I like X twice and then let's come and make sure you've read why they're very easy mistake to make rope length rope and you got again okay and then that does the same thing for the like one okay so now we know where the end of the rope should be we can set our player speed accordingly so H speed equal broke X the reason we're doing this rather than just saying oak walnut is so that we can make use of our collision code afterwards okay just the same as before and be a speed equals rope y minus y so that's finding the difference between where the end of the rope should should be and where we are currently and that's just saying ah speed to be that all right and then all we need after doing all this is a way to leave the swing state so I'm gonna say here B jump or whatever variable you stored pressing the jump key in states equal P state dot normal the speed fraction equals zero the speed equal negative jump so the last thing we want to do is come down to the collision section so this is my classic collision code just a wild statement moving this as close to the wall as possible and then stopping what we want to do is if we are in the swing state and we found the collision in either the horizontal or vertical so if a state equals P state swing you know if we're in the middle of a swing we found a horizontal collision here I want to make sure that the rope doesn't go on without is because the rope is being calculated every step in here so I want to make sure we collide properly after colliding we set the rope angle to just be wherever we currently are so I'm gonna type rope I'm going Evil's point direction rattle X grapple Y X Y and then I'm gonna sell speed to be zero so rope and your velocity equals zero you might have some fun experimenting with this like try saying it to just- whatever it was just rope angle wasit equals negative rope angle voici I even tried that I don't know what it would look like I imagine that might give you like a bounce or something coming off the wall play around with it and see you want see what happens anyway once you've written that in there Coffee the section you want to also put it in the vertical collision sanction I don't like duplicating Cobra was the only way I could immediately think to solve a problem importantly if you change it you have to sort of change it across both that's why doing that sort of stuff isn't great but that will work nonetheless ok so once you've got that in both of those that's everything we need to do in terms of actually calculating positions the last thing I want to do is go to the draw event okay if you don't already have one just right-click in the code go to add open event draw draw okay and they can draw them make sure he's got draw self and here you'll need that obviously to make sure the player continues to render themselves modeling family to a draw step but the other thing I want to add in here is first of all check to see if we're in this swing state so if state equal PA state don't swing draw line whip grapple X grapple y probe X rope Y and a width of two okay just gonna draw the line from our player to wherever they've grappled you can do that however you want though that's just the simplest okay and that really is all there is to it if I'm not forgetting anything so I'm gonna run the game now and hope for the best okay so I'm gonna click somewhere and here we go we've got our grappling hook okay there's one more thing we want to add to this and that's our ability to manipulate this ourselves because at the moment it's just automatically calculating the swing I think dampening should make it so that eventually you know it comes to kind of a hole like that and you can see how the acceleration is working as well I can't accelerate most when were at the edges and the least when we're sort of near the bottom okay so big acceleration and then it slows down and goes the other way and it slowly pulled the other way as well cuz the acceleration slowly increases in the other direction as we swing okay so it's like you would expect a pendulum team okay so last couple of things that's going to the staff event back up the swing state okay so just after we worked out where our rope angle acceleration should be I'm gonna type rope angle acceleration plus equal key right - e LF x naught point naught 8 okay it's a very big value for this you'll see it has a pretty pronounced impact in a second just so you can definitely see it working again this is just the exact same logic we use to move left and right in the platform right that's how we work out which way we're moving key right - key left it multiplied by a speed number of some kind you might want to call this rope manual acceleration raped or something you know whatever variable name you want to put in that okay and and then you can play with that to make it more or less as you desire and then the only other thing is rope length plus equal underscore key down - underscore key up times - okay and that's the exact same thing again as left and right just enabling us to pull reduce the length or increase the length of the rope any given time you might also want to add something like rope length equals um max rope length fine just to ensure that it has a minimum length okay so return whichever is bigger out of five or whatever rope length should be just so you never end up with but I like going into negative rope or whatever right I suppose I should take me in as eros at the moment you can click anywhere if you clicked within five I guess that might cause weird issues but there we go so if I'm run in that well we should find now is I can actually manipulate the length of the rope like that and I can also start to actually swing as if I were on a swing in any given direction and go or if we get enough we can go over the top like that which is pretty cool I think we're not doing it in oil it'll dampen and slowly bring this back as I say it's pretty extreme value like if I just hold right we even find eventually even when it like dampens all the way down were like like holding ourselves right but you import make it however you want it to be ok that's pretty much all there is to it as I'm not gonna go into like like doing sprite or whatever and things like that you can work out how you want things to be for your own specific game how you change sprites and its state machines is all fairly straightforward you know should hopefully know how to do that kind of thing by yourself I hope you found this one useful I took a lot of research to work out how exactly one of the teacher I hope you liked this method or at least found it interesting and useful cosine is a hell of a drug that's pretty cool that's a pretty useful pretty useful function I hope you enjoyed this video I know I've not been putting out a lot of stuff lately there's been a lot of personal stuff going on in my life but most that's all out the way now so hopefully we'll be seeing a lot more stuff from me in the very near future thank you as always to my patreon supporters for supporting the work that I do I couldn't make any of it without them thank you in particular and in no particular order to mark Clint's Dan Mike blankie Thomas Vanda named run turtle time valve Cody Hodkinson Louis R Pereira Sofia flame Rovin darlin Robert churches TJ James grimly Patrick Duffy Michael Ward matte coat Zen anime Shana --then mix lavish Steven Hagen Bowser the dog Harold Guidry Jason Macmillan and Owen Morgan thank you all so much for your support and thank you for watching thanks guys catch guys next time
Info
Channel: Shaun Spalding
Views: 32,389
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Tutorial, GameMaker Tutorial, GameMaker, Making Games, GameMaker Studio 2, GMS, GMS2, Rope Swing, Rope, Grappling Hook, Grapple, Ninja Rope
Id: 2prKyETuTaA
Channel Id: undefined
Length: 25min 45sec (1545 seconds)
Published: Fri Jul 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.