Collision 'Cannot translate before resolved' error bug & workaround

In trying to change the position of a display object ‘Wall’ on collision with one called ‘Ball’ by altering its x and/or y value, I kept getting “ERROR: Cannot translate an object before collision is resolved” if I executed the command in either the “ended” or “postCollision” event phases.

So, this causes an error:

local function wallCol (self,e) if e.other.id ~= "ground" then if e.phase == "ended" then wall.x = wall.x + 20 end end end

I also tried changing bodyTypes of the objects on collision, resetting all velocities to 0, etc, but the only thing that works is making the move a function and executing it with a delay:

local function wallCol (self,e) if e.other.id ~= "ground" then if e.phase == "ended" then local function moveWall() print("Move @ "..system.getTimer()) wall.x = wall.x + 20 end timer.performWithDelay(1, moveWall) end end end

works fine. It seems that neither the “ended” nor “postCollision” state means a collision is truly resolved and the object is available for manipulation.

Using system.getTimer() shows that the “ended” and “postCollision” phases happen at the same time, so they apparently denote the same thing. Seems like a bug, unless I’m missing something. [import]uid: 41305 topic_id: 7418 reply_id: 307418[/import]

I am getting the same issue. [import]uid: 8192 topic_id: 7418 reply_id: 28190[/import]

I’m getting the same error. What’s more interesting is that if you try and pass a variable as the amount you want to move an item after a collision, it fails with “ERROR: Cannot translate an object before collision is resolved”. Putting a static number in works with a delay though. For example:

local ball1 = display.newImage("ball1.png")  
ball1.x = 260; ball1.y = 340  
ball1.name = "ball"  
ball1.val = 10  
physics.addBody( ball1, { density=0.9, friction=0.5, bounce=0.2, radius=24 } )  
  
local platform2 = display.newImage("plank1.png")  
platform2.x = 320; platform2.y = 600  
platform2.name = "platform"  
physics.addBody( platform2, "static", { density=0.9, friction=0.5, bounce=0.2 } )  
  
local movePlatform = function( val )  
 print(val)  
 platform2.y = platform2.y - val  
end  
  
local onCollision = function( event )  
 if ( event.phase == "ended" ) then  
 if (event.object1.name == "platform") then  
 timer.performWithDelay( 1, movePlatform(event.object2.val) )  
 end  
 end  
end  
  
Runtime:addEventListener( "collision", onCollision )  
  

This will ERROR out. If I replace

platform2.y = platform2.y - val
with

platform2.y = platform2.y - 10
it works. Very odd. [import]uid: 15615 topic_id: 7418 reply_id: 30621[/import]