Turning an angle into a ratio of squares left/right:squares up/down

173 Views Asked by At

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

There are 1 best solutions below

3
On

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}.$$