5 Replies Latest reply on May 13, 2007 5:01 PM by Xpuc

    Bouncing Ball Algorithm

    Xpuc
      Hello everyone,
      This is the first time I am posting here, so excuse me if I posted at the wrong place. My problem: I want to create a 2D arcade game - I am sure you are all familiar with it - it's that classic game where you have the platform at the bottom of the screen (moving left and right) and many boxes on the top, and a bouncing ball which you should bounce off your platform and destroy all the boxes on the top to advance. My problem is that I am not sure how to make the ball bounce (be it of the wall, the platform or any box) in predictable and desired direction. For example if the ball hits the right side wall (coming from left and below the hitting point) at 45 degrees it should start moving after the hit again to the left (aka returning) and continue upwards at 45 degrees, and not go wherever it feels like. I hope explanation is clear enough, any help will be much appreciated. Also if you can post a sample script (because I am still not very familiar with coding, thus the simple game I am after).

      All the best to everyone and thanks again
        • 1. Re: Bouncing Ball Algorithm
          Level 7

          In case of horizontal or vertical walls the response of the ball should
          be to reverse it's x- or y-direction. easiest example:

          ball moves 2px per frame in the y- direction (moving right)
          if ball 'hits' stage-right it should bounce and move -2px (moving left)

          in your example:
          ball moves 2px per frame in the y- and -2 in the x-direction (moving
          right and up)
          if ball 'hits' stage-right it should bounce and move -2px in the
          y-direction (moving left) and maintain it's x-direction (moving up)

          Determining a hit depends a bit on the gfx you use, but usually involves
          the position of the ball, it's radius and the position of the wall.

          Ofcourse you'd have to take into account 'some' physics with gravity and
          the ball coming to a stop some time.

          http://www.jmckell.com/ has a great number of pages on lingo and animation.

          HTH,
          Manno

          Xpuc wrote:
          > Hello everyone,
          > This is the first time I am posting here, so excuse me if I posted at the
          > wrong place. My problem: I want to create a 2D arcade game - I am sure you are
          > all familiar with it - it's that classic game where you have the platform at
          > the bottom of the screen (moving left and right) and many boxes on the top, and
          > a bouncing ball which you should bounce off your platform and destroy all the
          > boxes on the top to advance. My problem is that I am not sure how to make the
          > ball bounce (be it of the wall, the platform or any box) in predictable and
          > desired direction. For example if the ball hits the right side wall (coming
          > from left and below the hitting point) at 45 degrees it should start moving
          > after the hit again to the left (aka returning) and continue upwards at 45
          > degrees, and not go wherever it feels like. I hope explanation is clear enough,
          > any help will be much appreciated. Also if you can post a sample script
          > (because I am still not very familiar with coding, thus the simple game I am
          > after).
          >
          > All the best to everyone and thanks again
          >

          --
          manno bult
          manno@aloft.nl
          http://www.aloft.nl
          • 2. Re: Bouncing Ball Algorithm
            Xpuc Level 1
            Thanks Manno,

            Hitting the walls shouldn't be a problem anymore thanks to your help. However I am still a bit lost with the ball bouncing off the bricks in the playfield. Every wall is going to be a different sprite so I can write code for each wall, but bricks are one sprite with four sides and I don't know how to tell Director which side the ball bounced at and at what angle, or should I create every brisk with four different sprites for each wall.

            Thanks
            • 3. Re: Bouncing Ball Algorithm
              Doc Raman Level 1
              There are a number of ways to deal with collisions. One way is to use the Havok Xtra (3d physics Xtra), and just display a 2d view of things. I tend to just do a lot of 2d basic physics calculations.

              Below is a behavior you can attach to the ball that uses Newtonian physics (or at least close) to move around and bounce off the walls of a region, and rectangular sprites (watch out for wrap and odd things). The code is definitely not optimized for speed.

              An example of this is shown here (I'll try to do a post this week with source)
              http://www.directoratnight.com/wp-content/uploads/2007/05/pinball_rectangle.html

              Info about the Havok Xtra can be found here:
              http://www.director-online.com/havok/

              And if you are interested in a book, here are two that might interest you:
              1) Mathematics and Physics for Programmers (Game Development Series) by Danny Kodicek
              2) Advanced Lingo for Games by Gary Rosenzweig

              -----------------

              --
              -- Bounce Around Inside Rectangular World
              -- R. Pfaff, multimediaguy@gmail.com
              -- http://www.DirectorAtNight.com
              --
              -- This behavior can be applied to a sprite. It will move
              -- around within a rectangular region, and bounce off any
              -- rectangular sprites it comes in to contact with.

              ---------------------- PROPERTIES
              property pSprite
              property pPos, pVel, pAcc -- position, velocity, acceleration
              property pDt -- time step
              property pGravity -- gravity

              property pRoomSprite, pRoomRect -- sprite of the room (and rectangle of that room)
              property pBlockList -- list of sprite numbers representing rectangles to bounce from
              property pPosBeforeMove -- keep track of prior step of motion

              property pDrag -- drag force in the room (1.0 = no drag, 0.0 = infinite drag, suggest 0.998-ish)
              property pRoomBounce -- elasticity of walls (1.0 = no loss of speed, 0.0 = total loss)
              property pBlockBounce -- elasticity of rectangles

              property pActive -- activity flag


              ---------------------- PROPERTY LIST
              on getPropertyDescriptionList me
              p = [:]
              p[#pRoomSprite] = [#format:#sprite, #default:sprite(1), #comment:"Sprite of room: "]
              p[#pBlockList] = [#format:#list, #default:[2,3,4,5], #comment:"List of blocks: "]
              p[#pDt] = [#format:#float, #default:0.5, #comment:"Delta T: "]
              p[#pVel] = [#format:#list, #default:[10.0, -10.0], #comment:"Initial Velocity: "]
              p[#pGravity] = [#format:#list, #default:[0.0, 0.98], #comment:"Gravity: "]
              p[#pDrag] = [#format:#float, #default:0.998, #comment:"Drag (suggest 0 to 1): "]
              p[#pBlockBounce] = [#format:#float, #default:0.75, #comment:"Block elasticity (suggest 0 to 1): "]
              p[#pRoomBounce] = [#format:#float, #default:1.0, #comment:"Wall elasticity (suggest 0 to 1): "]
              return p
              end

              ---------------------- BEGINSPRITE
              on beginSprite me
              pSprite = sprite(me.spriteNum)
              pPos = [pSprite.locH * 1. , pSprite.locV * 1.]
              pPosBeforeMove = duplicate(pPos)
              pAcc = duplicate(pGravity)
              pRoomRect = pRoomSprite.rect
              pActive = 0
              end

              ---------------------- EXITFRAME
              on exitFrame me
              if pActive then
              pPosBeforeMove = duplicate(pPos)
              me.moveAlong()
              -- see if I hit a block
              me.checkBlockCollision()
              -- see if I hit a wall
              me.checkRoomBounds()
              pSprite.loc = point(pPos[1], pPos[2])
              end if
              end


              ---------------------- AND THE REST

              on moveAlong me
              me.findAcc()
              me.findVel()
              me.findPos()
              end
              on findAcc me
              pAcc = pGravity
              end
              on findVel me
              pVel = pVel * pDrag + pAcc*pDt
              end
              on findPos me
              pPos = pPos + pVel*pDt
              end

              on checkRoomBounds me
              -- check to see if I hit a wall
              if point(pPos[1], pPos[2]).inside(pRoomRect) then return
              aLeft = pRoomRect.left
              aRight = pRoomRect.right
              aTop = pRoomRect.top
              aBottom = pRoomRect.bottom
              x = pPos[1]
              y = pPos[2]
              if x <= aLeft then
              x = aLeft
              pPos[1] = x
              pVel[1] = -1. * pVel[1] * pRoomBounce
              return
              end if
              if x >= aRight then
              x = aRight
              pPos[1] = x
              pVel[1] = -1. * pVel[1] * pRoomBounce
              return
              end if
              if y <= aTop then
              y = aTop
              pPos[2] = y
              pVel[2] = -1. * pVel[2] * pRoomBounce
              return
              end if
              if y >= aBottom then
              y = aBottom
              pPos[2] = y
              pVel[2] = -1. * pVel[2] * pRoomBounce
              return
              end if
              end

              on checkBlockCollision me
              -- check to see if I hit any of the rectangles
              repeat with i in pBlockList
              blockRect = sprite(i).rect
              aLeft = blockRect.left * 1.
              aRight = blockRect.right * 1.
              aTop = blockRect.top * 1.
              aBottom = blockRect.bottom * 1.
              x = pPos[1]
              y = pPos[2]
              if (x >= aLeft and x <= aRight) and (y >= aTop and y <= aBottom) then
              xi = pPosBeforeMove[1]
              yi = pPosBeforeMove[2]
              if (aLeft-xi >= 0. and x-aLeft > 0.) then
              x = aLeft
              pPos[1] = x
              pVel[1] = -1. * pVel[1] * pBlockBounce
              end if
              if (xi - aRight >= 0. and aRight-x > 0.) then
              x = aRight
              pPos[1] = x
              pVel[1] = -1. * pVel[1] * pBlockBounce
              end if
              if ( aTop - yi >= 0. AND y - aTop > 0. ) then
              y = aTop
              pPos[2] = y
              pVel[2] = -1. * pVel[2] * pBlockBounce
              end if
              if ( yi - aBottom >= 0. AND aBottom - y > 0. ) then
              y = aBottom
              pPos[2] = y
              pVel[2] = -1. * pVel[2] * pBlockBounce
              end if
              end if
              end repeat
              end

              on changeActivation me, aState
              pActive = aState
              end
              • 4. Re: Bouncing Ball Algorithm
                Doc Raman Level 1
                Junior member...when was I demoted? Arrghh...(says a member since the Director 4 days). Life will go on!

                :)
                • 5. Re: Bouncing Ball Algorithm
                  Xpuc Level 1
                  Thank you Doc Raman

                  That long and self-explanatory post really helped. Much appreciated.