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. Angles

Angles

Scheduled Pinned Locked Moved C / C++ / MFC
game-devperformancetutorial
10 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

    I got two questions. First I'm making a brick game. I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle. If the angle variable is specified directly it bounces off in the correct angle but I'm not sure how to calculate the angle depending on where the ball hits the paddle.

    double a = 135;
    double angle = (Math::PI / 180) * a;
    Ball_List[b]->xVel = speed*Math::Cos(angle);
    Ball_List[b]->yVel = -speed*Math::Sin(angle);

    Second. I want to animate a box going in a circle but I'm Not sure on the math. Thanks in advance.

    L S 2 Replies Last reply
    0
    • C Cyclone_S

      I got two questions. First I'm making a brick game. I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle. If the angle variable is specified directly it bounces off in the correct angle but I'm not sure how to calculate the angle depending on where the ball hits the paddle.

      double a = 135;
      double angle = (Math::PI / 180) * a;
      Ball_List[b]->xVel = speed*Math::Cos(angle);
      Ball_List[b]->yVel = -speed*Math::Sin(angle);

      Second. I want to animate a box going in a circle but I'm Not sure on the math. Thanks in advance.

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

      Cyclone_S wrote:

      I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle

      That is pretty vague, and probably not according to normal physics. Maybe what you want is: the outgoing angle equals the supplement of the incoming angle plus some delta, which is zero in the center and grows when the hit point is away from the center; so maybe calculate that distance and use it to add to or multiply the outgoing angle.

      Cyclone_S wrote:

      going in a circle

      the equations for a circle in two dimensional space are:

      (x - xc)^2 + (y - yc)^2 = r^2

      or

      x = xc + r * cos(a)
      y = yc + r * sin(a)

      where (x,y) is a point on the circle, (xc,yc) is the center, r the radius, a an angle in radians. I cannot believe you would not know that. Look at the equations, they say the point(x,y) is at a fixed distance r from a fixed point (xc,yc). :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
      Please use <PRE> tags for code snippets, they improve readability.
      CP Vanity has been updated to V2.3

      modified on Tuesday, May 31, 2011 9:17 PM

      D 1 Reply Last reply
      0
      • L Luc Pattyn

        Cyclone_S wrote:

        I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle

        That is pretty vague, and probably not according to normal physics. Maybe what you want is: the outgoing angle equals the supplement of the incoming angle plus some delta, which is zero in the center and grows when the hit point is away from the center; so maybe calculate that distance and use it to add to or multiply the outgoing angle.

        Cyclone_S wrote:

        going in a circle

        the equations for a circle in two dimensional space are:

        (x - xc)^2 + (y - yc)^2 = r^2

        or

        x = xc + r * cos(a)
        y = yc + r * sin(a)

        where (x,y) is a point on the circle, (xc,yc) is the center, r the radius, a an angle in radians. I cannot believe you would not know that. Look at the equations, they say the point(x,y) is at a fixed distance r from a fixed point (xc,yc). :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
        Please use <PRE> tags for code snippets, they improve readability.
        CP Vanity has been updated to V2.3

        modified on Tuesday, May 31, 2011 9:17 PM

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Luc Pattyn wrote:

        I cannot believe you would not know that.

        Just out of curiosity, why would you assume he would know?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

        L 1 Reply Last reply
        0
        • D David Crow

          Luc Pattyn wrote:

          I cannot believe you would not know that.

          Just out of curiosity, why would you assume he would know?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

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

          Assuming he is adolescent or older, he would have learned this and much more at school, both as formula's for describing a circle, and as a geometric illustration for explaining what a sine and cosine actually are. He already was using angles, sine and cosine, in his post, so it puzzles me how he would not come up with the equations if he had ever seen and understood them. And then there are books, and Google, and Wolfram, etc. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
          Please use <PRE> tags for code snippets, they improve readability.
          CP Vanity has been updated to V2.3

          C 1 Reply Last reply
          0
          • C Cyclone_S

            I got two questions. First I'm making a brick game. I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle. If the angle variable is specified directly it bounces off in the correct angle but I'm not sure how to calculate the angle depending on where the ball hits the paddle.

            double a = 135;
            double angle = (Math::PI / 180) * a;
            Ball_List[b]->xVel = speed*Math::Cos(angle);
            Ball_List[b]->yVel = -speed*Math::Sin(angle);

            Second. I want to animate a box going in a circle but I'm Not sure on the math. Thanks in advance.

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #5

            You might want to check out this site[Physics engines for dummies] for an easy introduction into the basics of vector algebra used to emulate quasi-physical simulations. You will find that in stead of angles and sinus/cosinus it explains the calculations needed for reflections with the help of vector algebra. Although the code is not C/C++ it should be easy enough to translate the relevant code for your problems.

            1 Reply Last reply
            0
            • L Luc Pattyn

              Assuming he is adolescent or older, he would have learned this and much more at school, both as formula's for describing a circle, and as a geometric illustration for explaining what a sine and cosine actually are. He already was using angles, sine and cosine, in his post, so it puzzles me how he would not come up with the equations if he had ever seen and understood them. And then there are books, and Google, and Wolfram, etc. :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
              Please use <PRE> tags for code snippets, they improve readability.
              CP Vanity has been updated to V2.3

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

              Thanks for the replies. Both problems are mostly solved. I have the paddle/ball equation figured out but I'm having a problem where any value less then 1 is ignored... any ideas? I need finer precision. Thanks.

              L 1 Reply Last reply
              0
              • C Cyclone_S

                Thanks for the replies. Both problems are mostly solved. I have the paddle/ball equation figured out but I'm having a problem where any value less then 1 is ignored... any ideas? I need finer precision. Thanks.

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

                Cyclone_S wrote:

                I need finer precision

                then scale it all up in integers; or use floating-point arithmetic throughout. :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                Please use <PRE> tags for code snippets, they improve readability.
                CP Vanity has been updated to V2.3

                C 1 Reply Last reply
                0
                • L Luc Pattyn

                  Cyclone_S wrote:

                  I need finer precision

                  then scale it all up in integers; or use floating-point arithmetic throughout. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                  Please use <PRE> tags for code snippets, they improve readability.
                  CP Vanity has been updated to V2.3

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

                  I am using doubles in the function. The problem is I can't move the panel(ball) anything less then 1 pixel. Maybe move the ball 1pixel over a couple of frames? I'm not sure how to do that. Here is the code.

                  						int speed=Ball\_List\[b\]->speed;
                  						// Paddle center.
                  						int paddle\_center = Player->Location.X + Player->Width / 2;
                  
                  						// Ball center.
                  						int ball\_center = Ball\_List\[b\]->gameBall->Location.X + Ball\_List\[b\]->gameBall->Width / 2;
                  
                  						// Find the location on the paddle that the ball hit
                  						int paddle\_location = ball\_center - paddle\_center;
                  
                  						// Increase X speed according to distance from center of paddle.
                  						double a=(90-paddle\_location);
                  						double angle = (Math::PI / 180) \* a; // angle in radians.
                  						Ball\_List\[b\]->xVel = speed\*Math::Cos(angle);
                  						Ball\_List\[b\]->yVel = -speed\*Math::Sin(angle);
                  
                  						Ball\_List\[b\]->gameBall->Top = Player->Location.Y - 16;
                  

                  Thanks

                  L 1 Reply Last reply
                  0
                  • C Cyclone_S

                    I am using doubles in the function. The problem is I can't move the panel(ball) anything less then 1 pixel. Maybe move the ball 1pixel over a couple of frames? I'm not sure how to do that. Here is the code.

                    						int speed=Ball\_List\[b\]->speed;
                    						// Paddle center.
                    						int paddle\_center = Player->Location.X + Player->Width / 2;
                    
                    						// Ball center.
                    						int ball\_center = Ball\_List\[b\]->gameBall->Location.X + Ball\_List\[b\]->gameBall->Width / 2;
                    
                    						// Find the location on the paddle that the ball hit
                    						int paddle\_location = ball\_center - paddle\_center;
                    
                    						// Increase X speed according to distance from center of paddle.
                    						double a=(90-paddle\_location);
                    						double angle = (Math::PI / 180) \* a; // angle in radians.
                    						Ball\_List\[b\]->xVel = speed\*Math::Cos(angle);
                    						Ball\_List\[b\]->yVel = -speed\*Math::Sin(angle);
                    
                    						Ball\_List\[b\]->gameBall->Top = Player->Location.Y - 16;
                    

                    Thanks

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

                    All of that could have been floats or doubles; it is only when painting that it eventually needs to be rounded to pixels, hence integers. :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                    Please use <PRE> tags for code snippets, they improve readability.
                    CP Vanity has been updated to V2.4

                    C 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      All of that could have been floats or doubles; it is only when painting that it eventually needs to be rounded to pixels, hence integers. :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                      Please use <PRE> tags for code snippets, they improve readability.
                      CP Vanity has been updated to V2.4

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

                      so there's no solution then?

                      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