how to determine position of player regarding to an 3d node?

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

I am making a doom-like first person shooter in 3d world. I am showing enemies as a 2d sprite animated with spritesheet. So i need to determine where the player is regarding to the object.

So if player if front of the object, i will show the front facing sprite animations.
If player is on the right of the object, i will show the right spirtes.
etc

I have followed a video where it is implemented with the camera like this:

var player_forward_direction = -camera.global_transform.basis.z
var npc_forward_direction = global_transform.basis.z
var npc_left_direction = global_transform.basis.x

var left_dot = npc_left_direction.dot(player_forward_direction)
var forward_dot = npc_forward_direction.dot(player_forward_direction)

But the problem here is that if i stand in one place and rotate the camera, this code is impacted. I need to implement this based only on player position, not on camera rotation.

Please help i am total noob in 3d math.

:bust_in_silhouette: Reply From: jzzzzz

try this code:

var enemy_translation=enemy.translation#replace "enemy" with enemy the node
var player_translation=player.translation#replace "player" with player the node
var d #direction to player


var dx
var dz
if enemy_translation[0]<player_translation[0]:
	dx="right"
	#enemy is to the right of the player
if enemy_translation[0]>player_translation[0]:
	dx="left"
	#enemy is to the left of the player

if enemy_translation[2]>player_translation[2]:
	dz="front"
	#enemy is in front of the player
if enemy_translation[2]<player_translation[2]:
	dz="back"
	#enemy is behind the player

if abs(enemy_translation[0]-player_translation[0])>abs(enemy_translation[2]-player_translation[2]):
	d=dx
else:
	d=dz

if d=="left":
	pass #put the correct sprite
if d=="right":
	pass #put the correct sprite
if d=="front":
	pass #put the correct sprite
if d=="back":
	pass #put the correct sprite

I hope that works, I can’t test it very well.
if something is backwards that flip the relative “>” and “<” symbols.
if it doesn’t work, I can try to help you get it working, but I am new pretty new to this stuff too. :slight_smile: