Jump to content
  • Advertisement
Sign in to follow this  
Drakkcon

Azimuth of ball's position in pong

This topic is 5044 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

Well, I'm trying to write my first game ever. Pong. I'm completely stumped right now, because I have no idea how to set the ball's azimuth with degrees. I'm only 14, so the extent of my math knowledge is trigonometry, and I have a feeling this will involve right triangles. Here's my code, I'm using SDL.
#include <iostream>
#include <SDL/SDL.h>

const int SCREEN_HEIGHT = 480;
const int SCREEN_WIDTH = 640;

class Paddle
{
  SDL_Surface *Image; //The paddle's image
  SDL_Surface *Field;

  int RateOfMovement; //pixels per second
  int CurrentPosX;
  int CurrentPosY;
  
  public:
    SDL_Rect BoundingBox; //for collision detection
    
    Paddle()
    {
      CurrentPosX = 1;
      CurrentPosY = 0;
      RateOfMovement = 1;
      Image = NULL;
      
      BoundingBox.x = 1;
      BoundingBox.y = 1;
      BoundingBox.w = 1;
      BoundingBox.h = 1;
    }
      
    void SetImage(char *BitmapToLoad)
    {
      Image = SDL_LoadBMP(BitmapToLoad);
    }
      
    void SetPosY(int PosY)
    {
      CurrentPosY = PosY;  
    }
        
    void SetRateOfMovement(int Rate)
    {
      RateOfMovement = Rate;
    }
      
    void MoveDown()
    {
      CurrentPosX += RateOfMovement;
    }
      
    void MoveUp()
    {
      CurrentPosX -= RateOfMovement;
    }
      
    void DrawWithCollision()
    {
      SDL_Rect BlitRect;
      BlitRect.x = CurrentPosX;
      BlitRect.y = CurrentPosY;
      
      BoundingBox.x = x;
      BoundingBox.y = y;
      BoundingBox.w = image.w;
      BoundingBox.h = image.h;
      
      SDL_BlitSurface(Image, NULL, surface, Field);
    }
    
    void FreeImage()
    {
      SDL_FreeSurface(image);
    }  
    
};

class Ball
{
  int speed;
  int azimuth;
  int xPos;
  int yPos;
  
  public:
    void SetSpeed(int Speed)
    {
      speed = Speed;
    }
        
    void SetAzimuth(int Direction)
    {
      azimuth = Direction;
    }
    
    void CalculatePosition()
    bool IsCollided();


As you can see, I'm not nearly done yet. Can anyone help? PS: Kind of pathetic to be stumped by pong....

Share this post


Link to post
Share on other sites
Advertisement
I guess my question would be why you would care what the azimuth is, if you mean the highest point the ball goes. Basically, your ball movement is a standard Newtonian physics problem. That means that every tick, you're going to want to subtract some sort of "gravity" acceleration from the y component of the ball's velocity vector. But, I haven't seen that many pong games, especially first drafts, that care about gravity.

As far as your code looks, you want to find initial angle apparently when you're talking about azimuth. At its simplest, angle of rebound is equal to the angle of approach in the opposite direction. So, think of your paddle as the bottom of a protractor. If your ball comes in at 90 degrees, it will leave at 180 - 90 = 90 degrees. If your ball comes in at 80 degrees, it will leave at 180 - 80 = 100 degrees.

If you've gotten this far, from there you use simple geometry to determine what that resolves as in x and y pixels. First, figure out the angle compared to a line parallel to the bottom of the screen passing through the point the ball bounced off of, in order to get into the right reference frame. Since your paddle is perpendicular, you should just need to subtract or add 90 degrees depending on which paddle you're looking at. Your velocity should be in pixels/tick, which makes that also your hypotenuse of your triangle. So, your actual movement will be:

xCurrent = xPrevious + sine (angle) * velocity
yCurrent = yPrevious + cosine (angle) * velocity

Note that negative numbers in this are just fine.

Hope that makes sense, doing it quickly off the cuff so onlookers feel free to chime in with corrections or addendums.

-fel

Share this post


Link to post
Share on other sites
I thought that azimuth just meant direction in degrees ( I sound like an idiot now ), however the way you find the direction of the puck is what I'm looking for. I should be able to figure it out from here, but I'll ask if I can't. Thanks!

Share this post


Link to post
Share on other sites
Well, it kind of does, but it brings in an observer and is usually used in a navigational sense. I was just confused because it's really specific in the GPS world, which is what I do for a living.

-fel

Share this post


Link to post
Share on other sites
yeah drakkcon i know what you mean! i had the same problem but i worked it out from this website - read this!

Apparantly: moving a ball with an X and Y per go is called Cartesian coordinates. The alternative, which you want, is called Polar coordinates. This is where you store the angle and speed instead of X and Y per go.

You can convert between polar and cartesian co-ordinates using Sin() and Cos(). Cos is for X and Sin is for Y! its simple!

ballX += speed_length * cos (angle);
ballY += speed_length * sin (angle);

So you can use this to find what x and y movement is needed for any angle! It works i have tried it!

<marquee height=120 scrollamount=1 behavior=alternate direction=up><marquee width=200 behavior=alternate direction=right>
Pong!</marquee></marquee>

Neil

Share this post


Link to post
Share on other sites
Thanks for the link Nightwalk! Perfect!
Thanks to Felisandria too, her initial explanation is probably the way Nightwalk's link explaines it, but it was a bit too much for me to get.

Nightwalk.rating++;
Felisandria.rating++;

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!