Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arcade bodies do not scale with camera #315

Open
fyyyyy opened this issue Aug 7, 2017 · 15 comments
Open

arcade bodies do not scale with camera #315

fyyyyy opened this issue Aug 7, 2017 · 15 comments

Comments

@fyyyyy
Copy link

fyyyyy commented Aug 7, 2017

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.
RolandMQuiros/phaser@f732cc1

@samme
Copy link
Collaborator

samme commented Aug 7, 2017

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

Maybe we can scale the body by worldScale instead.

@fyyyyy
Copy link
Author

fyyyyy commented Aug 18, 2017

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, ...
}

@samme
Copy link
Collaborator

samme commented Aug 18, 2017

That looks good. You can also pass params like

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

@fyyyyy
Copy link
Author

fyyyyy commented Aug 19, 2017

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

@samme
Copy link
Collaborator

samme commented Sep 3, 2017

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.

@fyyyyy
Copy link
Author

fyyyyy commented Sep 5, 2017

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

@samme
Copy link
Collaborator

samme commented Sep 5, 2017

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.

@fyyyyy
Copy link
Author

fyyyyy commented Sep 7, 2017

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.

@samme
Copy link
Collaborator

samme commented Sep 7, 2017

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.

@hexus
Copy link
Contributor

hexus commented Sep 25, 2017

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

hexus/phaser-ce@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.

@samme
Copy link
Collaborator

samme commented Sep 25, 2017

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

@hexus
Copy link
Contributor

hexus commented Sep 25, 2017

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

@samme
Copy link
Collaborator

samme commented Sep 25, 2017

@hexus
Copy link
Contributor

hexus commented Sep 26, 2017

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?

@samme
Copy link
Collaborator

samme commented May 5, 2018

body.syncBounds may help with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants