3 Replies Latest reply on Jun 9, 2015 2:12 PM by TomGreen

    Simple Collision Detection in Edge Animate

    BryanWangs

      I am new to edge animate and I've created a game in which a player moves their character left and right on the screen, trying to avoid colliding with other characters. I've been looking for a good and simple collision detection function/algorithm/plug-in to use to detect those collisions.

      The characters are all simple rectangles (convex?) and the other characters move vertically along the y-axis while the hero character can move left, right, up or down. I have two (2) issues that I can't seem to solve right now:

       

      1. How to keep my hero character within the boundaries of the stage (I don't want him going off the stage when the player tries to move him left/right/etc... too far;

      2. How to I detect collisions between the hero character and the 20 or so other characters that animate up and down along the y-axis?

       

      My thoughts are that bounding boxes would be all that is necessary for this rather simple game because I don't have a lot of characters to check for collisions and my characters are all rectangles. I've stripped it down to just two objects on the stage (a blue rectangle and a red rectangle) and I have them overlapping right now (should indicate a collision) and I've been trying to implement the following code to detect the collision. I've put this function in the compositionReady action:

       

                          var object1 = sym.$l("BlueRectangle");

                          var object2 = sym.$("RedRectangle");

                          if (object1.x < object2.x + object2.width  && object1.x + object1.width  > object2.x &&

                             object1.y < object2.y + object2.height && object1.y + object1.height > object2.y) {

                          // The objects are touching

                              alert("Oops! You crashed!");

                         setInterval(sym(e), 35);

      }

       

      But nothing happens when I test the animate project in a browser. No alert occurs. What am I doing wrong?

      Once I get it figured out, how do I implement the code to run collision detection for multiple rectangles that could collide with my hero character?

      How to I set the interval to check for collisions periodically and what is a good refresh rate?

       

      Any help would be greatly appreciated!