3 Replies Latest reply on Oct 17, 2012 11:34 AM by kglad

    Trying to get ricochet mechanics to work, need help!

    astalepancake

      Hey, for a project I'm doing, I'm making a ricochet game with Actionscript 2.0, where the player shoots a bullet and it bounces off objects trying to hit the opponent. It's working decently so far, but my problem is I'm having a VERY hard time getting it to work right.  Sometimes when I shoot at a box, the bullet will tunnel through it, bouncing off of invisible walls on its way. I just want the bullet to bounce off of the edge all around, but it is tunneling as I'll show you in a video I uploaded. Here's the code I'm using currently, but it's having this tunneling problem.

       

       

      if (bullet_fired.hitTest(boxobject)) {

                      if (bullet_fired._y<=184 || +bullet_fired._y>=214) {

                          bullet_fired.diry *= -1;

                          bounces += 1;

                      }else {

                          bullet_fired.dirx *= -1;

                          bounces += 1;

                      }

       

      where 184 is the top of the box and 214 is the bottom.

       

      Am i approaching this completely wrong or what am i doing wrong.

       

      Here's the video I uploaded of me demonstrating my problem. http://www.youtube.com/watch?v=O9dwW4_UcFM

       

      Please help me and give any suggestions you have, I'm pulling my hair out!!

        • 1. Re: Trying to get ricochet mechanics to work, need help!
          kglad Adobe Community Professional & MVP

          it looks like your bullet when fired at an angle close to 0 (or 180), repeatedly is changing y-direction.  to remedy, change y-direction AND move the bullet back to its previous position.

          1 person found this helpful
          • 2. Re: Trying to get ricochet mechanics to work, need help!
            astalepancake Level 1

            Well even sometimes after bouncing a few times, if it hits near the top/bottom of the box, it tunnels. And with the box on the right, it does the same thing but the other way, it tunnels up/down the box unlike this one which tunnels in the sides of it. I'll definitely try resetting the bullet outside the box after it hitTest's, but it just seems like this is a simple thing of bouncing a ball off a square, but it's being so stubborn. I feel like I'm approaching this all wrong and making it more complicated than it is. By looking at my code, can you tell if there is an easier/more accurate way of doing this?

            • 3. Re: Trying to get ricochet mechanics to work, need help!
              kglad Adobe Community Professional & MVP

              you're repeatedly satisfying both your hittest and your y constraints when the bullet is shot at a shallow angle.  ie, the y-property of your bullet does not change enough with the initial hit to prevent a 2nd, 3rd etc hit.

               

              again, to remedy, use:

               

              if (bullet_fired.hitTest(boxobject)) {

                              if (bullet_fired._y<=184 || +bullet_fired._y>=214) {

                                  bullet_fired.diry *= -1;

              bullet_fired._x=previousX;  // you need to define previousX and previousY

              bullet_fired._y=previousY;

                                  bounces += 1;

                              }else {

                                  bullet_fired.dirx *= -1;

                                  bounces += 1;

                              }