phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
36.94k stars 7.08k forks source link

Bug in sprite bounds / position when Arcade physics applied to body #726

Closed leaanthony closed 10 years ago

leaanthony commented 10 years ago

I have a sprite which is 32x32 and has arcade physics applied to it. When the sprite is still, the bounds fit perfectly:

stationary

But when it is moving, the sprite 'leaks' from the bounds. It's like the bounds are a split second behind the sprite:

moving1 moving 2 moving 3

The bounds are set to (0.5, 0.5) so should always be centered. The sprite is being rotated but that shouldn't affect the bounds. Interestingly, if I debug the bounds for the ship, the width and height changes:

Phaser.Rectangle {x: 486.71042494622895, y: 503.5811161641881, width: 40.04378847013709, height: 40.04378847013709, offset: function…}

Phaser.Rectangle {x: 11.992004369155975, y: 397.25188881279473, width: 37.1140292754896, height: 37.114029275489656, offset: function…}

Phaser.Rectangle {x: 215.11751794705464, y: 320.18095387933505, width: 36.89780461660354, height: 36.897804616603594, offset: function…}

Phaser.Rectangle {x: 575.6806394588796, y: 433.29808318578574, width: 35.31004921495287, height: 35.310049214952755, offset: function…}

Any ideas what is happening here?

lewster32 commented 10 years ago

The physics and rendering loops are decoupled in Phaser so there's a small degree of lag between the sprite's visual position and its physics body. This is the way virtually all implementations of physics in modern SDKs work.

Out of interest is this causing a particular problem with your game, or is this just an observation you've made?

leaanthony commented 10 years ago

Hi @lewster32. Thanks for the feedback. I'm having some issues getting bullets fired along the line that I'm drawing from the anchor point through the angle of rotation. I enabled debug to check that my calculations were correct when I found the behaviour described above. I presumed they were linked but from what you're saying they may not be?

lewster32 commented 10 years ago

I guess if it's an issue of having the bullets spawn at a specific visual location in relation to the sprite, you should use the sprite's position, whereas if you're in need of more accuracy in the physics, you'd use the body's position. These two will often differ slightly, and choosing which one to use depends on what you're trying to achieve.

photonstorm commented 10 years ago

It also depends when you're trying to achieve it. You can test if the body is in preUpdate or postUpdate by checking body.phase. preUpdate will be 1, postUpdate will be 2. If it's phase 1 and you use sprite x/y values then they will have changed by the time the render pass happens (as the sprite is repositioned in postUpdate).

leaanthony commented 10 years ago

Thanks for the info guys. This isn't a bug so I'm happy for it to be closed.