X to Y ratio movement of going in a bearing

I am coding a game where the player's position is stored in co-ordinates, and they can move in any direction by moving their mouse in it (eg. $$129$$ degrees), and I need a way to convert that number to co-ordinates to add.

So

• $$0°$$ would be $$(0, 1)$$, as they are going up $$1$$ and not moving right at all.
• $$45°$$ would be $$(1, 1)$$, as they are moving up $$1$$ and right $$1$$*.
• $$135°$$ would be something like $$(1, -1)$$ as they are moving $$1$$ down and $$1$$ right*.

etc.

*Not moving a full square, as it would be a circular rotation, so it would actually be something like $$0.5$$.

So something like this (which is quite easy in scratch as it is graphical): https://scratch.mit.edu/projects/284120140/

• Time to learn trig! sine and cosine is what you want. As you realized, if you do $(1,1)$ for 45 degree movement then they move faster diagonally than horizontally or vertically. The sine and cosine keep the speed exactly the same in every direction. – Bram28 Feb 3 at 19:43
• Could you just give me a formula for this or preferably some JS code. I'm in seventh grade, and I haven't learnt trigonometry. – Archie Baer Feb 3 at 20:18
• Given angle $\theta$, your $(x,y)$ will be $(sin(\theta), cos(\theta))$. But here is your chance to start with trig! You're motivated to find this out, and your project would be great for you to attach some meaning to these formulas that you'll have to learn soon enough ... – Bram28 Feb 3 at 20:22
• Any way to do this in reverse, @Bram28? – Archie Baer Feb 3 at 22:16
• What do you mean by 'in reverse'? From $(x,y)$ back to angle $\theta$? – Bram28 Feb 3 at 22:20

I can do a copy-and-paste from a previous post:

Combine a polar coordinate system with the line slope formula and the line distance formula.

Or use a land surveying system:

Any y-coordinate that is South of zero is negative. Any x-coordinate that is West of zero is negative. (For screen coordinates, South is positive while North is negative.)

Point A is (y1, x1) . Point B is (y2, x2) .

The direction of A to B is:

InvTan((x2 - x1)/(y2 - y1)) .

If (x2 - x1) is positive that is East else West. If (y2 - y1) is positive that is North else South. This procedure allows a quadrant direction to be determined between any two points. (For screen coordinates, positive is South else North.)

The distance of A to B is:

SquareRoot of ((x2 - x1)^2 + (y2 - y1)^2) .

The combination of direction and distance is a vector but here as an inverse. The coordinates of a point is the location of the point.

Forwarding a point (or setting a point) from A to B is:

y2 = y1 + (Cos(Direction) * Distance)

x2 = x1 + (Sin(Direction) * Distance) .

A North direction is a positive value added to y1 else negative. An East direction is a positive value added to x1 else negative. (For screen coordinates, a South direction is positive else negative.)