4

I'm a 9th grader in Algebra 1 right now and in my spare time I like programming, I am trying to re-create the game Pong right now but I am having a little problem. I know I am asking a question about programming and that I should be on stackoverflow.com, but I figured I would get better help here.

The problem I am having is making the dot move at a certain angle. Lets pretend that the squares on this bad drawing are pixels on the screen.

N/A

I know that if I wanted it to go at a 45° I would have it go in a path where for every pixel to the right it would go one pixel up (1:1), like this

N/A

And I am pretty sure that a 22.5° angle would go for every two pixels right it goes one pixel up (2:1).

N/A

But what equation would I use to calculate the ratio for other angles?

1 Answers1

1

The ratio you're looking for is $$\frac{\Delta y}{\Delta x} = \tan \theta,$$ where $\theta$ is the angle. To get the angle itself, you'll take the arctanget:

$$\theta = \tan^{-1}\left(\frac{\Delta y}{\Delta x}\right).$$

But, since you're programming, it may be far easier to constrain yourself to ratios of integers, calculate the angle from that ratio, and then associating a bounce-off of that angle for a certain part of the paddle. The actual angle may not matter depending on what API you're using.

But, your two-over, one-up situation gives an angle of

$$\theta = \tan^{-1}\left(\frac{1}{2}\right) \approx 26.56^{\circ}.$$

John
  • 26,319
  • Thank you, but because I have not taken trigonometry yet, I am having a tough time figuring out how to use the number I am getting. I was thinking that either tan(45)^-1 / 2, tan(90)^-1 / 2, or tan(180)^-1 / 2 would give me a 1, but they don't. – user3580232 Oct 24 '14 at 22:40
  • 1
    @user3580232: In most programming languages angles should be given in radians, not in degrees. One degree is $\pi/180$ radians. – Hans Lundmark Oct 24 '14 at 22:46
  • An important thing of note here is that pixels are very very small! So although using only ratios of integers is a very good idea, you can use a pretty large set of integers, since there are a great number of them on the screen. – A. Thomas Yerger Oct 25 '14 at 17:54