Multiple collisions not adding to tally

MentirasMentiras Posts: 45Member

Ok so the situation is this, I have multiple ray casts looking for bombs. When they find one they add it to a tally, the problem is that if there is a bomb to the right and a bomb diagonally the tally remains 1, when it should be 2. If there are multiple bombs to the right, the tally is correct, if there are no bombs to the right and multiple diagonally the tally is correct. It is only when there are bombs to the right and bombs diagonally that the tally is wrong.

        # BUG - diagonal collisions dont get added to hoizontal collisions. i.e if a block is on
        # a diagonal and can see a horizontal bomb and a diagonal bomb it should display 2
        if (rr.is_colliding() or ru.is_colliding() or rd.is_colliding()) and !is_in_group("bomb"):
            while (rr.is_colliding() or ru.is_colliding() or rd.is_colliding()):
                var obj = rr.get_collider()
                add += 1
                var obj1 = ru.get_collider()
                add += 1
                var obj2 = rd.get_collider()
                add += 1
                objects_collide.append(add)
                # forces raycasts to continue casting until they dont collide
                if rr.is_colliding():
                    rr.add_exception(obj)
                    rr.force_raycast_update()
                if ru.is_colliding():
                    ru.add_exception(obj1)
                    ru.force_raycast_update()
                if rd.is_colliding():
                    rd.add_exception(obj2)
                    rd.force_raycast_update()
        if objects_collide.size() == 1:
            s1.visible = true
        if objects_collide.size() == 2:
            s2.visible = true
        if objects_collide.size() == 3:
            s3.visible = true

Tags :

Best Answer

  • BimbamBimbam Posts: 20
    edited June 25 Accepted Answer

    SOOOO I had an afternoon to kill (it's raining) and it seemed like a good exercise to learn more myself, and, well, I may have accidentally written my own version of what I think your trying to get at (attached).

    I did start by trying to just replicate your code, but as soon as I thought about creating raycast nodes by hand in what I assumed was going to be 8 directions, I thought 'yehnah' and did it with code instead. Also, I assumed this was a button click type game and not a controllable character walking around, but the principal is the same I guess.

    Then as testing with console prompts was getting laborious I added a next_pass effect onto the material to show which 'bombs' have been scanned... and before you know it, ended up like this:

    Which looks like this when one is selected (and if you select a bomb, it does not count it in the total FYI):

    Essentially if you have a grid of 'bomb's and click somewhere, it should tally all the bombs in the cardinal directions from that bomb by iterating a list of vectors to fire the raycast to. For each iteration it follows the same principal as your code, that it adds the object to an exception list and refires until there is nothing left.
    If you wanted to you could use this method to tally up how many are in a given direction and not just the total, but felt like overkill.

    I was going to setup an array system for generating the bombs but for the purpose of a one off I just duplicated/made unique. Also the next pass code is horrendously inefficient so you will notice a bit of lag between button clicks/highlighting but I can't be bothered to tidy it up (probably just need to generate the next pass material in advance rather than on the fly and use call_deferred to update it).... in fact in general just assume this is not a great way to do it, but it does work haha.

    Let me know if you have any questions or this wasn't what you were aiming at. Learnt some new stuff about casting with code so I'm happy to help ^^.

    As a final sidenote, does anyone else use a massive ASCI art template for their code to make the mini sidebar thing more helpful?

    You get that for free ;)

Answers

  • BimbamBimbam Posts: 20Member
    edited June 24

    I have a few noob questions:

    Re. "!is_in_group("bomb")", shouldn't this be referencing the collider object? I am not sure what this is checking, but my weak understanding implies it's just confirming whether the node this script is run on is not in the group "Bomb" rather than the raycast's collision object (maybe by design? or I'm an idiot ^^)?

    Re. "while (rr.is_colliding() or ru.is_colliding() or rd.is_colliding()):"
    This 'looks' like regardless of which raycast is colliding, 'Add' would be incremented by 3 every time the while loop completes. Is this desired? I would guess the "add += 1" should be in each 'if xx.is_colliding()' block. I think that whole block might be doubling up a bit and could be rewritten as:
    if rr.is_colliding():
    add+=1
    rr.add_exception(rr.get_collider())
    rr.force_raycast_update()
    if ru.is_colliding():
    add+=1
    ru.add_exception(ru.get_collider())
    ru.force_raycast_update()
    if rd.is_colliding():
    add+=1
    rd.add_exception(rd.get_collider())
    rd.force_raycast_update()

    EDIT
    Noticed the sum of add isn't then used so am confused by the purpose of 'objects_collide'? by just checking the size of the array it currently looks like it is just counting how many times the while loop has been iterated. Is that deliberate?

  • MentirasMentiras Posts: 45Member
    edited June 24

    I will preface my answer by saying I am a beginner as well =)

    1. Yes, the !is_in_group(bomb) says that if any of those raycasts are colliding and not a bomb themselves, then continue down the logic path. If they are a bomb they are not involved (yet).
    2. This is probably where my error is because I got this section of code from a post that wasn't related to my game. But what I hoped it was doing was if any of those raycasts are colliding it adds 1 to the total number of collisions.
    3. This could also be where the problem is because what it should be doing is getting the total number of entries in the array. Which would be the number of bombs it has encountered, ideally from any direction.
  • BimbamBimbam Posts: 20Member
    edited June 25 Accepted Answer

    SOOOO I had an afternoon to kill (it's raining) and it seemed like a good exercise to learn more myself, and, well, I may have accidentally written my own version of what I think your trying to get at (attached).

    I did start by trying to just replicate your code, but as soon as I thought about creating raycast nodes by hand in what I assumed was going to be 8 directions, I thought 'yehnah' and did it with code instead. Also, I assumed this was a button click type game and not a controllable character walking around, but the principal is the same I guess.

    Then as testing with console prompts was getting laborious I added a next_pass effect onto the material to show which 'bombs' have been scanned... and before you know it, ended up like this:

    Which looks like this when one is selected (and if you select a bomb, it does not count it in the total FYI):

    Essentially if you have a grid of 'bomb's and click somewhere, it should tally all the bombs in the cardinal directions from that bomb by iterating a list of vectors to fire the raycast to. For each iteration it follows the same principal as your code, that it adds the object to an exception list and refires until there is nothing left.
    If you wanted to you could use this method to tally up how many are in a given direction and not just the total, but felt like overkill.

    I was going to setup an array system for generating the bombs but for the purpose of a one off I just duplicated/made unique. Also the next pass code is horrendously inefficient so you will notice a bit of lag between button clicks/highlighting but I can't be bothered to tidy it up (probably just need to generate the next pass material in advance rather than on the fly and use call_deferred to update it).... in fact in general just assume this is not a great way to do it, but it does work haha.

    Let me know if you have any questions or this wasn't what you were aiming at. Learnt some new stuff about casting with code so I'm happy to help ^^.

    As a final sidenote, does anyone else use a massive ASCI art template for their code to make the mini sidebar thing more helpful?

    You get that for free ;)

  • BimbamBimbam Posts: 20Member
    edited June 25

    Yeah I couldn't let that next pass material abuse bad practice horror show slide, so I made a new version of Controls.gd to get rid of 95% of the material change lag (it really wasn't much to fix it, I just have a horrible tendency to write hacky code quickly rather than good well thought out code).

    The very first click takes a moment, but I have my suspicions about OpenGL Shaders being the cause -.-

  • MentirasMentiras Posts: 45Member

    @Bimbam thank you for all the work you put in there =)

  • MegalomaniakMegalomaniak Posts: 2,625Admin

    @Bimbam that is one awesome minimap.

  • MentirasMentiras Posts: 45Member
    edited June 26

    @Bimbam

    As a final sidenote, does anyone else use a massive ASCI art template for their code to make the mini sidebar thing more helpful?

    I think the only appropriate response to that is "I do now". Very cool idea.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file