Easy enemy AI using collider queries!

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ต.ค. 2024

ความคิดเห็น • 65

  • @powertomato
    @powertomato 9 หลายเดือนก่อน +125

    The amount of people who think this is unoptimized because it checkes every single frame is too damn hign. It goes back to the NES era and if the collision checks are just bounding-box overlap checks it's constant time O(1).
    To everyone stating it should be event based: how do you think the engine knows an event happens? Spoiler alert: by checking it every frame... Those events are syntax sugar.
    Y'all need to have a look under the hood of engines a bit more. In this case how physics engines work and the sheer amount of math heavy lifting they need to do. Like how a single collision resolution requires 10-20 of geometry-aware collision checks (which is not constant but O(n*m)) per collision per frame on top of accurately keeping track of velocities, forces/acceleration. And yes they do that every frame 😱

    • @simplepotato24
      @simplepotato24 9 หลายเดือนก่อน +4

      O(1) is asymptotic time complexity. That's doesn't mean it's not a poor use of resource. Collision detection overall is a heavy operation when performed every frame. Events are low level checks that usually are optimized by game engines. Yes, In a 3d game, there are many collision checks per frame. But that doesn't mean that processing resources are infinite. Every game should use resources efficiently.
      That, when paired with a previous video about collision checks while jumping, justifies these comments

    • @simplepotato24
      @simplepotato24 9 หลายเดือนก่อน +5

      Most of the AAA games are guilty of it. Because of ever increasing competitiveness, devs usually completely ignore optimizations. This results in ever increasing demand for beefier GPUs when older ones could've worked just fine

    • @powertomato
      @powertomato 9 หลายเดือนก่อน +2

      @@simplepotato24 BB checks are literally 1 (in best case) 4 (in worst case) integer comparisons, as opposed to powering up a 2D-Physics engine and using the built in collision events, as most people suggest here. Which under the hood, does exactly the same, but instead of a simple collision check it's a SAT or other much heavier algorithm, that works with every type of polygon.
      I get what you're saying but on a modern CPU you could literally have a million of those enemies and you would still run at 750fps @3GHz and that is assuming you're only using one CPU core. Premature optimization is a waste of dev time, which they can spend on fixing something that actually matters.

    • @simplepotato24
      @simplepotato24 9 หลายเดือนก่อน +3

      ​@@powertomatoit is already using a 2d physics engine. Not only that, it is using a generic collision algorithm to check for collisions(which is probably the worst case). My main point is that we can avoid all that and check for only the x coordinates to be within a bound
      Yes, it would mean that code becomes difficult to manage. But it also means that it becomes easier to scale down to less powerful devices like phones(where we have to worry about battery usage), or even raspberry PIs.
      I get it that even mobile processors are more than capable of rendering such a game at ridiculous frame rates. But consider that these generic collision checks are used frequently in this game.
      It's not about premature optimization. It becomes easier to add new levels and features in future without worrying a lot about frame rates and computer specifications

  • @magic00squirrel
    @magic00squirrel 9 หลายเดือนก่อน +45

    I disagree with a lot of these comments complaining about how this is unoptimized and will cause bad performance. A game engines built in collision check has to run every frame anyway, that’s just the nature of detecting collision.

    • @candyandcarmel
      @candyandcarmel 7 หลายเดือนก่อน

      yes they have to check collisions every frame, but with some other method that doesn’t use this, it would have one less object to detect collisions for

  • @kodicraft
    @kodicraft 9 หลายเดือนก่อน +16

    I'd bet money that less than 10% of all these commenters have ever even started an IDE in their entire lives. This is a perfectly fine approach to this solution that's very efficient and flexible.

  • @TheNew1234_y
    @TheNew1234_y 3 หลายเดือนก่อน +3

    Remember, toby fox who made undertales, makes 1000 lines of switch statements and hundreds of if statements, yet he still succeded.

  • @iambeavis762
    @iambeavis762 3 หลายเดือนก่อน +2

    This is why i think red koopas are smarter than regular koopas

  • @eboatwright_
    @eboatwright_ 3 ปีที่แล้ว +68

    The way I do this, is to have extra tiles that only collide with enemies and not the player. And I code the enemies to turn around when they hit a block so that automatically works. :) Also, first! XD

    • @butterflytr3077
      @butterflytr3077 ปีที่แล้ว +8

      Pretty easy and practical

    • @eboatwright_
      @eboatwright_ ปีที่แล้ว +3

      @@butterflytr3077 Thanks ^^

    • @paulblart7378
      @paulblart7378 ปีที่แล้ว +10

      @@eboatwright_ Interesting idea, although then you have to place a tile at every edge where an enemy can get to. Not that it's a bad idea, I just prefer a less rigid method where I can just drop an enemy anywhere and it works.

    • @eboatwright_
      @eboatwright_ ปีที่แล้ว +2

      @@paulblart7378 Yeah definitely, the game I implemented this in had really small levels so it wasn't too inconvenient haha, but yeah I make my enemies more robust in my newer projects ^^;

  • @jas3.14
    @jas3.14 4 หลายเดือนก่อน +3

    This comment section makes it awfully clear that the loudest people are often the dumbest. As if checking for a couple collision every frame is somehow going to make a 2D pixel platformer slow down on modern day hardware.
    Big ups for these shorts, I bet they inspire many people even if they don't end up commenting!

  • @watynecc3309
    @watynecc3309 3 ปีที่แล้ว +7

    I love your Monogame/LOVE2D Tutorial !!

    • @watynecc3309
      @watynecc3309 3 ปีที่แล้ว +1

      Btw I made a Circle spin with XNA 4.0 ebook I'm pretty happy to learn what's a sprite sheets

  • @monodragon
    @monodragon 9 หลายเดือนก่อน +173

    based on the 2 shorts ive watched, which both have checks that happen every frame, this is nearly the most unoptimized game ever

    • @benjitb2955
      @benjitb2955 9 หลายเดือนก่อน +6

      you can probably do if statement checks that keeps track of how long it takes for the creature to get from one edge to the other, then simplifies the query to make it work every few 0.1 seconds or whatever it asks. of course youd need to make somthing else for games that have changing surfaces that utilize nav meshes, but i think this is deff probable

    • @KittAnimations
      @KittAnimations 9 หลายเดือนก่อน +30

      The purpose of the short is to inform potential game developers who are into this sort of stuff, it doesn't have to be super optimized just enough to explain to the average viewer

    • @breadmilkYT
      @breadmilkYT 9 หลายเดือนก่อน +23

      ​@@benjitb2955You could also just have invisible objects at the edges that tells the enemy to turn around when it touches it

    • @ZokWorks
      @ZokWorks 9 หลายเดือนก่อน +32

      This isn't really expensive at all. Collision checks happen per frame no matter what, the expensive part of the check is the code that runs when a collision occurs.
      I could have an object in mid-air that has a collider on it. The collider would check to see if it collided with "something" per frame. If it doesn't then the collider check will just move on to the next collider out of the thousands of them it has to check per frame.

    • @kodicraft
      @kodicraft 9 หลายเดือนก่อน +3

      @@breadmilkYT That's literally worse than this approach in every single way though? Now you need to run collision checks AND make sure you place invisible objects everywhere! That's super excessive!

  • @emblink27
    @emblink27 3 หลายเดือนก่อน

    In the first example, I’d prefer to add 2 patrol points. I think this is more flexible because the entity just moves from point A to point B. Assuming that no gravity component is added and tiles under the entity never change, I can completely ignore tile collisions for this NPC and only check for player or weapon collisions.

  • @schmittywobberganggermanson
    @schmittywobberganggermanson ปีที่แล้ว +6

    I really wanted to learn this for my game because my AI always gets stuck on polygons and sometimes even in normal flat terrain but I couldn't find any tutorial online smh

    • @arnoldwu6747
      @arnoldwu6747 ปีที่แล้ว

      You mean ur collider is physical instead of a trigger? (For unity, colliders can either be trigger or not trigger), also you can maybe use a ray cast on two edges of the character to check for collision.

  • @KenLaKen
    @KenLaKen 9 หลายเดือนก่อน +8

    if;GonnaFall then;don't

  • @rizenos955
    @rizenos955 3 ปีที่แล้ว +1

    Nice explanation

  • @darkfrei2
    @darkfrei2 3 ปีที่แล้ว +1

    I found that left collision is harder to check and you are need to use a tiny gap by tiled map.

  • @jackbauer9901
    @jackbauer9901 8 หลายเดือนก่อน +2

    Why is it a trick? How else would you do that?

  • @scimitaronic
    @scimitaronic 3 หลายเดือนก่อน

    Green koopa:

  • @patopeje1472
    @patopeje1472 9 หลายเดือนก่อน +1

    I've recently found an old game project I made in 2017/2018. I'm just a hobbyist and that was an absolute disaster (after that game I tried to make an other unsuccesful game and never more touched the hobby until now). Anyways, the point is that to make this behavior in the enemys, I just made TWO INVISIBLE OBJECTS, one for left side and one for right side, and put them anywhere there was enemys. When they hit the invisible objects, turn around and moves in the opposite direction. Totally unpractical and amateur shit.

  • @oriinafloresta
    @oriinafloresta 9 หลายเดือนก่อน +1

    I use godot which has is_grounded build into the CharacterBody2D by its basically the same under the hood

  • @jasomega2446
    @jasomega2446 ปีที่แล้ว +2

    Thanks.

  • @0bamo0
    @0bamo0 2 ปีที่แล้ว +2

    can i know , how to actually code the conditions for the query , im dumm i can't figure it out , i try with if statement but the ennemis just start walking back and forth at the end of the platform xD

  • @ignaciolopez8597
    @ignaciolopez8597 9 วันที่ผ่านมา

    What is more heavy to the engine, this or the use of raycasts?

  • @siriokds
    @siriokds ปีที่แล้ว +1

    How can I solve if I have a limited resource machine and I need to use a rectangular collision area in the main character? Any hints?

    • @paulblart7378
      @paulblart7378 ปีที่แล้ว

      Not sure what a "limited resource machine" is. Do you mean finite state machine? And you'll have to elaborate because I'm not sure what you're asking.

  • @benjitb2955
    @benjitb2955 9 หลายเดือนก่อน +1

    please make more of these, im new to this stuff, and these are helping me.

  • @cheemsboi9845
    @cheemsboi9845 ปีที่แล้ว +2

    Explained simply for everyone and new friendly thx

  • @alishakhan5469
    @alishakhan5469 3 หลายเดือนก่อน

    Heyy I just wanted to know which language is this . I'm new to game dev

  • @otakujiji
    @otakujiji 3 หลายเดือนก่อน

    Question, is this more efficient that raycasts?

  • @lancepate
    @lancepate 9 หลายเดือนก่อน

    It works, and optimization seems like a bit of a non-issue. Don't see the problem here lmao

  • @miguelsouto2091
    @miguelsouto2091 7 หลายเดือนก่อน

    Did you use Raycasting or just Colliders?

    • @lady_deaths_head
      @lady_deaths_head 4 หลายเดือนก่อน

      Any of them works but I think raycast is the standard

  • @the_greened_out
    @the_greened_out 9 หลายเดือนก่อน +27

    UPD no this doesnt lag i was wrong
    develover: does this
    also delveloper: why my game is so big and lags on my nasa supercomputer

    • @fodk7021
      @fodk7021 9 หลายเดือนก่อน +6

      Instead of complaining please share a better solution

    • @lady_deaths_head
      @lady_deaths_head 4 หลายเดือนก่อน

      ???

  • @X_TV.
    @X_TV. ปีที่แล้ว +1

    Game name?

  • @Wanfanel
    @Wanfanel 9 หลายเดือนก่อน

    why no raycasts?

  • @nobody1276
    @nobody1276 4 หลายเดือนก่อน

    Raycast?

  • @har3nz
    @har3nz 11 หลายเดือนก่อน

    OMG THX A LOT I STARTED UNITY A FEW WEEKS AGO I ALREADY KNEW LIKE A COUPLE OF THE THINGS TO DO THIS BORROWED SOME CODE FROM MY OTHER CODES IN THE SAME PROJECT AND DID SOME THINGS FOR ENEMY TO MOVE AND CHECK IF ITS GROUNDED AND IT WORKS I CANT BELIEVE IT THX A LOOOOT

  • @BratMaksa
    @BratMaksa 11 หลายเดือนก่อน

    Im sorry but what is this engine?

  • @thedoubtfulhost
    @thedoubtfulhost 9 หลายเดือนก่อน

    Way easier to just compare the location of its side and the location of the side of the platform. Just a simple if statement instead of a whole function that iterates over multiple objects

  • @cannotfigureoutaname
    @cannotfigureoutaname 5 หลายเดือนก่อน

    That's too smart for me, i would do raycasts

  • @MemeCheryl
    @MemeCheryl 9 หลายเดือนก่อน +1

    How to rot your computer in 2 easy steps

    • @Vegan_Kebab_In_My_Hand
      @Vegan_Kebab_In_My_Hand 3 หลายเดือนก่อน

      What? Why? You do realize every piece that code needs to be constantly cycled through line by line anyway, right? Sure there are functions you don't want to call every frame, but a simple 2d collision check is perfectly chill.

  • @gluehole4036
    @gluehole4036 ปีที่แล้ว

    this is a certified if !place_free(x,y) moment