Golen

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

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.

Share this post


Link to post
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.)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.