Godot Engine - Know Your Nodes: RayCast2D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I'm looking through the documentation but not seeing it: are there advanced methods to this?

Things like:

  • parabolic rays
  • bouncing rays (number of bounces and/or bounce/end based on collision layer)
  • multi-hit collision
  • custom rays (path2D, ray thickness/sine wave)
  • ray groupings (like 12 rays in slightly different directions, allowing finding collision points from 1 call)

I'm guessing these things aren't possible without using plugins...


Also can rays be used for more advanced character physics? In other words, precalculated landings/momentum shifts? Particularly for higher speeds that can cause issues with normal physics. Maybe even just reporting collisions early to only prevent those issues...

👍︎︎ 1 👤︎︎ u/insomniac_lemon 📅︎︎ Jun 10 2017 🗫︎ replies
Captions
welcome back to know your node this series of Godot game engine videos where we take a single node type from the engine and show how it's used in this video we will be talking about the raycast 2d node so what is array cast if you've done much reading about game development you've probably come across the term because Ray's and ray casting are very useful for solving a lot of different problems in games at its most basic array just like you learned in geometry is a line segment projecting out from a start point projecting this array is called casting and its purpose is to tell you if and where it hits something as you can see the ray cast 2d node in Godot has a bunch of different methods having to do with collisions and we're going to look at how to use them as I said there's lots of uses but for this video I'm going to demonstrate two of them ground detection and high-speed projectiles for this demo I've made a simple top-down shooter style scene with a player that can walk around and walls that are static body 2ds so that we have collisions and what we want to do is implement shooting now one way that you can do this is that you can have sprites that are instance at the tip of the barrel of the gun and travel in a straight line until they hit something but where this breaks down is if you want to have your projectiles be very fast-moving if you create a node and animate it moving extremely fast then you're going to have your physics breakdown if you have an object that is relatively narrow and the speed of your projectile is really fast then it's possible that between frames it will move from one side to the next and you won't detect that collision and you have to start doing a lot more expensive calculations to make sure that you don't you don't have that tunneling effect which is what it's called so instead what we want to do is say you're firing a bullet from a gun that bullet travels really really fast and all we care about is what does that bullet hit so if I shoot the gun right now it should hit the wall over here and that's where I should see the hit and I don't really care about animating it travelling along that distance because it moves too fast for you to see right it's just going to hit what it hits and that's what we're going to use a ray cast for we're going to cast array from the gun in a straight line and detect where it hits so here's our players scene we have a kinematic body to D with a sprite and a collision shape attached to it and a script that's handling all of the character controls rotation and movement and wall collision and so we want to now do the shooting and so we want to add raycast 2d to this so we're going to go to play we're going to add a ray cast to D and there we go and see it's drawn in the editor as a blue arrow and now we just want to change a couple of the parameters first of all any time you add a new ray cast to D it is disabled by default so if you want to use it you need to make sure to turn it on and of course you can do that in code as well and first of all we want this to come out of the barrel of the gun not out of the player's forehead so we're going to adjust the position and the barrel of the gun is about 30 in X and 10 and Y all right so we're going to put that at the tip of the gun barrel and forecast to which is this parameter right here this is set to zero comma 50 which is why it's pointing down so we want this ray to come out of the gun which is pointing in the X direction and we're going to put a large number here we're going to put a thousand and we're set the Y to 0 and so this ray is going to come out of the gun and go a long distance because what we want to know is does this hit anything as far as the bullet travels and we can see this in action if we turn on the visible collision shapes and run our main scene you will see that ray coming out of the player right and showing what direction our imaginary bullet is going to travel when we shoot it and so now we just want to query that ray cast and see where it hits all right and we're going to get this point right here okay so let's go to the script and let's rename this to the shoot ray and I'm going to copy this as well because what I want to do is in the player script I'm going to load a variable for it shoot ray is equal to get node shoot ray so now we have that to refer to and we're going to set process input to true so that we can have whenever we press the spacebar we're going to shoot a bullet so on our input method we're going to just say if event dot is action pressed UI select which is the spacebar by default then we're going to shoot whatever shoot means well let's define that so the shoot function could do a lot of different things but what we really care about is we're going to test if the shoot ray is colliding and if the shoot ray is colliding what should we do well we need some sort of visual feedback that we've hit something and so on the map on the 2d map again on the wall somewhere we want to see a puff of we're going to see a puff of smoke so we can see that the bullet hit there so what we're going to do is we're going to emit a signal I'm going to define that signal up here I'm just going to call it I'm just going to call it hit and then we're going to emit signal hit and and then we're going to pass along the location of the hit which is shoot ray get collision point and that's going to be the location of where it hit you can also get collision normal you can get Collider or Collider shape if you want to know what thing you hit or what part of a thing you hit but we care about the point where we hit because that's where we want to spawn the little puff of smoke so that's our play so whenever we hit space we're going to emit that signal if the Ray collides with something now to do that I've just made a quick little particle 2d scene that just does a little puff of smoke like this and that's what we want to instant at the hit location so in our main scene in the script we have our player instance and we're just going to connect that signal so player dot connect the signal is hit and the method we want to call is show hit we pass in the hit location and we just instance one of those puffs of smoke at that location and that's going to look like this let's turn off the collision shapes for a moment so you can see now whenever I press the spacebar I get a hit at whatever place on the wall that projectile hit so fit and so just to add a little bit more visual appeal I'm not going to go into this in too much detail but I just added a quick little trail so that when we emit the signal we emit the barrel location which is the players location plus the offset which was 30 comma 10 as well as the collision point and in the main script when we get those two locations I am instancing this little this little trail which is just an animated sprite that it stayed and so that just makes us look a little bit nicer like we have a little path where you can see where the bullet went and just this is a quick and dirty one obviously you could make it look a lot better but for the purposes of this demo I just wanted you to be able to see the path of the bullet as well as where it hits I hope that was helpful now let's look at one other application of the raycast 2d and this one is even simpler so we've got a simple platformer here where we have a character and a platform and [Music] using the standard kinematic body 2d we get the collision so the player Falls stands on the platform and run back and forth but we want to do jumping and right now what I have is if the up arrow is pressed we set the velocity to a negative value which makes them go up but the problem with that is I can keep pressing the up arrow whenever I want and I get a negative velocity every single time and I don't want that I only want the player to be allowed to jump when they're standing on the ground and that is another perfect use for a raycast 2d in fact we can do a very very simple one so first we open our players scene and we're going to add the raycast 2d now we want this we want this raycast to needed to detect the ground which means we want it to be sticking down below because right now what are we going to get we're going to get a collision constantly because this raycast 2d is colliding with the player and so we want to do a couple things let's name this ground ray let's enable it remember always remember to do that we want to cast our sorry first we want to move it let's move it downwards all right I wanted to come out from the bottom of the player and let's also make it a little bit shorter because it doesn't need doesn't really matter but it doesn't need to stick down that far below we just need it to come below the player's feet to where we want to detect the ground so let's just go into the script and we'll add a reference here to it to the ground ray so that we can refer to it in our code and here in the jumping now we're going to say if you press the up arrow key and ground ray is colliding so if you're pressing up arrow key and the ground ray is colliding will let you set your velocity and that's all we have to do so if we run the scene you'll see that I press the up arrow I jump if I tap it a bunch of times nothing happens when I'm already in the air and that's as easy as it gets if you have more complicated shapes you sometimes will have problems with your collision detection happening you know the ray actually collides with the player and you can solve that in a couple of ways the raycast 2d has an ad exception method which lets you name an object for it to ignore and so if you put the if you put the player object in there then the array would never detect a collision with that object you can also as with any other collision object in Godot use the layer use the collision layers and the collision masks to set the array on a different layer and have it ignore the player that way and this is also a good method for doing wall jumps you give the player some rays that are sticking out to the left and right and those can tell you whether you're against a wall or not to allow the player to wall jump that's another really common use of raycast so that'll do it for this video I hope that you found the examples useful so the source code for this project will be linked in the description below feel free to download it and take a look and play with it yourself and use rake apps whenever you can now see you in the next video you
Info
Channel: KidsCanCode
Views: 34,267
Rating: undefined out of 5
Keywords: programming, coding, tutorial, lesson, gamedev, game development, godot, godot engine
Id: esZdJegvANc
Channel Id: undefined
Length: 14min 11sec (851 seconds)
Published: Fri Jun 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.