This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+2 votes

I can't found anything about this in internet. Please Provide some simple example that can I use to test it?

in Engine by (14 points)

This sounds like a tough cookie to crack. Do you mean velocity and force with respect to a certain object, body, or reference frame?

3 Answers

–1 vote

I usually only work with 3d physics but I guess this is the same with 2D in godot.

Transforming a 3d vector from global to local space works via the xform and xform_inv from the Node2D (i.e. a RigidBody2D).

To convert a vector (rotation only) from local to global you would use global_translate.basis_xform(someXY).
That is helpful if you have a local force (i.e. rocket thrust "forward") and want to transform this into a global force vector for apply_force. You can also use global_translate.xform if you want to convert not only the rotation but also the local translation to a global value (that can be tricky though because add_force requires a relative offset in global space not a global position).

To your question:
global_translate.basis_xform_inv(someGlobalDirection) should transform a global vector back into local space.
So you can get i.e. the objects local "forward" velocity from a global velocity vector.

by (3,370 points)
–1 vote

Use Vector2.rotated(rotation). Example:

extends RigidBody2D

var thrust_force = 100

func _physics_process(delta):
  var thrust = Input.is_action_pressed("ui_up") as int * thrust_force
  applied_force = thrust * Vector2.UP.rotated(rotation)
by (332 points)
edited by
0 votes

func addforcelocal(force: Vector2, pos: Vector2):
var pos-local = self.transform.basisxform(pos)
var force-local = self.transform.basis
self.add_force(force-local, pos-local)

by (37 points)
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 Frequently asked questions and 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 [email protected] with your username.