No video

Godot Recipe: AI - Context-based Steering

  Рет қаралды 25,563

KidsCanCode

KidsCanCode

Күн бұрын

We get a lot of questions about AI. Here's a simplistic algorithm that produces great results.
Text version & link to download the sample project:
godotrecipes.co...
Support me on Patreon: / kidscancode

Пікірлер: 107
@DriftWare
@DriftWare 6 ай бұрын
How is this done with a 3d object? There's all kinds of vector functions that don't translate from 2d to 3d.
@syllight9053
@syllight9053 3 жыл бұрын
This is one of the most valuable programming channels out there! These ones are rare and is like a jewel. I am 14 and started gamedev on 13 and I love this channel for supporting kids like us to get onto programming! Keep up the great content!
@theeox
@theeox 5 ай бұрын
I would have really liked to see an implementation for 3D. I'm not really grasping it, doesn't help that godot 4 has different nodes and functions.
@jmbiv_dev
@jmbiv_dev 3 жыл бұрын
Glad to see videos coming again! I've been seeing context-based algorithms (as opposed to/in modification of traditional steering behaviors) gaining a lot of attention recently, such as Game Endeavor's AI video. I've been working on a couple implementations of my own and have enjoyed seeing how others have gone about doing it. Great video that explains a nice, scalable way to implement this.
@JahznCentral
@JahznCentral 11 ай бұрын
hi everyone. if you want the code to add interest to the opposite ray of the ray that sees danger, write the choose direction function like this func choose_direction(): > for i in num_rays: > > if danger[i] > 0.0 > > > if i > num_rays/2: > > > > interest[i - (num_rays/2) - 1] = added_interest > > > else: > > > > interest[i + (num_rays/2) - 1] = added_interest > > > interest[i] = 0.0 added_interest will be any arbitrary high number. you will have to fiddle with this. i found 20.0 works for my project.
@ollereberg7828
@ollereberg7828 2 жыл бұрын
Could you do a version of this for 3D pathfinding, like for space ship where you you actually need to use x, y, and z? Been trying to figure out proper 3D pathfinding in not just this but in Unity too and I always get stuck
@jordymendoza8192
@jordymendoza8192 Жыл бұрын
Can I get the code for the debugdraw node u had on the agent
@neto9348
@neto9348 7 ай бұрын
Lol, that’s exactly what I need for a game that I’m making on Godot and it just randomly appeared on my home page. I had a rudimentary code using the same concept, this will help me a lot, thanks.
@normaldefault2206
@normaldefault2206 3 жыл бұрын
So, I'm working on an RTS-style thing, and I figured I'd add this to my pathfinding to stop units from bumping into each other and generally getting in each others way. When it works, it's awesome, I love making little crowds of units cross paths with each other and shuffling out of each other's way; however, if two happen to collide head on, and ONLY the forward facing ray is blocked, the average vector is still straight ahead and they don't get anywhere. I can add if cases to get around this, but it doesn't feel quite so elegant...
@andrewvickerman1940
@andrewvickerman1940 2 жыл бұрын
Instead of the 0deg ray, make it -0.1deg or 0.1deg randomly. This is almost the same except that agents will favor one side slightly more than their other.
@TheDuckCow
@TheDuckCow 3 жыл бұрын
Really appreciated this - both the video and the webpage! Though I had to take a few extra steps to make it applicable for my own vehicle body-based car, was able to get this working nicely.
@vatrenikrug
@vatrenikrug 2 жыл бұрын
any chance that you can share some project? i cant get it working :( and i was copying from website :(
@TheDuckCow
@TheDuckCow 2 жыл бұрын
@@vatrenikrug sadly not open sourced or avail right now, although if you are curious how far I was able to get with it, you can check see it here: kzbin.info/www/bejne/nme6lHWAfsqKiNE It's slightly more than a copy paste though, I recommend just going one step at a time, and really make use of those debug drawing vectors.
@yatchan
@yatchan 3 жыл бұрын
Thanks for the great video! I've tried it out and in a scenario like this, with a simple obstacle course, you don't even have to set up a track. The boids are managing very well with just going forward :) With a little tweaking, it can be applied for example to a monster wandering randomly around a cave.
@couscous4349
@couscous4349 3 жыл бұрын
Great video! Any chance you could upload the project so I could take a look at the code you use to draw the arrows? Thanks!
@JamesKelly89
@JamesKelly89 11 ай бұрын
This seems like it would work well using a navmesh where you want to follow the path given by the navmesh agent but also want to avoid obstacles and other agents. So the get path direction could just just a vector to the next waypoint given by the navmesh.
@mrrandle5776
@mrrandle5776 4 ай бұрын
Yo dude, im trying this right now and cant for the love of me get it to work, any clue how you would write the get_path_direction to work with the navmesh? I have the nav agent drawing the path to the target. but nothing i write in the get path direction seems to make the interest follow it
@loot6
@loot6 Жыл бұрын
Seems good in theory but in practise has flaws. For example if the target is east and east is blocked by a small NPC in front of the target, just large enough to block only one ray, the best directions will be diagonal NE and SE to take you around the object. But if you take the average of those it's obviously east...which moves you into the NPC...
@cheapmeow8478
@cheapmeow8478 Жыл бұрын
Constraint AI that if you can't "see" target, then you can't be interested by it, may help avoid this problem.
@raremc1620
@raremc1620 3 жыл бұрын
Hey, thanks I was literally just planning of doing something similar before I noticed your new video!
@Knutmeister
@Knutmeister 3 жыл бұрын
Thanks a lot! I like the direction, all those car related video are going.
@vatrenikrug
@vatrenikrug 2 жыл бұрын
your recepies are amazing,i do hope to make my first games and to be able to give some back for your amazing work. thanks for being a teacher on godot :)
@tPlayerioT
@tPlayerioT 10 ай бұрын
it seems godot has now a built in simple steering behavior. Its called "Navigation obstacle" and you put them inside the thing you want to avoid. then enable "Avoidance" in your navmesh character.
@lumenwrites
@lumenwrites 3 жыл бұрын
What an amazing tutorial, thank you so much!
@TackerTacker
@TackerTacker 3 жыл бұрын
Thanks for the great video. I have one question. How would you handle tracks made out of tiles? Is there a way to combine multiple Path2D nodes into one continues path, or would it be better to keep them separated and somehow set the position to the different tiles one after the other?
@Kidscancode
@Kidscancode 3 жыл бұрын
As recommended in the video, it wouldn't affect the agent itself at all. It would be the responsibility of the Tilemap node to provide a `get_path_direction()`. It's hard to be specific, because it depends on how your Tilemap actually works, but maybe you have a direction vector mapped to each tile index.
@TackerTacker
@TackerTacker 3 жыл бұрын
@@Kidscancode I meant something like track segments not single directions, tiles with right and left turns, S curves, etc. maybe a crossroad. If I would prepare little track piece tiles in scenes, and every track piece scene has it's own Path2D with some kind of curve, I'm not sure how I would go about connecting these pieces together. I guess I would do path follow and at the end of the a path if there is still movement left I transfer it over to the next segment and start from there with that offset?! What would I do if there is someone fast enough to overshot an entire tile? Hmm, that would probably not happen unless the tiles are pretty small. I'll need to play around with it when I got some time. Again thanks for the tutorial, very inspiring.
@bamtoday
@bamtoday 3 жыл бұрын
Excellent tutorial!
@szokedencsimarci5668
@szokedencsimarci5668 3 жыл бұрын
thank you for this video!
@gameanddata3194
@gameanddata3194 3 жыл бұрын
I'm doing my game thanks to this tutorial, awesome.
@JorgeRosa
@JorgeRosa 3 жыл бұрын
GREAT tutorial! Thankyou!
@n01487477
@n01487477 3 жыл бұрын
Hi - thanks for all of your really informative tutorials. I note that this one doesn't seem to have a download link. Am I mistaken?
@pascalcasier959
@pascalcasier959 3 жыл бұрын
Excellent ! thank you so much.
@NicholasOrlowski
@NicholasOrlowski 3 жыл бұрын
Another amazing video, thanks!
@brianbatton9950
@brianbatton9950 3 жыл бұрын
Hi! I know I'm kind of late but I would just like to ask where I can download the source code for this so I can test it out. I went to your website but did not see any download links. If you could help me out I would be very grateful!
@JohnnyCashOriginal
@JohnnyCashOriginal Жыл бұрын
the position variable is not defined. var path_direction = owner.get_path_direction(position) ... what is this supposed to be? global position of the object?
@boerbol9422
@boerbol9422 2 жыл бұрын
Awesome video. Bless you!
@rosva4331
@rosva4331 Жыл бұрын
You sir, just saved my ass. Thank you.
@juanloutech2864
@juanloutech2864 3 жыл бұрын
Brilliant as usual. Thanks man! Very usefull stuff
@Marduk401
@Marduk401 Жыл бұрын
10:22 but what happens if you place an obstacle that only blocks the middle raycast without touching the others?
@staticsteam636
@staticsteam636 Жыл бұрын
how set this up in 3d???
@DA-cl4ww
@DA-cl4ww 3 жыл бұрын
They seem to get stuck when running into more complicated collision shapes. Is there a better way for them to perhaps consider a direction opposite of all those blocked?
@gabrielpsych6
@gabrielpsych6 Жыл бұрын
I really can't understand what are these "rays" since there are no rays being cast from the tree nor code. Where are they coming from? I tried following the code but nothing seems to happen.
@the_cheese_cultist
@the_cheese_cultist Жыл бұрын
you cast the ray through the intersect_ray function.
@michaelthelen5297
@michaelthelen5297 3 жыл бұрын
Wow very cool! How did you deal with the extra axis in 3d? (like avoid the rays hitting the road on an up slope, or missing a wall when cresting a hill) I imagine this could be handled with collision layers and raising collision boxes... just curious
@toms2oo8
@toms2oo8 3 жыл бұрын
Not sure but I imagine the same concept works in 3d space, eg. you just project rays from a sphere rather than a circle around the object. And then your follow path can also exist in 3d.
@Kidscancode
@Kidscancode 3 жыл бұрын
Depending on your setup, you don't even need that. A 3D car, for example, would still just need a ring of rays horizontally.
@thelumberjack8838
@thelumberjack8838 2 жыл бұрын
Please make a recipe for how to do this in 3D because the difference between 2d and 3D is night and day
@KibanyaG
@KibanyaG 2 жыл бұрын
Here's a basic implementation in 3D
@lutiyoffnik8661
@lutiyoffnik8661 3 ай бұрын
needed in version 4 godot
@froggyCodingClub
@froggyCodingClub 3 жыл бұрын
Thanks, clear explenation
@jlebrech
@jlebrech Жыл бұрын
can the steering angle change the wheel angle and in turn the wheels affect physics of the car?
@bruceparker6142
@bruceparker6142 Жыл бұрын
Has anyone got this to work on a 3d track in godot 4? I'm getting errors in the .rotated(rotation) parts.
@ericcollinseh8916
@ericcollinseh8916 3 жыл бұрын
Hey, how do you make the characters animation track the mouse ui?
@piousthepious
@piousthepious 2 жыл бұрын
Great tutorial! I do have one question. How do you handle avoidance with the danger? It seems to be the danger simply gets negated but how would I go about making it so the complete opposite direction is desired? My first thought is to add interest to the opposite ray but I have no idea how I would even get the opposite ray. Is there something I'm missing?
@andrewvickerman1940
@andrewvickerman1940 2 жыл бұрын
Just search how to get the opposite of a ray. There are a bunch of videos about it.
@nicolassoriano2621
@nicolassoriano2621 8 ай бұрын
it doesnt work, it says "Parser Error: The identifier "position" isn't declared in the current scope."
@thelumberjack8838
@thelumberjack8838 3 жыл бұрын
Thank you so much
@viniguerrero
@viniguerrero 3 жыл бұрын
What is the equivalent in 3D for this? var d = ray_directions[i].rotated(rotation).dot(transform.x)
@Kidscancode
@Kidscancode 3 жыл бұрын
Dot product is the same in 3D. To convert a vector to a local rotation, you could use `transform.basis.xform(ray_directions[i]`
@viniguerrero
@viniguerrero 3 жыл бұрын
@@Kidscancode thank you! But then how do you rotate the vectors for intersection here if they should be vector3? ray_directions[i] = Vector2.RIGHT.rotated(i * 2 * PI / num_rays)
@bruceparker6142
@bruceparker6142 Жыл бұрын
Will this work with the vehiclebody3d node?
@Kidscancode
@Kidscancode Жыл бұрын
This is about choosing what direction to move. It is completely independent of what kind of movement you then do.
@Edel99
@Edel99 3 жыл бұрын
OMG... this is hard stuff!! I find it hard to visualize all this vector stuff... :(
@Kidscancode
@Kidscancode 3 жыл бұрын
This is why I typically recommend beginners start working in 2D first. It's easier to get a handle on vectors, transforms, and the like and then move to 3D where it get significantly more complex.
@kuroodo_
@kuroodo_ 11 ай бұрын
Do you still have the code for this? Would love to check it out
@anindyaroythetechmaster
@anindyaroythetechmaster 3 жыл бұрын
Can you please make a video on in app purchases in godot. Your mobile ads video is one of kind still on KZbin. Also, there are no good tutorials for in app purchases. Can you make it for us?
@andrewvickerman1940
@andrewvickerman1940 2 жыл бұрын
This is very cool. How is it with performance? I'd like to see the agent code. What you posted doesn't show the rays visually.
@vatrenikrug
@vatrenikrug 2 жыл бұрын
no there is no visualy... its just a animation for us i gues..? but dint find project to download and i cant geth it working...
@1littlemaxy
@1littlemaxy 3 жыл бұрын
Great! And how can I make AI to choose exactly one of 8 directions, not a one between them? My game is isomectic 2d with 8 directions
@Kidscancode
@Kidscancode 3 жыл бұрын
Maybe just choose the longest interest direction, or round the result to the nearest one.
@itsME-dc4vm
@itsME-dc4vm 3 жыл бұрын
Nice ;D
@CarstenNikiel
@CarstenNikiel 3 жыл бұрын
Wouldn't it be better to not cast rays backwards at all? None of the rays that are 90 degree or higher to the forward direction will be able to add to the choosen direction. So you might need less rays for those mainly foward driving agents.
@Kidscancode
@Kidscancode 3 жыл бұрын
In this specific example, probably - but this is general algorithm that applies to more than just a forward-moving situation. You could use your backwards-facing rays to decide to hit the gas when somebody's on your tail. You might have multiple "targets" and so your interest array could point in many directions - if one is blocked you'd choose the other.
@CarstenNikiel
@CarstenNikiel 3 жыл бұрын
@@Kidscancode Yes definitely. But in your example with the dot product you eliminate everything that is backward facing. Just thought about the optimization possibilities. But of course you are correct that in other circumstances you might have need for the rays pointing backwards. Thanks for the awesome video, as always. ;-)
@xegrand7548
@xegrand7548 3 жыл бұрын
Can you teach how to make a speedometer in 2d ?
@ShootYourBricks
@ShootYourBricks 3 жыл бұрын
Just have the arrow set its rotation to -90 + speed or something like that
@ShootYourBricks
@ShootYourBricks 3 жыл бұрын
The speed would be the linear velocity added together
@temporal_paradox
@temporal_paradox 2 жыл бұрын
This is really useful, but you don't actually include the project files like you say, and you don't explain how we can actually see the arrows visualized. There's also the 'debugging' with the mysterious variable and key press that makes it all visible.
@trakegaming79
@trakegaming79 2 жыл бұрын
Can yo help me to make the car chase the player by some of your codes I really need that 3d
@Red_Eagle
@Red_Eagle 3 жыл бұрын
we want gears and drift!
@re-dxealhgamemaker3070
@re-dxealhgamemaker3070 Жыл бұрын
LIKE.
@elzemgames
@elzemgames 3 жыл бұрын
Nicee
@bruceparker6142
@bruceparker6142 Жыл бұрын
I wonder how much of this has changed since godot 4.
@Kidscancode
@Kidscancode Жыл бұрын
The concept of context based steering is the same regardless of game engine. Some minor implementation details will change from Godot 3 to 4, but the general idea works the same way.
@oreomoreo2110
@oreomoreo2110 3 жыл бұрын
sounds expensive
@vaibhavpandey6640
@vaibhavpandey6640 3 жыл бұрын
how to use raycast in pygame as we use in godot?
@Kidscancode
@Kidscancode 3 жыл бұрын
Pygame doesn't have rays. You'd have to manually build such functionality yourself.
@vaibhavpandey6640
@vaibhavpandey6640 3 жыл бұрын
@@Kidscancode then what can i use for checking collisions near by to turn my npc
@Kidscancode
@Kidscancode 3 жыл бұрын
Same answer. You have to do something like use a collision shape (ie an invisible sprite) and move it around, checking if it collides with anything.
@hasaranger9845
@hasaranger9845 3 жыл бұрын
Hey man I have a big request ,i want you to make a sidescroller pygame game tutorial series , you are the only guy i can understand and your code is simple. So PLEASEEE MAKE A PLATFORMER TUTORIAL SERIES I BEG U
@nigeltsuro8631
@nigeltsuro8631 3 жыл бұрын
The funny thing is I'm a kid🤣🤣
@saulnores3477
@saulnores3477 3 жыл бұрын
This is too complicated for me. I've seen easier methods.
@ShootYourBricks
@ShootYourBricks 3 жыл бұрын
I made a 3d car game where I had an area in front of the car and if it was touching something, than it would run a function. When the function ran, it would turn right if a raycast on the right side of the vehicle wasn't touching. If the raycast was touching something, than it would turn left if a second raycast wasn't touching. If both raycasts were touching, it would brake. (That was probably super confusing lol.)
@saulnores3477
@saulnores3477 3 жыл бұрын
@@ShootYourBricks Hi. Your method is simpler than the one in this video. I like it.
@ShootYourBricks
@ShootYourBricks 3 жыл бұрын
@@saulnores3477 Thanks! I'm glad you liked it.
@Kidscancode
@Kidscancode 3 жыл бұрын
Use what works for your specific application. This is a generic algorithm that scales well and can be applied to a wide variety of situations, not just cars.
@saulnores3477
@saulnores3477 3 жыл бұрын
@@Kidscancode Your channel is great but this method is above my gdscript knolewdge and I don't understand some things in it. May be in the future I will.
@yametekudasai2563
@yametekudasai2563 3 жыл бұрын
please make a full mobile fps game in godot please i beg you😢
@Kidscancode
@Kidscancode 3 жыл бұрын
My channel is about learning how to make games. If you learn *how* to code and use the tools, you can make any game you want. If I just make a full game and you copy it, will you have learned anything?
@yametekudasai2563
@yametekudasai2563 3 жыл бұрын
@@Kidscancode but i dont know how to code😢
@Kidscancode
@Kidscancode 3 жыл бұрын
@@yametekudasai2563 My channel and many others can help you learn.
@yametekudasai2563
@yametekudasai2563 3 жыл бұрын
@@Kidscancode but how😥😥
@nurwaneduardo7831
@nurwaneduardo7831 2 жыл бұрын
How to make a 3D ai car. Because you toturial in 2D.
Godot Recipe: Simplified Airplane Controller
14:29
KidsCanCode
Рет қаралды 21 М.
Godot Recipe: Drag-select Multiple Units
11:46
KidsCanCode
Рет қаралды 16 М.
АЗАРТНИК 4 |СЕЗОН 1 Серия
40:47
Inter Production
Рет қаралды 1,1 МЛН
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 43 МЛН
At the end of the video, deadpool did this #harleyquinn #deadpool3 #wolverin #shorts
00:15
Anastasyia Prichinina. Actress. Cosplayer.
Рет қаралды 15 МЛН
Advance Enemy AI in Godot
8:17
Jackie Codes
Рет қаралды 26 М.
Godot Recipe: 3D KinematicBody: Align with terrain
15:48
KidsCanCode
Рет қаралды 15 М.
Godot Steering AI: Free and Open Source Framework Out Now!
2:04
Godot Recipe: Multitarget Camera2D
9:16
KidsCanCode
Рет қаралды 10 М.
Godot Recipe: 3D KinematicBody Movement
11:56
KidsCanCode
Рет қаралды 25 М.
The Trick I Used to Make Combat Fun! | Devlog
8:12
Game Endeavor
Рет қаралды 1,6 МЛН
Godot Car Tutorial
12:30
SRCoder
Рет қаралды 81 М.
Reflecting Raycasts in Godot 4
8:59
Alfie Hanks
Рет қаралды 59
Path Based Mesh Generation in Godot 4
7:27
Crigz Vs Game Dev
Рет қаралды 28 М.
АЗАРТНИК 4 |СЕЗОН 1 Серия
40:47
Inter Production
Рет қаралды 1,1 МЛН