Find both cathetus values given only tan (angle) and hypothenuse

56 Views Asked by At

I am developing a game and came to a problem that couldn't solve yet. In a given scenario, an opponent shoots at a target in a 2D environment. At the beginning, all that I got is only the position of the enemy (say, (Ex, Ey)) and of the player (Px, Py). With this I can find the tangent of the angle that the line forms with the X axis (let's say, alpha angle). This 'bullet' moves in the screen by 40 pixels per frame, let's say... In the game logic, I have to update the bullet's X and Y (Bx, By) at each frame in a given value based only on those points and angle. In the picture, the position of the bullet at each frame is in Red. If I got the alpha angle between the hyphothenusa and the X axis would be very easy to find the Delta X, Delta Y I have to add at each frame to (Bx, By). But I couldn't. How to solve this problem? Thanks in advance.

Notice: the Y axis in the computer screens grows in the opposite direction

enter image description here

1

There are 1 best solutions below

0
On

Problem solved, I just forgot that there is this thing called ArcTangent, with it I found the alpha angle and therefore the delta offset:

Delta X = 40 * cosine(arctan(Py-Ey/Px-Ex))
Delta Y = 40 * sine(arctan(Py-Ey/Px-Ex))

Then just added Delta X to Bx and Delta Y to By at each frame and It did "the trick"

I'm also accepting other suggestions (if there is) to solve it, maybe doing less calculus than this. Thanks!