InputEventScreenDrag and move_and_slide offset

Godot Version

4.4

Question

When using InputEventScreenDrag you got an index, that you can use for getting the current mouse position.

I want to use this position to drag a CharacterBody2D Node with move_and_slide().

When i do, i got an offset from my mouse position, that i dont want to have. My code:

func _input(event):
  if event is InputEventScreenDrag:
    CharacterBody2D.velocity = event.position * 70
    CharacterBody2D.move_and_slide()

Sounds like an anchor calc problem.

Using local/global coords?

Usually DND needs to specify “where” the anchor of the target object is, top left, etc.

So knowing that, usually you would need to convert the global mouse coord to local of the target, then subtract that from the global coord if dragging in global coordinates. Then the target’s top left corner or wherever, is right under the mouse cursor.

1 Like

I assume its not an anchor issue, if i use get_local_mouse_position() everything works as it should be.

If i use event.screen_relative i get an offset that feels like acceleration.

If i use event.position the Character Node kind of follows the cursor with a delay.

Yeah I guess I am misunderstanding what you are trying to do. I did kind of miss you are setting velocity. I guess an image would help.

Yes im using velocity & move_and_slide() for moving, to be able to use the physics engine. What you need a picture of?

h I am just having a hard time visualizing what you are doing, so a simple interaction shot or something with arrows, I just can’t visualize what you are actually trying to do. :joy:

simple drag and drop, you press on the charakter node and it should stick right on the current mouse position without offsets

Well ok, that is what I kind of thought you were doing.

I need to try this myself, but I can’t see how you could move it without an offset unless you are doing everything in local coordinates.

But the velocity thing is messing me up and why I would need to just try and do a simple test. I was speaking about x,y coord movement, I guess I just don’t get how the x,y mouse coords translate into the movement, since they are position coords, not per second movement.

I dont know may be its not the right way to do it but how would you accomplish it if you need to include event.position but cant set CharacterBody`s position directly?

When moving a CharacterBody2D, you should not set its position property directly. Instead, you use the move_and_collide() or move_and_slide() methods. These methods move the body along a given vector and detect collisions.

but cant set CharacterBody`s position directly?

Well I quick thought would be, you know where the mouse went down, that is a Vector2, x, y position. Then if you are dragging and mouse up you have another Vector2 and can get the distance between as a motion vector.

But… how to solve moving a CharacterBody without velocity as you drag is another problem. :grin:

I kind of get what you were trying to do now, makes sense. I am a more 3D guy so learning all this 2d stuff now, so I don’t really know.

Abstractly I would say if you can turn off velocity.

Set disabled-mode

Defines the behavior in physics when Node.process_mode is set to Node.PROCESS_MODE_DISABLED. See DisableMode for more details about the different modes.

DisableMode DISABLE_MODE_MAKE_STATIC = 1

When Node.process_mode is set to Node.PROCESS_MODE_DISABLED, make the body static. Doesn’t affect Area2D. PhysicsBody2D can’t be affected by forces or other bodies while static.

Automatically set PhysicsBody2D back to its original mode when the Node is processed again.

Shot in the dark but maybe setting disabled-mode on mouse down, use Node2D move stuff, set position for the actual dragging of mouse down, then re-enabled the collision body which will then use velocity again.

I don’t know if this will work, but I would try it if I had this problem to solve. :laughing:

Good idea but actually i need velocity for further calculations like collision speed etc.

It is already working with get_local_mouse_position(), i just can’t figure out whats wrong with event position yet.