Hi,
I am trying to make a game where the player can drag tetris-like shapes on top of a 3x3 grid and not really sure what approach to take when it comes to hit detection and highlighting.
I’ve tried using the physics engine with one small shape per square in the object, but this doesn’t really work as no additional “hits” are triggered before the current hit has ended, so any objects directly underneath won’t trigger a new hit. Bit hard to explain but it should be pretty clear by running the code below.
Any help on how to accomplish this would be super helpful, with or without physics (I’m really only using it to declare the shapes).
-- -- -- Collision test -- local physics = require( "physics" ) physics.start() --physics.setDrawMode("debug") local targetUnit = {} targetUnit[1] = display.newRect(100,100,153,185) targetUnit[2] = display.newRect(253,100,153,185) targetUnit[3] = display.newRect(406,100,153,185) targetUnit[4] = display.newRect(100,285,153,185) targetUnit[5] = display.newRect(253,285,153,185) targetUnit[6] = display.newRect(406,285,153,185) targetUnit[7] = display.newRect(100,470,153,185) targetUnit[8] = display.newRect(253,470,153,185) targetUnit[9] = display.newRect(406,470,153,185) attack = display.newImage('patternattack.png') attack.x = 500 attack.y = 500 attack.objectType = 'attack' attackShape1 = { -155,-94, -153,-94, -153,-92, -155,-92} attackShape2 = { -2,-94, 2,-94, -2,-92, 2,-92} attackShape3 = { 155,-94, 153,-94, 153,-92, 155,-92} attackShape4 = { -2,92, 2,92, -2,94, -2,94} local attackCollisionFilter = { categoryBits = 2, maskBits = 5 } local attackBodyElement1 = { filter=attackCollisionFilter, shape = attackShape1 } local attackBodyElement2 = { filter=attackCollisionFilter, shape = attackShape2 } local attackBodyElement3 = { filter=attackCollisionFilter, shape = attackShape3 } local attackBodyElement4 = { filter=attackCollisionFilter, shape = attackShape4 } local collisionFilter = { categoryBits = 4, maskBits = 2 } local body = { filter=collisionFilter, isSensor=true } physics.addBody ( attack, attackBodyElement1, attackBodyElement2, attackBodyElement3, attackBodyElement4 ) physics.addBody ( targetUnit[1], body ) physics.addBody ( targetUnit[2], body ) physics.addBody ( targetUnit[3], body ) physics.addBody ( targetUnit[4], body ) physics.addBody ( targetUnit[5], body ) physics.addBody ( targetUnit[6], body ) physics.addBody ( targetUnit[7], body ) physics.addBody ( targetUnit[8], body ) physics.addBody ( targetUnit[9], body ) physics.setGravity( 0, 0 ) -- touch listener function function DragHandler( event ) --print (event.phase) if event.phase == "began" then display.getCurrentStage():setFocus( event.target, event.id ) event.target.isFocus = true event.target.alpha = 0.5 event.target:toFront() event.target.markX = event.target.x event.target.markY = event.target.y elseif event.phase == "moved" and event.target.isFocus == true then local x = (event.x - event.xStart) + event.target.markX local y = (event.y - event.yStart) + event.target.markY event.target.x, event.target.y = x, y -- move object based on calculations above end if event.phase == "ended" or event.phase == "cancelled" then --print('ended') end end local function onCollision( event ) print(event) event.object2:setFillColor(255,0,0) if event.phase == "began" then print('hit') event.object2:setFillColor(255,0,0) end if event.phase == "ended" then event.object2:setFillColor(255,255,255) end end --attack:addEventListener( "preCollision" ) attack:addEventListener("touch", DragHandler) Runtime:addEventListener ( "collision", onCollision ) -- -- --
Cheers