Rigedbody2d + mouse drag and drop not working properly

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

Hello all!

I’m having a bit problem while dealing with RigidBody and collision.

What I’m doing is instancing some nodes while clicking a button, a new node is instanced in a different position.

Those nodes need to be able to move by drag and drop on mouses, and initially, I did with Area2d nodes and it was working fine, however, I need it to handle collisions and I used the same principle for moving it.

The problem is that when I move using a rigid body, the movement appears to be limited to the rigid body area itself.

Trying to change the rigid body to a character also has the same effect, however, it resets to the initial position.

Note that in the background, there is a Are2d, however, to avoid issues, I also changed the layer/mask, however, no luck.

Here is the code I’m using to move them.

  var id
var MouseIn = false
signal MouseIn
signal MouseOut
func _ready():
	set_process(true)
	pass
func add_peca(id):
	print (id)
	pass
	
func _process(delta):
	if(MouseIn == true && Input.is_action_pressed("click")):
		set_position(get_viewport().get_mouse_position())
	pass
func get_MouseIn():
	return MouseIn
func _on_mouse_entered():
	MouseIn = true
	emit_signal("MouseIn")
	pass 
func _on_mouse_exited():
	MouseIn = false
	emit_signal("MouseOut")
	pass 

Do you know what i’m doing wrong? It’s the movement script itself or there is any property i’m not aware that cause it?

:bust_in_silhouette: Reply From: kidscancode

A rigid body cannot be moved by changing its position, because the physics engine has control of it. If you need to move it manually, you must either directly alter its physics state in _integrate_forces() or set it to MODE_STATIC so that it’s removed from physics processing. The latter is the better solution for drag-and-drop.

Here’s an example of this that I put together after getting similar questions often:
http://kidscancode.org/godot_recipes/physics/rigidbody_drag_drop/

Aside from that, there are some other things in this code that are wrong or are going to cause you trouble:

  • MouseIn is used as the name of both a signal and a variable (although it doesn’t look like that signal is used)
  • pass is extraneous. It does nothing.
  • set_process(true) has not been required since Godot 2.x
  • Rigid bodies (all CollisionObject2D nodes) can detect input events, so it’s not necessary to separately detect mouse in and click events. See _input_event signal.

Recommended reading:

Hello kidsscancode,

thanks a lot for the insight, this helped a lot!

didn’t know of the set_process(true) and pass thing, and you code helped me a lot to understand.

Just one thing to leave registered, the MouseIn variable actually have a diferent name on my code, i really didn’t noticed i used the same name from the signal when i posted here.

By the way, i love your tutorial videos!

Agre | 2019-06-13 22:06