Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. The classic bouncing ball logic!!

The classic bouncing ball logic!!

Scheduled Pinned Locked Moved C#
questionhelp
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Muammar
    wrote on last edited by
    #1

    Hey everyone, I'm sorry I'm asking this kind of question but I completely lost my logic today and need your help on this.. You see, it's a simple bouncing ball application, how can I make the ball bounce randomly and freely in the screen and still look natural.. Many thanks guys, here's what I'm doing now:

            if ((y + h) >= ScreenH)//hit bottom
                CurrentDirection = BallDirection.Right;
    
            if ((x + w) >= ScreenW)//hit right
                CurrentDirection = BallDirection.Top;
    
            if ((x) <= 0)//hit left
                CurrentDirection = BallDirection.Bottom;
    
            if ((y) <= 0)//hit top
                CurrentDirection = BallDirection.Left;
    
    
    
            switch(CurrentDirection)
            {
                case BallDirection.Bottom:
                x += step;
                y += step;
                break;
    
                case BallDirection.Top:
                x -= step;
                y -= step+7;
                break;
    
                case BallDirection.Left:
                x -= step;
                y += step;
                break;
    
                case BallDirection.Right:
                x += step;
                y -= step;
                break;
        }
    
    
            pb.Left = x;
            pb.Top = y;
            Invalidate();
        }
    
    L L 3 Replies Last reply
    0
    • M Muammar

      Hey everyone, I'm sorry I'm asking this kind of question but I completely lost my logic today and need your help on this.. You see, it's a simple bouncing ball application, how can I make the ball bounce randomly and freely in the screen and still look natural.. Many thanks guys, here's what I'm doing now:

              if ((y + h) >= ScreenH)//hit bottom
                  CurrentDirection = BallDirection.Right;
      
              if ((x + w) >= ScreenW)//hit right
                  CurrentDirection = BallDirection.Top;
      
              if ((x) <= 0)//hit left
                  CurrentDirection = BallDirection.Bottom;
      
              if ((y) <= 0)//hit top
                  CurrentDirection = BallDirection.Left;
      
      
      
              switch(CurrentDirection)
              {
                  case BallDirection.Bottom:
                  x += step;
                  y += step;
                  break;
      
                  case BallDirection.Top:
                  x -= step;
                  y -= step+7;
                  break;
      
                  case BallDirection.Left:
                  x -= step;
                  y += step;
                  break;
      
                  case BallDirection.Right:
                  x += step;
                  y -= step;
                  break;
          }
      
      
              pb.Left = x;
              pb.Top = y;
              Invalidate();
          }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      We got an article on Gravity and Collision Simulation in C#[^] that might help :)

      I are Troll :suss:

      M 1 Reply Last reply
      0
      • L Lost User

        We got an article on Gravity and Collision Simulation in C#[^] that might help :)

        I are Troll :suss:

        M Offline
        M Offline
        Muammar
        wrote on last edited by
        #3

        Thanks Eddy, unfortunately it doesn't help me!! I need a free bouncing behavior of one ball against walls.. Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't Thanks Ed.

        L 2 Replies Last reply
        0
        • M Muammar

          Thanks Eddy, unfortunately it doesn't help me!! I need a free bouncing behavior of one ball against walls.. Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't Thanks Ed.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I have found very useful links. http://www.vb-helper.com/howto_net_bouncing_ball.html[^] http://www.krissteele.net/blogdetails.aspx?id=87[^] HTH

          Jinal Desai - LIVE Experience is mother of sage....

          1 Reply Last reply
          0
          • M Muammar

            Hey everyone, I'm sorry I'm asking this kind of question but I completely lost my logic today and need your help on this.. You see, it's a simple bouncing ball application, how can I make the ball bounce randomly and freely in the screen and still look natural.. Many thanks guys, here's what I'm doing now:

                    if ((y + h) >= ScreenH)//hit bottom
                        CurrentDirection = BallDirection.Right;
            
                    if ((x + w) >= ScreenW)//hit right
                        CurrentDirection = BallDirection.Top;
            
                    if ((x) <= 0)//hit left
                        CurrentDirection = BallDirection.Bottom;
            
                    if ((y) <= 0)//hit top
                        CurrentDirection = BallDirection.Left;
            
            
            
                    switch(CurrentDirection)
                    {
                        case BallDirection.Bottom:
                        x += step;
                        y += step;
                        break;
            
                        case BallDirection.Top:
                        x -= step;
                        y -= step+7;
                        break;
            
                        case BallDirection.Left:
                        x -= step;
                        y += step;
                        break;
            
                        case BallDirection.Right:
                        x += step;
                        y -= step;
                        break;
                }
            
            
                    pb.Left = x;
                    pb.Top = y;
                    Invalidate();
                }
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You're doing it the hard way, why not just store a speed vector? (and don't create a struct/class for it, just store two ints or floats somewhere) Speed can be negative, depending on the direction. It will also make it much easier if you decide to do "other funny physics things", like gravity or other accelerations; you could just add the acceleration vector to the speed vector every update. (or scaled by time, if your steps are not fixed) To bounce that way, you can do something like

            until no change
            {
            if (ball outside of horizontal range)
            mirror speed horizontally and mirror ball around the edge it hits
            if (ball outside of vertical range)
            mirror speed vertically and mirror ball around the edge it hits
            }

            Muammar© wrote:

            y -= step+7;

            That is a little odd.

            1 Reply Last reply
            0
            • M Muammar

              Thanks Eddy, unfortunately it doesn't help me!! I need a free bouncing behavior of one ball against walls.. Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't Thanks Ed.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Muammar© wrote:

              unfortunately it doesn't help me!!

              I was guessing that you were stuck on the gravity-part, adjusting speed when you hit a wall. What are you exactly stuck with, and how can I help?

              Muammar© wrote:

              Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't

              I did honey, and you stated that it didn't help.

              I are Troll :suss:

              M 1 Reply Last reply
              0
              • L Lost User

                Muammar© wrote:

                unfortunately it doesn't help me!!

                I was guessing that you were stuck on the gravity-part, adjusting speed when you hit a wall. What are you exactly stuck with, and how can I help?

                Muammar© wrote:

                Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't

                I did honey, and you stated that it didn't help.

                I are Troll :suss:

                M Offline
                M Offline
                Muammar
                wrote on last edited by
                #7

                Thanks Ed. I just wanted the angle of the movement to change randomly each time I hit the walls.. and I'm not a honey :-\ BTW: I felt sorry yesterday for your world cup loss, you did wonderful nevertheless :)

                L 1 Reply Last reply
                0
                • M Muammar

                  Hey everyone, I'm sorry I'm asking this kind of question but I completely lost my logic today and need your help on this.. You see, it's a simple bouncing ball application, how can I make the ball bounce randomly and freely in the screen and still look natural.. Many thanks guys, here's what I'm doing now:

                          if ((y + h) >= ScreenH)//hit bottom
                              CurrentDirection = BallDirection.Right;
                  
                          if ((x + w) >= ScreenW)//hit right
                              CurrentDirection = BallDirection.Top;
                  
                          if ((x) <= 0)//hit left
                              CurrentDirection = BallDirection.Bottom;
                  
                          if ((y) <= 0)//hit top
                              CurrentDirection = BallDirection.Left;
                  
                  
                  
                          switch(CurrentDirection)
                          {
                              case BallDirection.Bottom:
                              x += step;
                              y += step;
                              break;
                  
                              case BallDirection.Top:
                              x -= step;
                              y -= step+7;
                              break;
                  
                              case BallDirection.Left:
                              x -= step;
                              y += step;
                              break;
                  
                              case BallDirection.Right:
                              x += step;
                              y -= step;
                              break;
                      }
                  
                  
                          pb.Left = x;
                          pb.Top = y;
                          Invalidate();
                      }
                  
                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  That is all wrong. You have a position p (with x and y components), a speed v (with x and y components), and an acceleration/gravity a (possibly with x and y components). For small time steps, you could evolve like this (apply to each component individually):

                  p+=v;
                  v+=a;

                  where a probably is just a gravitational constant (which happens to be positive since the y-axis is pointing downward in GDI+). And collisions would invert the relevant speed component, so floor/ceiling would do v.y=-v.y; and left/right walls would cause v.x=-v.x; ADDED optionally you could also reduce the speed by some factor (say v*=0.9) on each collision to mimic non-elasticity. /ADDED :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  modified on Tuesday, July 13, 2010 7:42 AM

                  D 1 Reply Last reply
                  0
                  • M Muammar

                    Thanks Ed. I just wanted the angle of the movement to change randomly each time I hit the walls.. and I'm not a honey :-\ BTW: I felt sorry yesterday for your world cup loss, you did wonderful nevertheless :)

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Muammar© wrote:

                    I just wanted the angle of the movement to change randomly each time I hit the walls..

                    That would give an unnatural effect, a bounce needs to be the opposite of the approaching direction and speed. Then there's acceleration, unless you want the ball to bounce at a constant speed. To translate this to your code; the current speed of your picturebox is determined by the Step variable?

                    Muammar© wrote:

                    and I'm not a honey

                    Hehe, point taken.

                    Muammar© wrote:

                    I felt sorry yesterday for your world cup loss, you did wonderful nevertheless

                    Thanks for the sentiment - I don't understand even the basic rules of the game, so I can't have an opinion really. The only part that I saw in the reruns this morning is a karate-kick in the chest of a soccer-player.

                    I are Troll :suss:

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      That is all wrong. You have a position p (with x and y components), a speed v (with x and y components), and an acceleration/gravity a (possibly with x and y components). For small time steps, you could evolve like this (apply to each component individually):

                      p+=v;
                      v+=a;

                      where a probably is just a gravitational constant (which happens to be positive since the y-axis is pointing downward in GDI+). And collisions would invert the relevant speed component, so floor/ceiling would do v.y=-v.y; and left/right walls would cause v.x=-v.x; ADDED optionally you could also reduce the speed by some factor (say v*=0.9) on each collision to mimic non-elasticity. /ADDED :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                      modified on Tuesday, July 13, 2010 7:42 AM

                      D Offline
                      D Offline
                      David Skelly
                      wrote on last edited by
                      #10

                      Luc Pattyn wrote:

                      a speed v (with x and y components)

                      Was I the only one paying attention in school physics classes? You there at the back, boy: what is the difference between speed and velocity? No cheating by looking it up in the text book.

                      Luc Pattyn wrote:

                      where a probably is just a gravitational constant

                      "A" gravitational constant? When I was at school, there was only one. I suppose it does make the simulation more interesting if you can adjust the fundamental nature of the universe. http://en.wikipedia.org/wiki/Gravitational_constant[^]

                      L 1 Reply Last reply
                      0
                      • D David Skelly

                        Luc Pattyn wrote:

                        a speed v (with x and y components)

                        Was I the only one paying attention in school physics classes? You there at the back, boy: what is the difference between speed and velocity? No cheating by looking it up in the text book.

                        Luc Pattyn wrote:

                        where a probably is just a gravitational constant

                        "A" gravitational constant? When I was at school, there was only one. I suppose it does make the simulation more interesting if you can adjust the fundamental nature of the universe. http://en.wikipedia.org/wiki/Gravitational_constant[^]

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        David Skelly wrote:

                        the difference between speed and velocity?

                        speed, velocity, derivative of position against time, it is all the same vector. If you want to play word games, be my guest.

                        David Skelly wrote:

                        "A" gravitational constant?

                        Here "a" stands for acceleration, i.e. force per unit of mass (remember Newton?); there could be many forces, from what the OP tells us gravity could be one of them. There may be other forces such as friction in a fluid, springs, you name it. The minimum to satisfy the OP seems to be a constant due to gravity, hence "a gravitational constant" (which, if you really payed attention isn't really constant, as it depends on distance and homogeneity of the masses involved, but from where he started out, that would be good enough an improvement already). :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                        D 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          David Skelly wrote:

                          the difference between speed and velocity?

                          speed, velocity, derivative of position against time, it is all the same vector. If you want to play word games, be my guest.

                          David Skelly wrote:

                          "A" gravitational constant?

                          Here "a" stands for acceleration, i.e. force per unit of mass (remember Newton?); there could be many forces, from what the OP tells us gravity could be one of them. There may be other forces such as friction in a fluid, springs, you name it. The minimum to satisfy the OP seems to be a constant due to gravity, hence "a gravitational constant" (which, if you really payed attention isn't really constant, as it depends on distance and homogeneity of the masses involved, but from where he started out, that would be good enough an improvement already). :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                          D Offline
                          D Offline
                          David Skelly
                          wrote on last edited by
                          #12

                          Luc Pattyn wrote:

                          speed, velocity, derivative of position against time, it is all the same vector

                          But speed isn't a vector. It's a scalar. Or to put it another way, it is the derivative of distance against time, not position against time.

                          Luc Pattyn wrote:

                          acceleration, i.e. force per unit of mass

                          No, acceleration is the rate of change of velocity versus time.

                          Luc Pattyn wrote:

                          if you really payed attention

                          Well, that was always my problem in physics classes. It was just so dull.

                          Luc Pattyn wrote:

                          it depends on distance and homogeneity of the masses involved,

                          No, I'm pretty sure the gravitational constant is the same everywhere throughout the universe, which is why it's called a constant. ;)

                          P 1 Reply Last reply
                          0
                          • D David Skelly

                            Luc Pattyn wrote:

                            speed, velocity, derivative of position against time, it is all the same vector

                            But speed isn't a vector. It's a scalar. Or to put it another way, it is the derivative of distance against time, not position against time.

                            Luc Pattyn wrote:

                            acceleration, i.e. force per unit of mass

                            No, acceleration is the rate of change of velocity versus time.

                            Luc Pattyn wrote:

                            if you really payed attention

                            Well, that was always my problem in physics classes. It was just so dull.

                            Luc Pattyn wrote:

                            it depends on distance and homogeneity of the masses involved,

                            No, I'm pretty sure the gravitational constant is the same everywhere throughout the universe, which is why it's called a constant. ;)

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #13

                            Here on Gallifrey, gravity is whatever we want it to be. ;)

                            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                            My blog | My articles | MoXAML PowerToys | Onyx

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups