Here I was testing collision on an object vs collision (or lack thereof) on a group. It seems the group works, but it doesn’t…kind of.
Yes, It’s vague but that’s exactly how it acts. The group will transition, but the testObject removes itself at different positions. Sometimes you drag the white square (player) towards the red square and it sometimes removes, sometimes it removes a long ways away. It’s always like it’s removing it from where it WAS at not where it IS. Confused yet?
Let me break it down to these simple questions:
-
Is it possible to transition a group, and have proper collision detection?
-
Is it possible to translate a group, and have proper collision detection?
If so how?
This code runs out of the box. Follow along, run it as is, then comment out step one, and uncomment step 2 to demonstrate what I’m talking about. I’d really like to translate the group for a few reasons. Transitions, seem they slow down as they get closer to the ending point vs translations which if you have in a runtime listener, they are consistent (but have issues if framerate drops, another discussion entirely)
local physics = require("physics") physics.start(true) physics.setDrawMode("normal") physics.setContinuous( true ) --Not sure if this will be needed, but for now here we are physics.setGravity(0, 0) physics.setPositionIterations( 16 ) -------------------------------------------- -- forward declarations local enemyGroup = display.newGroup() local playerGroup = display.newGroup() --Set up player local player = display.newRect(100,100, 50, 50) physics.addBody(player) ;player.isSleepingAllowed = false player.type = "player" playerGroup:insert(player) local testObject = display.newRect(600,100,40,40) testObject:setFillColor(255,0,0) testObject.type = "testObject" physics.addBody( testObject, "kinematic", { isSensor = true } ) enemyGroup:insert(testObject) testObject.alpha = 1 --Notice that if you did this, the "collision" removes testObject before it --even hits the player, or it delays then removes. Or it removes somewhere else OTHER --than where the player is....etc. --Uncomment this, and test with transition.to(testObject --enemyGroup.x = \_W/2 --\*\*\*\*\*\*\*\*\*\*\*\* --STEP ONE: run this - see how it works FINE. --\*\*\*\*\*\*\*\*\*\*\*\* --Transition object on it's own WORKS FINE. transition.to(testObject, {time = 9000, x = -6000 }) --\*\*\*\*\*\*\*\*\*\*\*\* --STEP TWO: --\*\*\*\*\*\*\*\*\*\*\*\* --Comment out the above transition, uncomment this one. --This is an object in a group. But seems to not exist to collision handler. --\*\*\*\*\*\*\*\*\*\*\*\* --Transition group, it doesn't work. It's strange. --transition.to(enemyGroup, {time = 2000, x = -1400 }) function player:touch( event ) if event.phase == "began" then self.markX = self.x --When fast dragging, the coordinate tracking helps keep accuracy. self.markY = self.y --When fast dragging, the coordinate tracking helps keep accuracy. elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX ---comment out to restrict to up and down on Y local y = (event.y - event.yStart) + self.markY self.x = x ---comment out to restrict to up and down on Y self.y = y end return true end Runtime:addEventListener( "touch", player ) --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then print ("onCollision phase began") local agro = event.object1 local hit = event.object2 if agro.type == "player" and hit.type == "testObject" then display.remove(hit) hit = nil print ("onCollision : testObject removed") --Runtime:removeEventListener("enterFrame", moveGridObjects) print ("onCollision remove object") end end end Runtime:addEventListener("collision", onCollision)