0 votes

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

1 Answer

+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 (70 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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.