collision ended - but throws error on translate

It seems that the end of a collision event.phase does not mean that it is ‘resolved’.

local function onLocalCollision( self, event )  
   
 if ( event.phase == "ended" ) then  
   
 print( "collision ended with " .. event.other.index )   
 -- OUTPUT: collision ended with 2  
  
 event.other.x = 0   
 -- ERROR: Cannot translate an object before collision is resolved.  
   
 end  
end  

So how do you actually know when a collision is resolved ?

I’ve seen posts about using timer.performWithDelay(), but that would just be a guess, wouldn’t it, hoping that the collision would resolve by then ?
And what if the object is involved in another collision before the timer fires? [import]uid: 186251 topic_id: 33239 reply_id: 333239[/import]

That seems weird to me too. But using a timer would definitely work in this case.

As for what to do if you get another collision before the timer ends? Either make the timer very short, or better yet, cancel the timer at the start of the next collision. [import]uid: 135827 topic_id: 33239 reply_id: 132120[/import]

Hello,
A short timer is definitely the correct solution. 10 milliseconds should do the trick, but I tend to use maybe 30-50 just to be sure that the event happens in the next cycle. Basically, that’s what is going on… Box2D can’t perform some methods like translate or reposition until the NEXT cycle following a collision (it needs to complete all of the internal math and calculations).

It’s fairly unlikely that you’ll have two or more collisions completing in the span of 10-50 milliseconds, but if you’re worried about it, you can probably build some kind of “timer stacking” wherein post-collision timers stack and execute in series. I’m not sure of all the logistics behind it, but in theory it could work.

Brent [import]uid: 9747 topic_id: 33239 reply_id: 132138[/import]

Thanks to you both for the info.
So far, a timer works for a small number of objects, <20.
Will see how it holds up around 100 objects. [import]uid: 186251 topic_id: 33239 reply_id: 132181[/import]

That seems weird to me too. But using a timer would definitely work in this case.

As for what to do if you get another collision before the timer ends? Either make the timer very short, or better yet, cancel the timer at the start of the next collision. [import]uid: 135827 topic_id: 33239 reply_id: 132120[/import]

Hello,
A short timer is definitely the correct solution. 10 milliseconds should do the trick, but I tend to use maybe 30-50 just to be sure that the event happens in the next cycle. Basically, that’s what is going on… Box2D can’t perform some methods like translate or reposition until the NEXT cycle following a collision (it needs to complete all of the internal math and calculations).

It’s fairly unlikely that you’ll have two or more collisions completing in the span of 10-50 milliseconds, but if you’re worried about it, you can probably build some kind of “timer stacking” wherein post-collision timers stack and execute in series. I’m not sure of all the logistics behind it, but in theory it could work.

Brent [import]uid: 9747 topic_id: 33239 reply_id: 132138[/import]

Thanks to you both for the info.
So far, a timer works for a small number of objects, <20.
Will see how it holds up around 100 objects. [import]uid: 186251 topic_id: 33239 reply_id: 132181[/import]