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 / C++ / MFC
  4. Ball Physics

Ball Physics

Scheduled Pinned Locked Moved C / C++ / MFC
game-devhelptutorial
6 Posts 4 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.
  • C Offline
    C Offline
    Cyclone_S
    wrote on last edited by
    #1

    Hi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.

    				if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds))
    				{
    					
    					if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right))
    					{
    						Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel;
    
    					}
    
    					if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom))
    					{
    						Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel;
    					}
    				}
    
    L CPalliniC S 3 Replies Last reply
    0
    • C Cyclone_S

      Hi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.

      				if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds))
      				{
      					
      					if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right))
      					{
      						Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel;
      
      					}
      
      					if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom))
      					{
      						Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel;
      					}
      				}
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You are using logical OR (||) instead of AND (&&). In the first case if BALL.RIGHT is greater than BRICK.LEFT OR BALL.LEFT is less than BRICK.RIGHT you slow it down. However you need both conditions to be true; for example if BALL.RIGHT is greater than BRICK.LEFT then you change velocity, but the ball is not necessarily less than BRICK.RIGHT at this point. I think the same holds true for the vertical movement.

      The best things in life are not things.

      C 1 Reply Last reply
      0
      • L Lost User

        You are using logical OR (||) instead of AND (&&). In the first case if BALL.RIGHT is greater than BRICK.LEFT OR BALL.LEFT is less than BRICK.RIGHT you slow it down. However you need both conditions to be true; for example if BALL.RIGHT is greater than BRICK.LEFT then you change velocity, but the ball is not necessarily less than BRICK.RIGHT at this point. I think the same holds true for the vertical movement.

        The best things in life are not things.

        C Offline
        C Offline
        Cyclone_S
        wrote on last edited by
        #3

        I tried using && instead of || but it messed up the ball physics. The ball would return in the same direction back towards the paddle. The problem seems to be that Ball.right is is always going to be greater then Ball.left when the ball is colliding with the top or bottom of the brick so the check for horizontal collision will also get executed. I'm not sure how to work around this. Thanks.

        1 Reply Last reply
        0
        • C Cyclone_S

          Hi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.

          				if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds))
          				{
          					
          					if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right))
          					{
          						Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel;
          
          					}
          
          					if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom))
          					{
          						Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel;
          					}
          				}
          
          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Cyclone_S wrote:

          Ball_List[b]->gameBall->Right >= Brick1->Left

          Physically, the ball have already passed the bound of the brick, i.e. reversing the speed is not enough: you should update the position accordingly. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          C 1 Reply Last reply
          0
          • CPalliniC CPallini

            Cyclone_S wrote:

            Ball_List[b]->gameBall->Right >= Brick1->Left

            Physically, the ball have already passed the bound of the brick, i.e. reversing the speed is not enough: you should update the position accordingly. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            C Offline
            C Offline
            Cyclone_S
            wrote on last edited by
            #5

            This is as close as I got so far but it makes the ball pause for a moment when it hits the top and bottom of the brick. This code is in the timer event. Any solution to this? Thanks.

            // Check for brick collisions.
            if(left == false && Ball_List[b]->gameBall->Bounds.IntersectsWith(Brick1->Bounds))
            {

            					if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right))
            					{
            						Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel; left = true; top = true;
            
            					}
            
            				}
            				else {left=false;}
            
            				if(top == false && Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds))
            				{
            					
            					if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom))
            					{
            						Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel; top = true;
            					}
            				}
            				else{top=false;}
            
            1 Reply Last reply
            0
            • C Cyclone_S

              Hi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.

              				if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds))
              				{
              					
              					if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right))
              					{
              						Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel;
              
              					}
              
              					if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom))
              					{
              						Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel;
              					}
              				}
              
              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              Recently I've found a very neat blog that explains how to implement some basic physics of a very similar kind as what you are trying to achieve. Check out Physics engine for dummies for some very useful explanations and code that can get you started. The explanations you can find there on how best to implement collision control are very exhaustive. Although the code examples do not feature rectangular bricks, I am sure you can adapt them quite easily by stealing the code from the collision control with the outside border. You just need to make sure that you will always have to check all borders, rather than just 1 at a time. However, for a start it may be easier to just use circular bricks and a circular ball.

              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