Physics: How to avoid getting a 'buffered' collision callback after re-starting Physics???

Background:
* have tracked down an annoying problem I’ve had
* in this game I pause physics, then later I reposition my player object & restart (start) physics again
* what I see is a collision event firing that I wasn’t expecting, as the player isn’t near an object after I reposition it
* so I’m guessing that it is the carry-over of some previous collision that occurred

QUESTION: How can I avoid getting a “buffered”/“superfluous” collision event happening here, directly after starting the physics engine???

thanks [import]uid: 140210 topic_id: 31079 reply_id: 331079[/import]

Hi Greg,
This seems odd. When are you pausing the physics exactly? Can you determine specifically what collision event is happening, and with what it’s colliding? Do you have some kind of sensor object that might be queuing a collision?

Brent [import]uid: 9747 topic_id: 31079 reply_id: 124330[/import]

Hi Brent - The player object here is actually a sensor. I made it like this so it can pass through other objects and detect the collisions (e.g. wind etc). I put logging in and the superfluous collision is from an object where it was prior to resetting and repositioning the player, so it does seem like it was a buffered one.

Does a “sensor” change the way collisions get buffered then? Basically I was after a way to reset/reposition the player, but then also clear out any collision events so that when I turn physics back on I wouldn’t get an “old” buffered collision event appearing. [import]uid: 140210 topic_id: 31079 reply_id: 124339[/import]

Hi Greg, do you pause the physics immediately when the collision occurs? If so, this might be the issue. As a test, try to delay your pause by just 1 game cycle by doing a short timer (50 milliseconds or something) and see if the problem still occurs.
[import]uid: 9747 topic_id: 31079 reply_id: 124376[/import]

Hi Brent - looking back through the code this is what I’m doing

[code]
Collision Event

  • detect and put flag for next game cycle

Game Loop

  • physics.setGravity(0, 9.8)
  • physics.removeBody( player )

Later When User Clicks Menu to get overlay and then choose RESET

  • physics.pause()
  • physics.setGravity(0, 0)
  • physics.addBody( player )
  • player.isSensor = true
  • performWithDelay: physics.start()

==> Then later after this pick up stray collision event from prior
[/code] [import]uid: 140210 topic_id: 31079 reply_id: 124447[/import]

Hi Greg,
I might also suggest not removing and re-adding the physics body, unless you have a very specific reason for doing so. You could probably just make the body inactive during the reset. I don’t know why re-adding a body would trigger another collision (or buffer it), but try avoiding that entire process and see if anything changes.

Best regards,
Brent
[import]uid: 9747 topic_id: 31079 reply_id: 124577[/import]

thanks Brent - I’ve actually decided to change the approach I had to avoid the whole issue - I was actually repositioning the player then using collisions (begin/end) to get it to run along the top of an object - this way irrespective of the level & object in that level the player would automatically be walking along on the top - I’ll just manually add in the line for the player to follow each level

I’m still actually doing the removeBody however - I have the scenario player hits something => swap it out for a ragdoll set of objects whilst it falls tumbles. So at this point I’ve been making the normal player invisible & doing a removeBody on it. Then after the reset, reposition it and make it visible & add the physics body back. Not sure if this is the standard approach or not here… [import]uid: 140210 topic_id: 31079 reply_id: 124625[/import]

Hi Greg,
This seems odd. When are you pausing the physics exactly? Can you determine specifically what collision event is happening, and with what it’s colliding? Do you have some kind of sensor object that might be queuing a collision?

Brent [import]uid: 9747 topic_id: 31079 reply_id: 124330[/import]

Hi Brent - The player object here is actually a sensor. I made it like this so it can pass through other objects and detect the collisions (e.g. wind etc). I put logging in and the superfluous collision is from an object where it was prior to resetting and repositioning the player, so it does seem like it was a buffered one.

Does a “sensor” change the way collisions get buffered then? Basically I was after a way to reset/reposition the player, but then also clear out any collision events so that when I turn physics back on I wouldn’t get an “old” buffered collision event appearing. [import]uid: 140210 topic_id: 31079 reply_id: 124339[/import]

Hi Greg, do you pause the physics immediately when the collision occurs? If so, this might be the issue. As a test, try to delay your pause by just 1 game cycle by doing a short timer (50 milliseconds or something) and see if the problem still occurs.
[import]uid: 9747 topic_id: 31079 reply_id: 124376[/import]

Hi Brent - looking back through the code this is what I’m doing

[code]
Collision Event

  • detect and put flag for next game cycle

Game Loop

  • physics.setGravity(0, 9.8)
  • physics.removeBody( player )

Later When User Clicks Menu to get overlay and then choose RESET

  • physics.pause()
  • physics.setGravity(0, 0)
  • physics.addBody( player )
  • player.isSensor = true
  • performWithDelay: physics.start()

==> Then later after this pick up stray collision event from prior
[/code] [import]uid: 140210 topic_id: 31079 reply_id: 124447[/import]

Hi Greg,
I might also suggest not removing and re-adding the physics body, unless you have a very specific reason for doing so. You could probably just make the body inactive during the reset. I don’t know why re-adding a body would trigger another collision (or buffer it), but try avoiding that entire process and see if anything changes.

Best regards,
Brent
[import]uid: 9747 topic_id: 31079 reply_id: 124577[/import]

thanks Brent - I’ve actually decided to change the approach I had to avoid the whole issue - I was actually repositioning the player then using collisions (begin/end) to get it to run along the top of an object - this way irrespective of the level & object in that level the player would automatically be walking along on the top - I’ll just manually add in the line for the player to follow each level

I’m still actually doing the removeBody however - I have the scenario player hits something => swap it out for a ragdoll set of objects whilst it falls tumbles. So at this point I’ve been making the normal player invisible & doing a removeBody on it. Then after the reset, reposition it and make it visible & add the physics body back. Not sure if this is the standard approach or not here… [import]uid: 140210 topic_id: 31079 reply_id: 124625[/import]