Body.blocked not behaving as expected


#1

As you can see I have two flying bodies - however only one gets cleared. If I set them to travel left, none get cleared. They are colliding against tiles so I am using blocked instead of touching, but what else am I missing?

this.bats.children.iterate((child) => {
  child.body.setAllowGravity(false);
  child.setActive(true);
  child.setCollideWorldBounds(true); 
  let rightLoop,
      leftLoop;
  if(!child.flying){
    console.log();
    child.flying = true;
    console.log('flying');
    const firstLoop = setInterval(()=>{
      console.log('loop');
      child.setVelocityX(160)
      if(child.body.blocked.right) {
        console.log('clear');
        clearInterval(firstLoop)
      }
    },60)
  }

#2

Seems to be a loop speed issue. 60ms loop doesn’t collide as expected, but attempting to mimic the input key speed by setting it to 1ms does.

Perhaps(and I’m entirely guessing) something to do with framerate/loop syncing?


#3

Hi @colemars,
When the collision occurs between the sprite and the tile, the physical engine activates the “blocked.right” property and applies a separation algorithm on the X axis that modifies the velocityX of the sprite:

body.velocity.x = -body.velocity.x * body.bounce.x;

You can see the full code here.
Every 60ms, when you check the condition “child.body.blocked.right”, this is false because the separation has already been applied (arcade physics).
Regards.


#4

Avoid setInterval().