if given a space relative to a player and his facing direction how could i get the corresponding spaces in any direction

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BusterDublup

for example lets say i want a space on a tilemap thats two spaces in front of and one space to the left of the player when the player is facing to the left (like the blocks space in the pic below). how can i get the space thats two spaces in front and one to the left as the player turns in different directions (the spaces marked with the x’s) without just listing out all the possible space co ordinance in advance?

Imgur: The magic of the Internet

:bust_in_silhouette: Reply From: klaas

Well … im afraid … Its allways two spaces to the front and one to the left.
What changes is front and left…

i’m not following what you mean. how would i get the coordinance of the space im looking for?

BusterDublup | 2020-08-24 20:02

i dont know what your up to … but this is a solution for your question

var facing = "left" #or use numbers, constant, enums etc

match facing:
        front = Vector2(0,-1)
        back = Vector2(0,+1)
        left = Vector2(-1,0)
        right = Vector2(+1,0)
        front = Vector2(-1,0)
        back = Vector2(+1,0)
        left = Vector2(0,+1)
        right = Vector2(0,-1)
... and so on

do the 2 front one left move

player_pos += front + front + left

klaas | 2020-08-24 20:21

thanks for the reply. i was trying to come up with a generalizable way to get the tiles a player could attack given the players position and the way in with he attacks without having to list the coordinance for each direction but maybe that not possible.
i appreciate your time.

BusterDublup | 2020-08-24 20:57

Sure its possible … i thought its some sort of chess like move.
But your information is not that specific.
What are the rules for an attack?
Distance? orientation? etc.

klaas | 2020-08-24 21:08

different characters would be able to attack different spaces but more or less attacks would all be something like the example i gave, x number of spaces ahead of the character and y number of spaces to the left or right, in one direction at a time but any direction would be possible depending on which way the character is facing at the time.

BusterDublup | 2020-08-24 21:18

yeah … thats exactly whats the code i gave you is good for.

its youst like

#character 1 can attach f,f,l
attack_position = player_pos + front + front + left
#charcter 2 can attack left  left
attack_position = player_pos + left + left

my code just redefines whats front, back left and right

klaas | 2020-08-24 21:22

i think i see what you’re saying now. thank you. thats very helpful.

BusterDublup | 2020-08-24 21:50