ArcadePhysics.overlap() lagging behind if object repositioned in previous statement

arcade physics.overlap() fails to return true if the object was immediately repositioned in the previous statement to overlap with one another, furthermore, when repositioning away from one another it instead returns the opposite of the desired output: causing the behavior appreciable in the following video:


mirror

The active code is straightforward, a method called within the update loop:

knifeHitbox.x=knifeHitbox.player.x+((knifeHitbox.player.flipX)?-60:60);
if (this.scene.physics.overlap(KnifeHitbox,target)){console.log("hit");}

I’m certain I need to workaround this problem, as my design is unprofessional, yet I’m still curious about this dynamic

You have to reposition the physics body instead.

1 Like

Thanks again,
Is the body position not linked to the sprite or is this problem is caused by the physics engine not having time to adjust the variables?

The sprite is updated from the physics body only once per step, after update().

I have found that it’s also possible to run Body.preUpdate() to force the physics to adjust the body positions.

// set mySprite position
mySprite.body.preUpdate();

This might be a simpler solution due that to move the body you are repositioning its top-left corner, resulting in asymmetric reposition that needs to be worked around as well. However, I cannot assure this solution performance-wise.