Collision and Triggers

So, I’m making a simple pong game to learn the ins and outs of Defold.
So far I’ve made the ball move, ‘built’ the field and added paddles that respond to user input.

My next challenge was to make the borders collide with the ball and paddles, but I ran in a problem.
Using an example I wrote a piece of code that would check for collision, but regardless if it collided with the borders it didn’t seem to receive a message(I checked the mask and group for both of the objects), but it did get one for a collision with the ball:

function on_message(self, message_id, message, sender)
    --check for collision
    if message_id == hash("contact_point_response") then
    	print(sender)
    end
end

I wanted to then check if the paddle collided with the ball or the border to make it respond only to the border.


The next thing I did was to try and implement the triggers that would count score, and I got a message when the ball hit them, but I didn’t find a way to check which side was hit.

if message_id == hash("trigger_response") then
    	if message.enter then
	    	if sender == hash("[main:/level_pong/border#edge_red]") then
    			print("Score Red!")                  -- <^ change this if you change project structure!!!
    		elseif sender == hash("[main:/level_pong/border#edge_green]") then
    			print("Score Green!")
    		end
    	end
    end

How would I check the sender id properly, and is there a piece of documentation that would explain the attributes of the message’s parameters? (e.g.: message.enter)

I made a Pong game in Defold a while back. The code is here: GitHub - britzl/pong: Defold implementation of the classic game Pong and the game is here: Pong 1.0 I bet you can find some inspiration and solutions to your problems just by looking through the project and code.

Some comments:

Really hard to tell what’s wrong. The most common problem is either that the masks and groups aren’t correct or that the script isn’t attached to the game object in question.

Two different solutions. Assign different groups to the left and right side and check message.group OR check position:

local screen_width = tonumber(sys.get_config("display.width"))
if go.get_position(message.other_id) < screen_width / 2 then
    print("left side")
end

Yes: API reference (physics)

Or you can do pprint(message) to get the table contents formatted into a human readable format. Or iterate over key-value pairs in the message:

for k,v in pairs(message) do
    print(k,v)
end