Hello welcome to uh tutorial number 10 where we’re using peta amlan’s a cena engine to make uh pandas amongst other things um minecraft like clone simulation so last video was dedicated to getting this little guy uh what’s his name greninja greninja 28 named by guy um moving around and following us i’ve have You as you noticed i’ve now spent 10 minutes to give him a much nicer texture and so it looks like a panda um i’m only generating 12 um chunks of uh terrain at the start i’ve just remembered anyway let’s just generate a bit more and then turn that Off so we’ve got his animation um working and things in this video i want to begin by just sorting out the rate of rotation i ended the last video um with a mistake i was thinking that i could put in the second argument so i pass in the second argument to the Um look at function which is doing the work of turning our mob around like this but you can see it’s following me instantly and you might want our panda friend to kind of look a little bit more slowly um so i found a way i can’t believe it Worked but there’s just one line we need to code so let’s go and do that so we can just use um lerp which we’ve used before in fact if i scroll down now i’m in the main module for where we move our player our subject down here Line 87 in the current version of the code we are setting our subject’s y position so up and down using this lerp function which is linear interpolation which basically just means like moving something from a to b like that so we go from our current position is the first argument to The target position y which will be in this case the height of the terrain block and then finally the speed of that interpolation so what we’re going to do in our mob system is oh yes by the way i’ve got my three asterixes here meaning yeah i’ve i’ve Created a new texture if you want that new panda texture you don’t want to make your own i’d recommend make your own on blockbench or whichever whichever soft piece of software you want to but if you want my one it will be in the the assets folder online link is in this description Uh so it’s panda underscore texture that new one i think the old one was called panda tex um maybe you like the old one which makes the panda look like a cow anyway so we’ve got um so our panda called grey we’ve got turn speed and at the moment i’m applying that Um here as i said line 15. um yeah our bug this second parameter is not affecting um our our model at all so in fact we can take that out so take that out there um what we want to do then is use linear interpolations so what We’re really doing is saying well our mob’s uh rotation underscore y so we’re just using the the your rotation which is like that equals and so we want to use lerp and say well we want our current rotation there we go and now we need a target rotation so what’s our target rotation So here we have a problem because our target rotation is a mystery it’s happening behind the scenes we’re using a line 15 the lookout function which does all the work of finding the target heading and then rotating our model already so we don’t know what the target location is so actually What we want to do is say um oh really um our mob’s rotation on the y-axis oops then we’ve got the y-axis is already that the target is instantly moved there so actually that’s going to be our second parameter which is counter-intuitive so the first parameter wants to be what it was The rotation before we applied the lookout function so in other words we want to go and say what’s your original rotation so maybe temp original rotation temporary original rotation um so we’ll just make out that variable this uh name of course doesn’t exist as a keyword i’ve just made that up Contemporary original rotation equals our mob’s current rotation on the y-axis so that so now we can say we want you to move from this current rotation to whatever the look at moves you to to point straight at the player which is weird so it does actually move it there And then this line line 18 is going to move it back but then turn it a little bit towards that target and then lastly now we can this is the crucial thing we’re doing or the reason why we’re doing this is is to be able to apply some timing to that So then we can say we can use our turn speed so we can say gray dot so i’m in this function aren’t i where whatever mob we pass into here will be referred to using the the the parameter mob so mob dot turn speed and again we want times time dot delta Time to smooth that across different devices or running speeds on the same device okay um so that should work now um we’ll just up the turn speed so i derive that number from fiddling around with things and testing when it’s not working um turn to be not affected let’s remove That right let’s run that so mob system back in my oh that’s convenient back in my main module our main module apologies um we are importing that mob system module everything from there via this line in the main module anyway i’m back in the main module now i can run this To see if indeed our pandas our mob’s rotation is a bit slower oh damn it something’s going on frame animation 3d object has no attribute turn speed that’s just going to be a typo isn’t it because i’ve written turn speed with a capital s there some camel humping Down here i wrote it all lower case there we go and back to our main module and run i’ve got a cup of tea brewing i need to go and put some milk in that so i’ll have to pause this in a second oh isn’t that brilliant um our mob Gray has just disappeared randomly i don’t know if that’s happened to you on your computer let me know in the comments but sometimes because of the loading he falls down and disappears that we should write some some code to address that issue so we kind of need to to check some Maybe in the the mob’s update we need to say hey are you like minus 3 000 i.e have you fallen through the floor therefore you need we need to rescue okay it’s not there again that suggests there’s something wrong with um my code actually okay let’s have a look at the code What have i done wrong oh no i think we do need to keep this line 17 in there temp or time dot delta time yet we’ve got delta time maybe decrease the turn speed 0.1 this is what happens when you don’t have a cup of tea there he is Just walking away from me oh is that what happened maybe he walked straight off the the terrain so i need to generate some more train oh turning very slowly no it was faster before so he would have i don’t know looked at me immediately now he’s walking got our animation running lovely Now he’s turning slowly towards me oh going the other way now another thing of course that we’ll look at is you know glitching through the the terrain here basically we need to set we need to like kind of set a size for our mobs and have a property of the size so you Might be standing on four terrain blocks so in other words we have to look at those four terrain blocks and move to the height of the highest one out of those four i think that would be something like that proper result okay so that’s definitely moving at a Different rate so it’s partially working what i’m going to do in maine is uh generate maybe 32 chunks maybe 64. um mob system so we want him yeah moving at maybe one ten times as fast what have i done in my prepare code mob system speed one interesting You have a turn speed of one and then run that oh let me pause it go get my t and then i’ll run it together pause and we’re recording again right press play in the main module so might take a little longer to load this time If i said it’s who was it 64 chunks that’s not too bad and so it means our panda friend gray won’t fall off immediately so he’s looking at me you know what we’ll do at the moment sorry to restart again but he’s um part of his instincts are saying How close and they approach so if he’s 10 meters or 10 units away um then he’s not gonna get any closer let’s say you can get you can get three meters get within three meters of me cray so i want to just see a bit more interaction a bit more intimacy Um but i was happy with um how long that took to load that was okay oh can you believe it it’s glitched again i really want to know what’s happening there um so in the mob system in terrain walk let’s just put something here Um it might be a bad place to put it but um czech mob hasn’t fallen off the planet no i think that needs a winky face um so what we need to say we need to say is um if um mob as we pass in the mob’s uh What’s it called frame animation 3d objective thing uh mob if mob dot y is less than minus 100 then mob dot y equals um 32 hopefully that’s something like being in the air we’ll put 100 in the air oh okay and then we’ll say mob dot um It might be nice to put them in front of the subject anyway let’s just try that let’s see if it oh and we what we should do is so i print something out a little debug log print to the console i’ve fallen off oh i’ll do double quotes there so that We can use um apostrophes without a problem there we go inside the string right okay my goodness it’s almost 15 minutes in and all we’ve done is um kicked a panda off the planet a few times he’s off again so um anyway kind of like [Laughter] He did appear in the air and is he falling now yep there he comes there he is and he’s kind of doing a strange wiggle and now he’s turning to face me and he’s getting more intimate there we go so that’s three meters away Um if i go this way yeah it turns like more slowly so this is so we’ve we’ve done the thing that we want to do is use some linear interpolation to slow down or speed up this the rotation of our models um i’m reminded kind of of minecraft when An animal comes up to you this close often they look at you so you can think for yourself i’ll develop this later on but you can think of yourself and use what we’ve put in place already to accomplish this for yourself now if you’re anxious to do it you could have a System whereby this the head is animated kind of independently and follows the player almost instantly and that’s um which gives the a lot more kind of like liveliness to the uh to the mob but anyway i like that so there is a bug at the moment and if we go like behind Them he does a weird wiggle dance very i’m very proud by the way of the texture of the tail i think that’s really good oh there he goes a wiggle dance so it looks like we’ve given the panda some clever and interesting ai behaviors it’s just all bugs it’s all bugs Um and it’s probably a combination of um look at not being used quite correctly and um this rotation hack so at the moment our model is is reversed on the z-axis so it’s forward orientation is inverted so um we’ve used this little hack so i don’t know now if we take that out Presumably that would um make the animal flee from as i.e they would point away i think what’s happening perhaps sometimes depending on the loading speed is parachuting in i think the the mob must um load more quickly sorry than the terrain and therefore it’s underneath the terrain you can kind of overtake him Yeah he’s running away from us oh and we’re not taking out the um sorry we’re not taking out the unwanted rotations now which kind of looks good when he’s running away from me up an incline it looks like he’s tipping upwards which is nice yes There we go um have i got the cheat sheet up i have there might be a way i did just read something was it just to do with entities about reversing it on the z-axis repair to get position combined flip faces oh look at 2d might be better for us um I think it is on here isn’t it um set position right oh there’s also smooth follow somewhere which might be a better built-in way of getting our mob to follow the player um combine flip faces get position ready repair it to generate cube map set shader input attribute bounds while i’m just looking Here a few people have commented about using um a shader which i’d absolutely recommend but i’m using a mac and so the shaders won’t work yet or i haven’t found a way to get them to work with a scene as in built like shaders and things um To work that i did try on a windows machine and there was also a problem i got lighting working the shade is working but the terra it didn’t work well work with the um texture atlas basically it um it set the the texture the texture atlas to every single like The whole texture alice to every single block on the terrain um which is a big problem so i kind of abandoned that um i was talking to someone on the issues page um let’s have a look sky and moon was that it just a jump jump to something else it’s loading really slowly There we go so it’s um dangcap07 um i had some nice ideas about yeah using the moon and the sun and rotations um oh and i didn’t go into detail here no it must have been on another comment anyway basically i was thinking like uh the way uh minecraft works is every like Block area in the whole world has um a value for how light that area should be so like where i’ve applied different random tints or shades to each like grass block you could have a look at how far down underground it is and get darker and Darker and darker or what time of day it is to get darker darker and then apply that color to the to the entity to the texture um also we can use fog density and we could apply like a black color for night time and then get the density can get thicker and thick And thicker and so um creating like a nighttime effect so we can we can do some things with with lighting without using shaders so we’ll experiment with that um anyway i was looking for i’m sure there was something about forward get the forward direction get the back with directions like re-setting The um forward direction of an entity and i’m sure it said it in here somewhere um let me what i’ll do i’ll pause it so i’m not just looking through a list on screen and then if i find anything or not i can unpause okay so i didn’t find it however i did Find um smooth follow this function here so it looks like we can set a target offset from that target speed and rotation speed so we might be able to use that one line alex you can experiment with that yourself um and it might be able to achieve um in one fell swoop What we’ve done in our mob movement with several lines so yes i’d recommend that having a look i might have a look at it later on anyway the second thing we want to do in this on the main thing we want to do in this tutorial video is sort out um Collisions for walls so ah i’ve got my prepared code here so this is what we’re going to do so an overview so you can get straight to the code if you want we’re gonna write a new module called like bump or something like that i’m gonna pass into the bump the subject And the terrain object um and it looks like oh look i’ve put in um these lines from at the top of the main module so um the top of the yeah terrain uh sorry the terrain collision walking around algorithm that we’ve written so fast so That the player is on top of the terrain um so we’ve taken that top it off so so basically we’re writing a module where we can finally get our walk script stuff removed from the main module which is good news um so we’re also um looking at walking into walls as well so It’s kind of like horizontal collisions and also vertical collisions as well right so what we’ve got is now a function inside this function which i haven’t used often and that’s called check bump so it returns true or false so are we bumping into something and then i’ve written um a comment here Input f what does that sound is the location of the block ahead behind side etc so it’s the location of of a block um and we’re gonna check if we’re bumping into it and what have i written a little for loop i in range one step one so that it looks like it’s Checking from the players kind of midriff height or your knees upwards and saying is there any block there therefore return true and to see if there’s a block there we’re checking the terrain dictionary and if we see a t mark there standing for terrain there must be a block there So returns true else returns false now i’ve got some new like settings i’ll go over this when we build this how close can we get to that wall so i’m saying 0.555 that’s how well how far we’re checking ahead of us um then we’re getting that position um Actually the r position is a subject’s position rounded not flawed and then the subjects forward is the subject forward where we’re facing and the subject forward dot y is zero oh so we’re kind of like zeroing off the y axis in case we’re like looking up or something like that we just want Forward straight ahead of you not like looking up because the steeper the angle your forward might be hardly forward at all in terms of x and z and of course we want to check x and z in terms of vertical bumping um now i’m saying b direction that must be bump direction equals So our position plus the subjects forward multiplied by or how close are we going to check and then we pass that b direction that bump possible bump direction into the check bump um function that’s inside of our bump wall function and so what that does is take hold of that that um Direction that location and it just looks up and down on the terrain dictionary because they’ve got different y positions so you might be able to bump into something there in your head or something a bit lower down so it has to check up and down now i’ve written a function here Because we also want to then check if that’s happening behind us left and right so where i’ve written subject forward before then we also want to look at um minus subject forward so that’s behind us or to the subject’s left to the subject’s right And so on um so all of those will refer to have to do that check of looking up and down the dictionary on the y-axis so that’s why we’ve got a convenient function to do that for us um and you might notice wait a minute why have I got this check bump at that forward position for example why am i also looking at that direction plus left and plus right well i noticed when we’re bumping into something and we come from a diagonal we might go straight through the wall and also we could walk a diagonal at a Wall and still pass through it so we want to kind of look at the the uh block or the space ahead of us but also the space left and right so like three blocks altogether close and if we do bump into that it means that we are We are going to like hit that object so all of those need all those positions um according to x and z need to be checked and then this function checks all those positions up and down the y axis so this check bump thing will be referred to a lot Because we’re doing all four directions and for all of those four directions we’re checking three blocks so there’s a lot of checks going on okay so i’ve written um yeah i’ve written that function for that reason right and then yes underneath that so it’s right left uh behind and in front And what we do once we pass those areas to check are we bumping into something if we return true to that ie we find some terrain in that position so we’re going to bump into it or sidestep into it or reverse into it so if we return true and then if that’s True we say held keys w so that’s walking forward equals zero so even if the player’s got their w held down we’re immediately switching that off so you can’t physically move anymore um that seems to be the best way of doing it i experimented with physically moving or kinematically moving the player backwards Turning their speed off and on and nothing worked quite as well as this so that’s the method i’m using and so if you’re going backwards then s will be needed to be turned off left a and d so wsad obviously change the keys if you’re using the arrow keys use You know that the corresponding arrow keys here or whatever keys you’re using um there we go so um underneath that is the um i should label this again this is our walking script walking on the terrain itself there we go so this is what i’ve joined out of the main module And brought here which is lovely and then we’ve got our original lap okay and our gravity at the bottom so that’s this code at the bottom is exactly the same now what’s slightly different is we haven’t got the the variables to do with step height and things here because we Use those actually in our calculations for vertical collisions so uh we set those right at the top the most important ones are the x z and y the the subject’s present position and we want to round those instead of flooring them um so we’ve got a step height and we’ve Got our players height block found is set to false to begin with right in fact because i’ve explained all of that and the code is available for you to kind of like copy and paste or have up on your screen to to write yourself i don’t think i’ll um recreate it from scratch Since i think that’s fairly simple to explain but um let’s uh incorporate this in our code at least so i need to save this as or rather let’s go and create a new module and set this to um python and then we call this vertical collisions ie not ghosting through walls etc oh You know what no it’s like subject terrain collisions there we go okay and save that as in the as always with a new module you want to save that in your project folder in the main part of your project folder so not in an assets folder and we want to call this Bump system ah dot p y there we go okay um so now i’ll go to my prepared code copy everything place that in our bump system there we go and now um what we want to do in maine at the top there is from bump system underscore system import everything i’m presuming And then um so i’m in my main module obviously so then in update um down here where it starts at block found here we shouldn’t need any of this so hopefully i could just delete that yep and then say um what’s it called bump bump wall And then i’ve got to pass in the subject and the terrain um and let’s just write a little note to ourselves which is um check um or really walk on solid uh terrain and check wall collisions save so that should be referring now to our just bring the bump system over here Should refer to this so everything in here should be incorporated okay let’s see if that runs i think i’ve saved everything wonderful um we’re at least walking on our uh oh we’re gonna sneeze uh pause it i can’t pause it because i have to come out here oh i’m okay now um Since i’m out of running this mob system we need to stop our panda running away from us so i have to reincorporate this although it’s got a wiggle bug oh let’s think um notation is that your temporation is that look at that yeah we do want to incorporate that What did i do in my prepare code mod system prepare code yeah i did exactly the same thing so it’s fine um right and replace this comment turn speed not affected because we sorted that out we’ll say uh bug uh wiggle walk when aligned with subject something like that so we got a Wiggle walk okay um yeah oh yep that’s fine so we’ve taken out the uh x and z rotation we’re leaving in the your rotation that should now be our panda gray should be now facing us as we expect and what we’re doing is um testing the terrain in the wall collisions Which as i said in the previous video has a preview to this um new ability here we go transforms are kind of the sense of reality so let’s build um a wall so now it’s three blocks high so for my settings i can the step height is two lovely And it stops us in our tracks and we can’t sidestep through we can’t reverse through lovely so that works beautifully and if you put a block on the floor we can step up and step over lovely but we can’t oh can we get it back down oh yeah because they’re only too high Relatively oh remember a panda has got a higher step height so it’s straight over oh also we should yeah we haven’t actually turned on the um collisions have we for our uh our panda so he can walk through things so maybe as an exercise you could um continue your own development of the Mob system by copying the code into his movement for these collisions yeah so just to reiterate all we’re doing is for the vertical collisions we’re just saying from one unit up um to the the step height of our player is there a a a block in the immediate square in front Of them or that one or that one for the heights from about three up if there is then we turn off w if there isn’t we can keep using w and now i can’t get through um lovely okay so um i haven’t really tested this in terms of mining so Because there’s a lot of colliding to me to be experienced down there so whoa that’s how fast the blister mining is whoa oh is that the panda i’m inside the panda now so i can’t like move at all oh it moved a little bit if i make myself a little tunnel here Hopefully i’ll be able to walk forward yep and now i’m kind of like trapped underneath that bit yeah i can walk forward and stuff but i’m not glitching through which is really nice okay i wonder how fast i can get to the surface if i can’t get to the surface um very quickly With my panda then i’ll abandon not the panda never abandon the panda um so what do i need oh what i need to do yeah sorry it’s just uh what happens here when i oh so i can build oh no i can’t build there i can kind of build through The terrain that’s bad i get myself into an impossible position what’s going on oh god so there’s yeah a little possibility that we have to sort out players can build themselves through walls by standing on something or through ceilings i can see in the console also our poor panda has um Fallen off the planet but like whoa i almost fell off about it then but it’s quite interesting getting into these problems um i can see i love seeing underneath the terrain so actually it’s working rather well really so i shouldn’t be able to move much and i’m not glitching through the walls and Things it’s just that build bug so yeah so that’s that’s pretty cool okay so i can’t get myself back up to the surface so i’ll abandon it right that’s 40 minutes uh that’s quite nice um next video it’ll be good to show you how to do saving and loading Um some people are asking about getting jumping working um so i might do that well i’ll aim to do that behind the scenes um is there anything else uh trees i guess and then we’re back into biome so so within 10 videos 10 11 videos let’s be Generous okay 12 videos by about 12 videos we’ve caught up with the 22 in the previous series um oh yeah and adding an axe and some animation would be good yeah and just having fun making more mobs appear i can’t wait to get loads of chickens running around things like that Making our own mobs um oh caves yeah and actually developing the terrain so getting like caves and different biomes and again engaging with our purlin noise again would be a good idea so next video certainly we want to save the terrain and load so i’ve got that working Um that’s in actually my prepared code i need to put it in its own module but it’s not much that’s how to load a world that’s the longest part there and then saving the world is there a very small few lines in case you wanted to preview that Okay or indeed copy that and use that and that will be in the github repository right thank you very much for watching see you next time goodbye Video Information
This video, titled ‘Python Minecraft with Ursina: terrain collisions, and turn speed for panda mob – part 10’, was uploaded by Red Hen dev on 2022-01-29 14:57:42. It has garnered 627 views and 23 likes. The duration of the video is 00:41:38 or 2498 seconds.
A simple tutorial for using the lerp() function to control the speed of rotation of our animated panda mob.
Also, I explain my code for how to deal with vertical terrain collisions, such as bumping into walls.
Playlist: https://youtube.com/playlist?list=PLmP1LNMzp97rIbwnGHhjjobHHqWekxy8Y
Code and assets from video: https://github.com/RedHenDev/ursina_tutorials/tree/main/python_meshCraft_tut_2021
Thanks for watching!