Drag and drop mouse not colliding

Hello, I am trying to solve the below problem for two days straight now.
my cursor is not colliding with any game object and I tried to debug.
the message.id and message.group is always nil.

print("Clicked", message.id, message.group, message.x, message.y)
--->Clicked	nil	nil	97.5	297.5

You need to show more of your code and your project. How do you handle drag and drop? Where is that print() used?

here is the controller script


function init(self)
	if not go.get("#cursor", "acquire_input_focus") then
		self.forward_input = true
		msg.post(".", "acquire_input_focus")
	end

	cursor.listen(msg.url("#cursor"), cursor.DRAG_START, function(message_id, message)
		print("drag")
		-- prevent dragging of blue aliens
		if message.group == hash("blue") then
			print("prevent")
			return true
		end
	end)
end

function on_input(self, action_id, action)
	if self.forward_input then
		msg.post("#cursor", "input", { action_id = action_id, action = action })
	end
end

function on_message(self, message_id, message, sender)
	if message_id == cursor.OVER then
		print("Cursor over", message.id, message.group, message.x, message.y)
	elseif message_id == cursor.OUT then
		print("Cursor out", message.id, message.group, message.x, message.y)
	elseif message_id == cursor.PRESSED then
		print("Pressed", message.id, message.group, message.x, message.y)
	elseif message_id == cursor.RELEASED then
		print("Released", message.id, message.group, message.x, message.y)
	elseif message_id == cursor.CLICKED then
		print("Clicked", message.id, message.group, message.x, message.y)
	elseif message_id == cursor.DRAG_START then
		print("Drag started", message.id, message.group, message.x, message.y)
	elseif message_id == cursor.DRAG_END then
		print("Drag ended", message.id, message.group, message.x, message.y)
	elseif message_id == cursor.DRAG then
		print("Drag", message.id, message.group, message.x, message.y, message.dx, message.dy)
	end
end

and here is the card script (attached to the thing i want to drag)

local cursor = require "in.cursor"

local function shake(self)
	go.cancel_animations(".", "scale.x")
	go.cancel_animations(".", "scale.y")
	go.set_scale(self.initial_scale)
	local scale = go.get_scale()
	go.set_scale(scale * 1.2)
	go.animate(".", "scale.x", go.PLAYBACK_ONCE_FORWARD, scale.x, go.EASING_OUTELASTIC, 0.8)
	go.animate(".", "scale.y", go.PLAYBACK_ONCE_FORWARD, scale.y, go.EASING_OUTELASTIC, 0.8, 0, function()
		go.set_scale(self.initial_scale)
	end)
end

function init(self)
	self.initial_scale = go.get_scale()
  -- msg(".","acquire_input_focus")
end

function on_message(self, message_id, message, sender)
	if message_id == cursor.OVER then
		self.over_pos = go.get_position()
		go.set_position(vmath.vector3(self.over_pos.x, self.over_pos.y, 1))
		go.animate("#face_sprite", "tint.w", go.PLAYBACK_ONCE_FORWARD, 1.5, go.EASING_OUTQUAD, 0.1)
	elseif message_id == cursor.OUT then
		local pos = go.get_position()
		go.set_position(vmath.vector3(pos.x, pos.y, self.over_pos.z))
		go.animate("#face_sprite", "tint.w", go.PLAYBACK_ONCE_FORWARD, 1, go.EASING_OUTQUAD, 0.1)
	elseif message_id == cursor.PRESSED or message_id == cursor.RELEASED then
		shake(self)
    print("shake")
	end
end

as you can see i copied the same code as the example you guys provided

And how have you configured the cursor collision objects and the cards? What are their types, group and masks?


This is the card game object I create from a seperate collection using a factory


here is the cursor

cursor over and cursor out do not print on the console as well. I am struggling too much to solve this. please help

Step one is to enable Physics debugging:

This will visualize the collision objects and also show interactions between them.

But what I believe is happening is the fact that the group and mask of the cursor is “default” while the card collision object is of group “card” with mask “cursor”. You need to have a matching and opposite setup for the cursor. The cursor should be of group “cursor” and it should mask against “card”. More info here:


the in game cursor is not in sync with the actual cursor, plus collision is still not being detected after changing the mask and the group of the cursor

Did you change the group/mask of your cursor?

yes

Not in sync how?

Can you share the project? Either here as a zip file or send it to bjorn@defold.se

feel free to pull

The “problem” is that you are using RenderCam but you are not providing a correct translation from mouse position to world position for your cursor. This is mentioned in the cursor documentation:

You need to forward converted coordinates. Disable the “acquire input focus” checkbox of the cursor.script and update your controller.script like this:

local rendercam = require "rendercam.rendercam"

function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	local pos = rendercam.screen_to_world_2d(action.x, action.y)
	action.x = pos.x
	action.y = pos.y
	msg.post("#cursor", "input", { action_id = action_id, action = action })
end
1 Like

okay thanks