1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Discussion Efficiency of rectangle in rectangle

Discussion in 'GameMaker Studio 2 Community Tech Support' started by inkBot, Jun 18, 2017.

  1. inkBot

    inkBot Member

    Joined:
    Sep 15, 2016
    Posts:
    44
    I've been contemplating how to approach detecting connecting attacks in my game, and the usual method I see in tutorials on the matter is to use a second player object that acts as a hitbox. While skimming through the GML reference, I noticed rectangle_in_rectangle. From a cursory reading it seems perfect to use to replicate the hitbox/hurtbox mechanics from fighting games.

    I can see many upsides to use rectangle_in_rectangle but I've been wondering, would using it be more or less efficient than the previously mentioned additional object method?
     
  2. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    The "hitbox object" approach is primarily for simplifying things, not for optimization. Creating instances usually does the polar opposite of that.

    Whether using rectangle_in_rectangle in its place would be more efficient or not depends on how you're doing things currently. If you're using precise collisions, then you're certainly causing a performance hit. Here's a a tech blog article explaining why and also providing a nice way to handle rectangle-to-rectangle collisions.

    From what I can tell, rectangle_in_rectangle should technically be faster than built-in collision checking due to how simple the internal calculations for it are. Try using the profiler to see how well it works on a massive scale. But you shouldn't really go for it at the cost of overcomplicating things, which in turn will outweigh the benefits.
     
  3. kburkhart84

    kburkhart84 Firehammer Games

    Joined:
    Jun 26, 2016
    Posts:
    467
    I'm understanding that using code for directly checking(rectangle-in-rectangle for example) will be faster...but, will it be better for your use case? One advantage of creating a sort of second invisible instance for this purpose is so you can more easily visualize the actual collision boxes. Most of the time, precise collision checking is slow, but if it works better for your game, and is fast enough, who cares? If you are making a fighting game with few characters going at a time, it shouldn't bog the system down at all. And it would let you draw sprites that correspond to animations of your character. Say you are doing a punching attack, and you only want the fist to actually do damage, you draw a second equally sized sprite, and just alpha out everything that isn't the fist. You then have a second instance "overlaying" your player that is what actually tracks the collisions. Typically this would be invisible, but if you wanted to, you could implement a system where it draws it if you set a "debug" variable or something.

    One thing to consider, there is a such thing as premature optimization. In general, if you are using Gamemaker, you are already trading developement ease and speed for final product performance. The engine won't be able to catch up with a natively coded specialized engine, but of course it would take much longer to develop a game in such a system, which is where Gamemaker wins for many people. This applies not only to the game engine, but to other aspects. Is it easier to simply draw over/alpha out the fists for your fighters, or easier to make out coordinates frame by frame for rectangles in order to code it so it could be faster. Honestly, if the easy way to do it is fast enough, leave it alone. Only if things get bogged down, and something specific is the cause, do you then go and change it to a harder to work with but faster version.
     
  4. inkBot

    inkBot Member

    Joined:
    Sep 15, 2016
    Posts:
    44
    The main thing that got me interested in using rectangle_in_rectangle is the flexibility it gives. For example: for any given attack I can check if the hitbox rectangle overlaps or is inside the hurtbox rectangle. If instead I would want to do a grab, I can check specifically if the hitbox rectangle is completely inside the hurtbox rectangle and only then execute the grab.

    This is how I judged each method.

    Additional object method: This is how I would've done it in other software/engines I've worked with, so it's a familiar approach. However, for each character on screen, there will now be an equivalent-sized texture taking up space in VRAM (unless setting it to be invisible clears it from VRAM, which sounds reasonable to assume, but I'm honestly unclear on whether or not it does). That may be negligible in the end, but is probably worth keeping in mind.

    Rectangle in rectangle method: The most obvious benefit here is that there's no need for additional objects. On the other hand, every time an attack is made, the rectangles are changed on a frame by frame basis. This is the part of it that I thought may be slow or inefficient when there are many things happening at once. But if it's not noticeable for the player, then it doesn't really matter if it is slower.

    My biggest concern about optimization here was if there was any obvious drawbacks to rectangle_in_rectangle that I may be ignorant to, being still new to working with GML. From your responses so far, it seems there really isn't.

    Thanks for the responses. Obviously if anyone else has input to add, I'd be glad to read it.
     
    Last edited: Jun 19, 2017
  5. Mike

    Mike nobody important GMC Elder

    Joined:
    Apr 12, 2016
    Posts:
    2,393
    The built in collisions now use an R-Tree, so will not test things that aren't close to each other, but doing a bounding box test yourself won't give you this speed up. The built in collision tests using bounding boxes should be the fastest way to do collisions - although this may not always be the best for your game, depending on your needs.
     
    Guest likes this.
  6. Ribhu

    Ribhu Member

    Joined:
    Dec 14, 2018
    Posts:
    1
    upload_2018-12-14_20-17-49.png
    upload_2018-12-14_20-22-40.png
    this is my player with two movement tiles and the current code im using that works , it allows the dark green box (collision box of my player) to move within a the movement objects/tiles(dull green blocks). Was hoping someone could help me with the following questions :
    1) seem like i can achieve this by rectangle_in_rectangle [so as to limit the movement in such a way that only full collisions are allowed and not partial collision. i dont quite know how to go about it
    2) any other suggestions would be much appreciated
     

    Attached Files:

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice