Calculate the left and right side based on a direction

Let’s assume I have a position (x|y) on a grid and I want to move to the next cell with the direction (a|b). The next cell would be at the position (x + a|y + b).

But how would I calculate the cells at the left and right side from my current position?

So I would like to know how to calculate the direction vectors based on the direction (a | b)

how do you get back to (x|y), is left a positive or negative ? Is top left (0,0) ?

If your movements are not cardinal, and both A and B are non-zero when moving forward (it isn’t clear that this is the case based on your post, as references to ‘cells’ are usually in the context of grid-based movement), then the easiest way to get the perpendicular directions is by getting the cross product of (A,B) and Vector3.forward/back (assuming an XY oriented grid).

Vector3 direction = Vector3.Cross( AB, Vector3.forward );

Left = (x - a|y + b), Right = (x + a|y - b)

The simplest form of a rotation matrix

1 Like

Thanks guys.

@LethalInjection
The first cell is at (0|0) (bottom left). Top left would be (0| maxY)

@Madgvox
Would you mind explaining it ELI5? That would be awesome

@Boz0r
Does it always work? Let’s say I would move downwards with the direction (0 | -1), the directions would swap, no? Or to the right with (1|0)

Update…

@Boz0r I think you are right

Shouldn’t answer questions late at night or you miss obvious things like this :X

you might want something like this, but you might reverse some of the signs based on where top corner is.

Left = (x - a|y ), Right = (x + a|y ) since y = 0;

up = (x |y + b), down = (x |y - b) since x = 0;