C# Platform Collisions Not Working With Multiple Platforms











up vote
0
down vote

favorite












Right now I'm trying to make a simple game editor in C#, however there's a problem with when the user adds more than one platform to the screen:



private void tmrRunGame_Tick(object sender, EventArgs e)
{
foreach(Platform plat in platList)
{
if (plat.getBounds().IntersectsWith(player.getBounds()))
{
tmrGravity.Stop();
isColliding = true;
}
else
{
isColliding = false;
}
}

if(player.getY() < 500 && !isJumping && !isColliding)
{
tmrGravity.Start();
}
else
{
tmrGravity.Stop();
}
}


This code only stops the user from falling through the last created platform, all of the ones before that the user is able to just fall right through. What makes this all the more confusing is that the program is detecting collisions for all of the platforms, but only doing what it's supposed to for one! It's very frustrating and any help is appreciated.



This is how I'm adding platforms if that helps in any way:



private void pbPlatformSelect_MouseClick(object sender, MouseEventArgs e)
{
Platform plat = new Platform(100, 10, 50, 50);
plat.drawTo(this);
platList.Add(plat);
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    Right now I'm trying to make a simple game editor in C#, however there's a problem with when the user adds more than one platform to the screen:



    private void tmrRunGame_Tick(object sender, EventArgs e)
    {
    foreach(Platform plat in platList)
    {
    if (plat.getBounds().IntersectsWith(player.getBounds()))
    {
    tmrGravity.Stop();
    isColliding = true;
    }
    else
    {
    isColliding = false;
    }
    }

    if(player.getY() < 500 && !isJumping && !isColliding)
    {
    tmrGravity.Start();
    }
    else
    {
    tmrGravity.Stop();
    }
    }


    This code only stops the user from falling through the last created platform, all of the ones before that the user is able to just fall right through. What makes this all the more confusing is that the program is detecting collisions for all of the platforms, but only doing what it's supposed to for one! It's very frustrating and any help is appreciated.



    This is how I'm adding platforms if that helps in any way:



    private void pbPlatformSelect_MouseClick(object sender, MouseEventArgs e)
    {
    Platform plat = new Platform(100, 10, 50, 50);
    plat.drawTo(this);
    platList.Add(plat);
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Right now I'm trying to make a simple game editor in C#, however there's a problem with when the user adds more than one platform to the screen:



      private void tmrRunGame_Tick(object sender, EventArgs e)
      {
      foreach(Platform plat in platList)
      {
      if (plat.getBounds().IntersectsWith(player.getBounds()))
      {
      tmrGravity.Stop();
      isColliding = true;
      }
      else
      {
      isColliding = false;
      }
      }

      if(player.getY() < 500 && !isJumping && !isColliding)
      {
      tmrGravity.Start();
      }
      else
      {
      tmrGravity.Stop();
      }
      }


      This code only stops the user from falling through the last created platform, all of the ones before that the user is able to just fall right through. What makes this all the more confusing is that the program is detecting collisions for all of the platforms, but only doing what it's supposed to for one! It's very frustrating and any help is appreciated.



      This is how I'm adding platforms if that helps in any way:



      private void pbPlatformSelect_MouseClick(object sender, MouseEventArgs e)
      {
      Platform plat = new Platform(100, 10, 50, 50);
      plat.drawTo(this);
      platList.Add(plat);
      }









      share|improve this question















      Right now I'm trying to make a simple game editor in C#, however there's a problem with when the user adds more than one platform to the screen:



      private void tmrRunGame_Tick(object sender, EventArgs e)
      {
      foreach(Platform plat in platList)
      {
      if (plat.getBounds().IntersectsWith(player.getBounds()))
      {
      tmrGravity.Stop();
      isColliding = true;
      }
      else
      {
      isColliding = false;
      }
      }

      if(player.getY() < 500 && !isJumping && !isColliding)
      {
      tmrGravity.Start();
      }
      else
      {
      tmrGravity.Stop();
      }
      }


      This code only stops the user from falling through the last created platform, all of the ones before that the user is able to just fall right through. What makes this all the more confusing is that the program is detecting collisions for all of the platforms, but only doing what it's supposed to for one! It's very frustrating and any help is appreciated.



      This is how I'm adding platforms if that helps in any way:



      private void pbPlatformSelect_MouseClick(object sender, MouseEventArgs e)
      {
      Platform plat = new Platform(100, 10, 50, 50);
      plat.drawTo(this);
      platList.Add(plat);
      }






      c# game-physics collision






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 19:58









      D Manokhin

      609119




      609119










      asked Nov 21 at 19:56









      BenCompSci

      397




      397
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          Replace foreach loop with this code:



          var playerBounds = player.GetBounds ();
          isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
          if (isColliding) tmrGravity.Stop ();


          if you don't like LINQ, you can change you loop like this:



          var playerBounds = player.GetBounds ();
          isColliding = false;
          foreach (var plat in platList) {
          if (plat.GetBounds ().IntersectsWith (playerBounds)) {
          isColliding = true;
          tmrGravity.Stop ();
          break;
          }
          }





          share|improve this answer








          New contributor




          trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

























            up vote
            1
            down vote













            I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.



            So try putting 'break;' right after 'isColliding = true';



            This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.






            share|improve this answer























            • Yes that worked, thank you!
              – BenCompSci
              Nov 21 at 20:08











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419630%2fc-sharp-platform-collisions-not-working-with-multiple-platforms%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            Replace foreach loop with this code:



            var playerBounds = player.GetBounds ();
            isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
            if (isColliding) tmrGravity.Stop ();


            if you don't like LINQ, you can change you loop like this:



            var playerBounds = player.GetBounds ();
            isColliding = false;
            foreach (var plat in platList) {
            if (plat.GetBounds ().IntersectsWith (playerBounds)) {
            isColliding = true;
            tmrGravity.Stop ();
            break;
            }
            }





            share|improve this answer








            New contributor




            trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






















              up vote
              2
              down vote













              Replace foreach loop with this code:



              var playerBounds = player.GetBounds ();
              isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
              if (isColliding) tmrGravity.Stop ();


              if you don't like LINQ, you can change you loop like this:



              var playerBounds = player.GetBounds ();
              isColliding = false;
              foreach (var plat in platList) {
              if (plat.GetBounds ().IntersectsWith (playerBounds)) {
              isColliding = true;
              tmrGravity.Stop ();
              break;
              }
              }





              share|improve this answer








              New contributor




              trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.




















                up vote
                2
                down vote










                up vote
                2
                down vote









                Replace foreach loop with this code:



                var playerBounds = player.GetBounds ();
                isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
                if (isColliding) tmrGravity.Stop ();


                if you don't like LINQ, you can change you loop like this:



                var playerBounds = player.GetBounds ();
                isColliding = false;
                foreach (var plat in platList) {
                if (plat.GetBounds ().IntersectsWith (playerBounds)) {
                isColliding = true;
                tmrGravity.Stop ();
                break;
                }
                }





                share|improve this answer








                New contributor




                trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                Replace foreach loop with this code:



                var playerBounds = player.GetBounds ();
                isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
                if (isColliding) tmrGravity.Stop ();


                if you don't like LINQ, you can change you loop like this:



                var playerBounds = player.GetBounds ();
                isColliding = false;
                foreach (var plat in platList) {
                if (plat.GetBounds ().IntersectsWith (playerBounds)) {
                isColliding = true;
                tmrGravity.Stop ();
                break;
                }
                }






                share|improve this answer








                New contributor




                trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered Nov 21 at 20:10









                trollingchar

                1014




                1014




                New contributor




                trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                trollingchar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.
























                    up vote
                    1
                    down vote













                    I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.



                    So try putting 'break;' right after 'isColliding = true';



                    This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.






                    share|improve this answer























                    • Yes that worked, thank you!
                      – BenCompSci
                      Nov 21 at 20:08















                    up vote
                    1
                    down vote













                    I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.



                    So try putting 'break;' right after 'isColliding = true';



                    This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.






                    share|improve this answer























                    • Yes that worked, thank you!
                      – BenCompSci
                      Nov 21 at 20:08













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.



                    So try putting 'break;' right after 'isColliding = true';



                    This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.






                    share|improve this answer














                    I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.



                    So try putting 'break;' right after 'isColliding = true';



                    This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 21 at 20:09

























                    answered Nov 21 at 20:05









                    Ben Krueger

                    376417




                    376417












                    • Yes that worked, thank you!
                      – BenCompSci
                      Nov 21 at 20:08


















                    • Yes that worked, thank you!
                      – BenCompSci
                      Nov 21 at 20:08
















                    Yes that worked, thank you!
                    – BenCompSci
                    Nov 21 at 20:08




                    Yes that worked, thank you!
                    – BenCompSci
                    Nov 21 at 20:08


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419630%2fc-sharp-platform-collisions-not-working-with-multiple-platforms%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Catalogne

                    Violoncelliste

                    Héron pourpré