Golen Posted September 9, 2017 Share Posted September 9, 2017 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. ^ The collision area updates its size first, and later snaps to its new offset. ^ The sword sprite, 48x48. Red box indicates where the body's collision area should be. Link to comment Share on other sites More sharing options...
samme Posted September 9, 2017 Share Posted September 9, 2017 I would try sprite.body.setSize(/* … */); sprite.reset(sprite.x, sprite.y); Taz 1 Link to comment Share on other sites More sharing options...
Golen Posted September 9, 2017 Author Share Posted September 9, 2017 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 More sharing options...
Taz Posted September 10, 2017 Share Posted September 10, 2017 Ninja physics handle it for you - ignoring it lets Arcade be lighter and quicker I think... Link to comment Share on other sites More sharing options...
Recommended Posts