Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

Help with issues om my simple pong game

This topic is 4680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

1) How do I make a genraic function to just see if the mouse is moving on its y axis and also the x axis? 2) how do I perdict the direction of a ball ? EX: in a pong game? 3) How would I load a verible from a text file? I have an issue were the ball will hit the corner of my paddle and go twice as fast or something like that
int Update_Ball_Pos()
{
   
 BallX += BallVX;
 BallY += BallVY;     
   
 if (BallY >= 599)
 {
         --BallVY ;
         }     
         
else if (BallY <= 1)
{ 
          ++BallVY;
          }
          
else if (BallX  <= 1)
     {
             ++BallVX;
             }          
    
  else if (BallX >= 799)
  {
       --BallVX;
       }
       
  else if ((BallX+60 ) >= PaddleX&& (BallX+60) <= (PaddleX+49)   &&(BallY+57) >= PaddleY&&(BallY+57) <= (PaddleY+196) )
     {
          //BallVY=-BallVY ;
          --BallVX;
          } 
    
 if ((BallX +60) > (PaddleX +49))
 {
            BallX = 400;
            BallY= 300;
            BallVX = 1 ;
            BallVY = 1;
            }

}  
  

Let me know were I can optimze my math and were I went worng Moer questions to come if I dont get them figured out

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by kingpinzs
1) How do I make a genraic function to just see if the mouse is moving on its y axis and also the x axis?

2) how do I perdict the direction of a ball ? EX: in a pong game?

3) How would I load a verible from a text file?

I have an issue were the ball will hit the corner of my paddle and go twice as fast or something like that

*** Source Snippet Removed ***

Let me know were I can optimze my math and were I went worng

Moer questions to come if I dont get them figured out


1) No idea.

2) Direction of the ball is based of ball velocity. Aka, BallVX and BallVY. So, using the standard cartesian coordinates..if BallVX = 1, and BallVY = 1, the ball is heading to the right and up by 1 pixel every time. I dont know if those are directions are right as i havent used DirectX much, but that is the general idea. So future direction on a straight path (aka a pong ball) would just be the velocity of both the X and Y axis.

3) Variable from a file. Look up <fstream> on Google. Use an input file and read it in using the std header library. Enjoy.

about the ball going really fast, instead of just reversing ball velocity (the --BallVX; code in the paddle collision function..reverse it and multiply it by like 0.8

So..

else if ((BallX+60 ) >= PaddleX&& (BallX+60) <= (PaddleX+49) &&(BallY+57) >= PaddleY&&(BallY+57) <= (PaddleY+196) )
{
//BallVY=-BallVY ;
--BallVX * 0.8;
}


Try it and see what happens. Play with the number and see what works best.

Share this post


Link to post
Share on other sites
The direction of a ball in real life is based on the angle which it hits, so if you intend to make this pong game based on real physics then you must figure out at what angle the ball is coming in at and make it bounce off at the same angle in the opposite direction. ie. if you have a grid which is a 4x4 square and the ball is moving up 2 and right 1 it's heading toward teh paddle at approximatly a 22.5 degree angle. In return the ball should leave the paddle at the same angel but reversed so it should then (assuming the paddle is on the y axis at the far right side of the grid) bouce off going up 2 and left 1 which ends up with your 22.5 degree angle and a 45 degree angle in between and resembling a > on the grid if you traced it's path.

Hope i helped.
-Lordoftools
"Knowledge is not only a gift, it's a responsibility."

Share this post


Link to post
Share on other sites
You want to reverse the velocity, not decrease or increase it.

So instead of --BallVX and ++BallVX I would write BallVX = -BallVX, same for Y.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!