X to Y ratio movement of going in a bearing

50 Views Asked by At

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/

1

There are 1 best solutions below

0
On BEST ANSWER

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.)