11 Replies Latest reply on May 21, 2007 4:04 AM by voguecreative

    Pool Game

    voguecreative
      Hey. Im having no luck whatsoever in creating a snooker game in Director. I have created all the images for tables etc and began to code the script.

      The game is basically a one ball snooker game with the aim of potting the ball in any pocket. I have two sprites (ball and cue).

      The game runs like this.

      The user moves the mouse which rotates the cue around the ball - DONE
      The user clicks the mouse to set the cue in place - DONE
      The user presses SPACE BAR and the ball travels in the direction the cue is facing towards. - THIS IS WHERE I AM HAVING PROBLEMS

      so far my code looks like this:

      [code]
      function exitframe(me) {


      _global.angle1 = Math.atan((_mouse.mouseV-sprite("cue").locV )/(_mouse.mouseH-sprite("cue").locH ))

      if (_mouse.mouseH < sprite("cue").locH) {
      sprite("cue").rotation = _global.angle1*180/Math.PI -90
      }
      else {
      sprite("cue").rotation = _global.angle1*180/Math.PI +90
      }

      }
      function beginsprite(me) {
      cuelock = 0
      }


      function exitframe(me) {

      if (cuelock == 0) {
      _global.angle1 = Math.atan((_mouse.mouseV-sprite("cue").locV )/(_mouse.mouseH-sprite("cue").locH ))
      if (_mouse.mouseH < sprite("cue").locH) {
      sprite("cue").rotation = _global.angle1*180/Math.PI -90
      }
      else {
      sprite("cue").rotation = _global.angle1*180/Math.PI +90
      }
      }
      if ((cuelock == 1) && (_key.keyPressed(49)) && (_mouse.mouseH < sprite("cue").locH)) {


      sprite(2).loch = _global.angle1*180/Math.PI +90
      sprite(2).locv = _global.angle1*180/Math.PI +90
      }
      else {

      sprite(2).loch = _global.angle1*180/Math.PI -90
      sprite(2).locv = _global.angle1*180/Math.PI -90


      }
      }

      function mouseup(me) {
      cuelock = 1
      }


      function rightmouseup(me) {
      cuelock = 0
      }
      [/code]

      Can anyone help me finish this off asap as its driving me crazy...seriously i wake up in the night with visions of pool tables and angles and maths....

      Im willing to negotiate a reward for anyone who can help me complete this game. It needs to be in Javascript for Director MX04 as that is the software im comfortable using.

      Thanks

      Tom
        • 1. Re: Pool Game
          Level 7
          thoford75 wrote:
          > Hey. Im having no luck whatsoever in creating a snooker game in
          > Director. I have created all the images for tables etc and began to
          > code the script.

          > _global.angle1 =
          > Math.atan((_mouse.mouseV-sprite("cue").locV)/(_mouse.mouseH-sprite("cue").locH
          > ))

          For a start, I'd use Math.atan2 to avoid the hassle of checking which
          quadrant it was in, and the otherwise messy case of
          _mouse.mouseH==sprite("cue").locH, which would lead to a division by zero
          error (unless Math.atan understands INF).

          Andrew


          • 2. Re: Pool Game
            voguecreative Level 1
            Hi. Thanks for your help, but i am still unable to send the ball along the angle of the cue?
            • 3. Re: Pool Game
              Level 7
              thoford75 wrote:
              > Hi. Thanks for your help, but i am still unable to send the ball
              > along the angle of the cue?

              I didn't see any code in your OP for moving the cue ball...

              Andrew


              • 4. Re: Pool Game
                voguecreative Level 1
                if ((cuelock == 1) && (_key.keyPressed(49)) && (_mouse.mouseH < sprite("cue").locH)) {


                sprite(2).loch = _global.angle1*180/Math.PI +90
                sprite(2).locv = _global.angle1*180/Math.PI +90
                }
                else {

                sprite(2).loch = _global.angle1*180/Math.PI -90
                sprite(2).locv = _global.angle1*180/Math.PI -90


                }
                }

                This is the code to move the ball on the press of space bar
                • 5. Re: Pool Game
                  Level 7
                  thoford75 wrote:
                  > if ((cuelock == 1) && (_key.keyPressed(49)) && (_mouse.mouseH <
                  > sprite("cue").locH)) {
                  >
                  >
                  > sprite(2).loch = _global.angle1*180/Math.PI +90
                  > sprite(2).locv = _global.angle1*180/Math.PI +90
                  > }
                  > else {
                  >
                  > sprite(2).loch = _global.angle1*180/Math.PI -90
                  > sprite(2).locv = _global.angle1*180/Math.PI -90
                  > }
                  > }
                  >
                  > This is the code to move the ball on the press of space bar

                  Ah, see, you're not actually /changing/ the position of the ball (presumably
                  in sprite(2)) with that code, you're just setting it to one value.

                  Perhaps the following would be helpful to you:-

                  Animation maths in Lingo:
                  http://www.jmckell.com/

                  Pool physics:
                  http://www.gamasutra.com/features/20020118/vandenhuevel_01.htm
                  (You will have to register (free) if you're not already registered.)

                  A pool demo using the Havok xtra in Director:
                  http://director-online.com/havok/demos/demo-pool.html

                  Andrew


                  • 6. Re: Pool Game
                    voguecreative Level 1
                    Thank you for your help Andrew it is greatly appreciated. However all i require is the code that allows the cue ball sprite to move 100px in the direction the cuestick is facing..
                    • 7. Re: Pool Game
                      Level 7
                      thoford75 wrote:
                      > Thank you for your help Andrew it is greatly appreciated. However all
                      > i require is the code that allows the cue ball sprite to move 100px
                      > in the direction the cuestick is facing..

                      Add 100*cos(theta) to x and 100*sin(theta) to y, where theta is the angle
                      the cue is facing (in radians). Don't forget that Director's y-axis is
                      upside-down.

                      Andrew


                      • 8. Re: Pool Game
                        voguecreative Level 1
                        Cheers mate. How do i get the angle the cue is facing?
                        • 9. Re: Pool Game
                          Level 7
                          thoford75 wrote:
                          > Cheers mate. How do i get the angle the cue is facing?

                          Isn't that what your angle1 variable is referring to?

                          Andrew


                          • 10. Re: Pool Game
                            voguecreative Level 1
                            if ((cuelock == 1) && (_key.keyPressed(49))) {




                            sp = sprite(2)








                            sp.loch = Math.sin(_global.angle1) * 100
                            sp.locv = Math.cos(_global.angle1) * 100






                            }

                            This code works but the ball does not go off at the correct angle....
                            • 11. Re: Pool Game
                              voguecreative Level 1
                              quote:

                              Originally posted by: thoford75
                              if ((cuelock == 1) && (_key.keyPressed(49))) {




                              sp = sprite(2)








                              sp.loch = Math.sin(_global.angle1) * 100
                              sp.locv = Math.cos(_global.angle1) * 100






                              }


                              This code works but the ball does not go off at the correct angle....

                              Any suggestions?