phaser-ce icon indicating copy to clipboard operation
phaser-ce copied to clipboard

arcade bodies do not scale with camera

Open fyyyyy opened this issue 8 years ago • 16 comments

This Issue is about

  • A bug in the API:
    • Phaser version(s): 2.6.1 and earlier up to 2.8.1 and potentially higher
    • What should happen: As sprites do scale with camera, arcade bodies should scale accordingly
    • What happens instead: Sprites scale with camera, however arcade bodies stay unaffected by camera scale, leading to incorrect collisions. this.game.camera.scale.setTo(2)
  • An error in the documentation: The documentation should at least hint at this problem

See also a potential fix. https://github.com/RolandMQuiros/phaser/commit/f732cc1129c28f1cd821e28259f942f34a11c1a6

fyyyyy avatar Aug 07 '17 10:08 fyyyyy

Yes, it's always worked this way. https://codepen.io/samme/pen/OWyPqq

Maybe we can scale the body by worldScale instead.

samme avatar Aug 07 '17 20:08 samme

Is it possible to attache listeners to the camera ? So that entities can subscribe to a camera onScale event and scale their bodies accordingly. This would make it easier to update all bodies in the world.

n/m i figured this out i believe

  // init() ...
   game.camera.scale.onChange = new Phaser.Signal();

 // whenever you change camera scale, dispatch the event
    this.game.camera.scale.onChange.dispatch();

 // in your sprites, listen to the event
    this.game.camera.scale.onChange.add(this.updateBody.bind(this));

// update body
updateBody() {
  this.body.setSize(width * this.game.camera.scale.x, ...
}

fyyyyy avatar Aug 18 '17 17:08 fyyyyy

That looks good. You can also pass params like

this.game.camera.scale.onChange.dispatch(this.game.camera.scale);
// …
updateBody(cameraScale) {
  this.body.setSize(/* … */);
}

samme avatar Aug 18 '17 21:08 samme

ok thanks, here is an example in case anyone is curious. https://codepen.io/anon/pen/ayqMLP?editors=0010

fyyyyy avatar Aug 19 '17 10:08 fyyyyy

I made a branch for this, substituting sprite.worldScale for sprite.scale. The body size was correct but the offset was not, so I guess I've missed something.

samme avatar Sep 03 '17 20:09 samme

I guess all my sprites had anchor 0.5, thats why there wasnt any offset issues when scaling

fyyyyy avatar Sep 05 '17 08:09 fyyyyy

I think it's complicated because with worldScale the scale origin is the the top-left of the group (usually [0, 0]), whereas with scale it's the sprite's anchor.

samme avatar Sep 05 '17 20:09 samme

maybe it should work like the fixedToCamera: true property as in scaleWithCamera: true or scaleWithWorld: true Then users can set it individually for groups or sprites, by default its false to not break existing code.

fyyyyy avatar Sep 07 '17 08:09 fyyyyy

I think the formula in https://github.com/photonstorm/phaser-ce/blob/v2.8.5/src/physics/arcade/Body.js#L618 needs to be changed somehow if worldScale is substituted there.

samme avatar Sep 07 '17 21:09 samme

The real fix here is not to scale physics at all, but instead detach camera scaling from physics entirely.

https://github.com/hexus/phaser-ce/compare/master...camera-transform

This is a branch I've been working on, but I've only been hesitant to make a PR because it's a big change and will need some testing across all examples.

hexus avatar Sep 25 '17 05:09 hexus

Well there's always https://codepen.io/samme/full/ZLbGzB/ 😸

samme avatar Sep 25 '17 06:09 samme

Looks great, but changes the resolution of the canvas! Ouch!

hexus avatar Sep 25 '17 06:09 hexus

Also looks pretty nice, though it's using CSS, and the world is cropped if you zoom out further than 1x.

I still think the best solution is to decouple the camera from the physics, as I've implemented. :)

By the way, are you on the Phaser Slack @samme?

hexus avatar Sep 26 '17 12:09 hexus

body.syncBounds may help with this.

samme avatar May 05 '18 19:05 samme