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?

in Engine

+1 vote

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
by (71 points)

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

witch godot version do you use

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