5 Replies Latest reply on Dec 8, 2006 9:33 PM by kglad

    Billiards and hitTests

    FlashIsTheCoolest123 Level 1
      Heya all. I hope I don't annoy you with this probably low-level ActionScript question, but, I continue.

      I am making a quick and cheesy billiards game, and I am able to get the first ball moving with the mouse, but for the "ricocheting of the balls", I am at a loss. Here is a little chunk of code that determines the already moving ball's speed:

      if (Dragging) {
      Stretch = Normal;
      VX = (x - PrevX) * 10;
      VY = (y - PrevY) * 10;
      }
      else {
      x = x + (VX/10);
      y = y + (VY/10);
      }

      That ball's instance name is "Ball", and the example ball that I am trying to ricochet away is "Ric". So in conclusion to all of this, I just need to know how to make the "Ric" Ball ricochet into the right direction, and the right speed, when hit by the moving ball. Oh, and to slow down the already moving ball's speed and change it's direction, depending on where it hits. If I am missing any information that you need to know, please inform me. This will be the coolest thing I have ever made if it works.

      Thanks SO much!

      --- Matt
        • 1. Re: Billiards and hitTests
          kglad Adobe Community Professional & MVP
          here's a billard table simulation i made for another poster. the only differences between this simulation and your simulation are the starting conditions and you'll probably want to add some rolling friction so the xVector and yVector decrease in absolute value with each time increment.

          the sim i made starts with a given number of balls on the table all at random locations moving at random speeds in random directions. you'll start with some other initial conditions. in particular all balls, except one, will have zero speed.

          • 2. Re: Billiards and hitTests
            FlashIsTheCoolest123 Level 1
            *Sings* tra la la la la, la la la la... . Wow, you know way too much stuff... Is there anything that is simpler then that? I'm not worried about friction or reducing speed when the ball hits a rail, or just natural friction of the table, but is there any code that may be simple like this?:

            onClipEvent(enterFrame){
            if(this.hitTest(_root.Ric)){
            VX = //something
            VY = //something
            Stretch -= 0.5
            }
            }

            ... I would love to base some stuff on your code, if I was smart . But, thank you very much still!
            • 3. Re: Billiards and hitTests
              kglad Adobe Community Professional & MVP
              i don't think so.

              the minimum you'll need, to give some appearence of realism, would be for the balls to interact with the sides of the table and for the balls to interact with each other. and that's all that's in the above code after the borderA = [boxtop, boxright, boxbottom, boxleft]; line.
              • 4. Re: Billiards and hitTests
                FlashIsTheCoolest123 Level 1
                Lets see what my head can do then.
                Thank you very much!

                --- Matt

                (By the way, you REALLY know your stuff )
                • 5. Re: Billiards and hitTests
                  kglad Adobe Community Professional & MVP
                  you're welcome.