How to get position of an Area or RigidBody node?

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

I’m trying to apply a physics force on an Area or RigidBody node, call it xi. To do so I have to get the position of other objects xj in its surrounding, since the force exerted on xi by xj depends on the relative position posij = xi - xj.

How do I get the position of an Area node or a RigidBody node through GDScript?

:bust_in_silhouette: Reply From: abdolilah

if the script is in xj then do:

var xj_pos= self.rect_position

if that did not work then do:

var xj_pos=self.position

if the script is in the parent of xj then do:

var xj_pos= $xj.rect_position

or:

var xj_pos= $xj.position

Thanks for the reply =). The script is intended to be in xi, where the positions of xi and every xj need to be accessed.

Neither of the suggestions work for me. The following being attached to a RigidBody node

extends RigidBody

func update_force():
    var xi_pos_1 = self.position             # <-----
    var xi_pos_2 = self.rect_position        # <-----

func _physics_process(delta):
    update_force()

gives me the errors:

Invalid get index 'position' (on base: 'RigidBody (SpaceCell.gd)').
Invalid get index 'rect_position' (on base: 'RigidBody (SpaceCell.gd)').

toblin | 2020-03-06 15:22

witch godot version do you use

abdolilah | 2020-03-06 15:38

ah i get it
you are working in 3D
then do :

var ix_pos = self.translation

and you have to notice some thing hire :
if you do

print("ix_pos is: ", ix_pos)

then the output in the Debugger is going to be

ix_pos is: (0, 0, 0)

ok the value depend on the position of ix
if you want to access the x or y or z position you have to do

var ix_x_pos = self.translation.x

or

 var ix_y_pos = self.translation.y

or

 var ix_z_pos = self.translation.z

and have a nice day

abdolilah | 2020-03-06 15:55