How To get Relative Position for Transforming?

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

I was trying to make a physics-based block placement system in godot by adding collisionshapes and sprites to a rigidbody. These had an an offset based on the position of the mouse. This works… until they are rotated, and I know why, but not how to fix it. But first, the code:

	var collision = CollisionShape2D.new()
	var shape = RectangleShape2D.new()
	var sprite = Sprite.new()
	var offset = position - ltarget.get_position()
	shape.extents = Vector2(32, 32)
	sprite.set_texture(load("res://sprites/icon.png"))
	collision.shape = shape
	ltarget.add_child(collision)
	collision.set_transform(Transform2D(0, offset))
	collision.add_child(sprite)
	ltarget.mass += 1

When I try to place when the main block is rotated, though, for the transform it takes the global offset between it and the mouse and rotates it with it. How do would I get the normal relative position between them instead of global, so the offset would work correctly? Thanks for any answers!

I’m not entirely sure what behaviour you’re after as position - ltarget.get_position() will give you the relative position. Have you tried using set_global_transform(Transform2D(0, offset)) instead?

Magso | 2020-03-27 22:41

I tried this before and it didn’t work, but after fiddling with it for a while, it works! Thanks so much for reminding me of this.

a human | 2020-03-28 17:17