Jump to content

Arcade body.setSize - Offset and size not in sync!


Golen
 Share

Recommended Posts

I'm trying to add a collision box to a sword swing. I'm using one sprite for the swing and rotating it 0, 90, 180, 270 degrees, depending on direction. As physics bodies are not affected by their sprite's rotation, I'm using body.setSize(width, height, offsetX, offsetY) to move the collision area depending on direction.

Problem: It seems that (width, height) updates earlier than (offsetX, offsetY), or that the new offset is delayed one update. This leads to the hitbox reaching beyond the player's attack range. How can I avoid this?

One way to easily test this is by calling sprite.body.setSize( <randomX>, <randomY>, <randomOffsetX>, <randomOffsetY> ). The sprite's angle does not affect this.

59b35265634e8_RYsAU6v-Imgur.gif.d9cd0d21e2b05af0969eeb1868fdb31a.gif

^ The collision area updates its size first, and later snaps to its new offset.

sword.png.caaaac14a8792246d4fe6e527ca435c5.png

^ The sword sprite, 48x48. Red box indicates where the body's collision area should be.

Link to comment
Share on other sites

Nice catch!! That solution works well.

I'm having a look at the source, and my guess is that the position is only updated in body.preUpdate(), which is called the next frame? Meanwhile, the collision and render is called prior to that. Size is updated immediately in body.setSize().

(I'm slightly annoyed that the body is affected by scale, but not angle. In case I want the sword to alternate directions by flipping the y axis, I have to add calculate additional offset.)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...