Runtime event, collision

Hi,

I’m working on a “Words in a pic” clone but I’m having some problems with the collision detection

In this code a physical body is created for each letter image:

for i = 1, #word do         letters[i] = display.newImage(letterTable[word:sub(i,i)], 30 \* i, 380)         letters[i].letter = word:sub(i,i)         letters[i].name = "letter"         physics.addBody(letters[i], "static")         lettersGroup:insert(letters[i])                  -- Drag event         letters[i]:addEventListener("touch", function(event)                  if event.phase == "began" then                     letters[i].markX = letters[i].x    -- store x location of object                     letters[i].markY = letters[i].y    -- store y location of object                 elseif event.phase == "moved" then                     local x = (event.x - event.xStart) + letters[i].markX                     local y = (event.y - event.yStart) + letters[i].markY                     letters[i].x, letters[i].y = x, y    -- move object based on calculations above                 end                 if event.phase == "ended" then                     print("Ended!")                 end                 return true             end)              end  

In another function I create a body for each image of an empty letter box:

   

 for i = 1, #wordTable[index] do         imageGuessedWord[i] = display.newImage(emptyLetterBoxImage, 30 \* i + (5 \* i), 350)         imageGuessedWord[i].name = "emptyletterbox"         physics.addBody(imageGuessedWord[i], "static")     end  

In the main method I create an event listener:

Runtime:addEventListener(“collision”, onCollision )

The onCollision function should print out some text but doesn’t print out anything.

I want to use a Runtime event so that I can do different stuff depending on where it collides and/or if slot is taken.

“When detected as a Runtime event, each collision event includes event.object1 and event.object2, which contain the table IDs of the two Corona display objects involved.”

Can someone help me explain why the onCollision event isn’t being triggered when a letter box collides with an empty letter box?

Thanks in advance

Best regards,

Tomas

I have now found the answer why the function is not triggered:

  • static bodies don’t move, and don’t interact with each other; examples of static objects would include the ground, or the walls of a pinball machine.

http://developer.coronalabs.com/content/game-edition-physics-bodies

 

Just need to find a way on how to work through this

Hi @tomaswesterlund,

The solution is to just make at least one of the bodies in a collision “dynamic” (the default type). Just one body in a collision needs to be dynamic, not necessarily both. Remember that you can make any body type into a sensor, and you can also turn off gravity on a per-object basis (if for whatever reason you’re using gravity).

Regards,

Brent

I have now found the answer why the function is not triggered:

  • static bodies don’t move, and don’t interact with each other; examples of static objects would include the ground, or the walls of a pinball machine.

http://developer.coronalabs.com/content/game-edition-physics-bodies

 

Just need to find a way on how to work through this

Hi @tomaswesterlund,

The solution is to just make at least one of the bodies in a collision “dynamic” (the default type). Just one body in a collision needs to be dynamic, not necessarily both. Remember that you can make any body type into a sensor, and you can also turn off gravity on a per-object basis (if for whatever reason you’re using gravity).

Regards,

Brent