3 Replies Latest reply on Oct 25, 2013 12:07 AM by pauland

    collision detection and avoid

    jasonzhuang

      The scenario is when dragging a item to a "fix and dangerous" item, it should detect the collision and move away from it. The collision detection already works fine, the problem is how to avoid collision.

      Question: After collision detected, how to perform the avoid collision and move away from danger item?

       

      I also post the question on Stack Overflow, which has more information. http://stackoverflow.com/questions/19534684/rectangle-collision-detection-and-avoid

        • 1. Re: collision detection and avoid
          pauland Level 4

          Have an 'invisible' rectangle that sitsover the object and extends beyond the visible area. Use that for collision detection - you will detect the collision before the object looks like it's colliding. You can then adjust the x,y position of the object to be further away from the object with which the collision is immenent.

          • 2. Re: collision detection and avoid
            jasonzhuang Level 1

            howo to adjust x,y position? This is really my problem.

            Suppose the fix item bounds is fixBound = new Rectangle(x1,y1,w1,h1); the moving item bounds is movingBound = new Rectangle(x2,y2,w2,h2); the moving item moved offsetPoint is offset = new Point(x,y);

            • 3. Re: collision detection and avoid
              pauland Level 4

              A simple approach to this is to remember the x and y co-ordinate of the item being dragged. So on start drag, record the x,y position. whenever you receive a move or drag end event, check for a collision. If there's a collision, reset the position of the item to the last recorded position. If there's no collision, you can record the update x, y co-ordinates and continue.

               

              This will stop anyone dragging an item over a dodgy object.

               

              Googling " as3 collision avoidance" may help.