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
37.12k stars 7.1k forks source link

Scaled world confuses camera follow, breaks fixedToCamera #512

Closed jigglycrumb closed 10 years ago

jigglycrumb commented 10 years ago

See live example here: http://phasercamera.hpcodecraft.me

Holding down the right arrow key makes the mushroom leave the screen and the camera does not follow. Also, the UI at the top starts to wander off screen. If globalScale is set to 1 everything works as expected.

photonstorm commented 10 years ago

Ok this is fixed now for objects that have fixedToCamera set. But I cannot think of a clean way to do it for camera follow targets - the camera never changes the position of the target, it just focuses on it, but the object has gone 'off screen' because the camera is only displaying a zoomed-in portion of what it can normally see. It /thinks/ the rest is still there, it doesn't know it's hidden off the side of the canvas.

I have tried shrinking the deadzone based on scale, but that doesn't stop the target object wandering off, although it possibly could in combination with adjusting the worlds pivot point. If you change the pivot of the world then all kinds of interesting and screwy things happen. My solution honestly would be to create a Group (or DisplayObjectContainer) that sits above the World on the Stage, and you pivot and rotate that as needed. Not ideal I know, sorry - but at least fixedToCamera was resolved! (unless pivot changes of course.. damnit)

anissen commented 10 years ago

Sorry to comment on a closed issue, but this issue has not been properly resolved in my opinion.

I also need to zoom in/out on a object that the camera follows. I tried following the advice of grouping everything except camera in a group and scaling that, but I still have the problem of the camera losing track of the object it follows.

I made an implementation myself using Pixi that handles zooming in/out on a moving object. I do it something like this:

  var halfWidth  = (canvas.width / 2)  / camera.zoom;
  var halfHeight = (canvas.height / 2) / camera.zoom;
  // clamping the camera within the bounds of the world
  var clampedX = clamp(followObject.position.x, halfWidth, world.width - halfWidth);
  var clampedY = clamp(followObject.position.y, halfHeight, world.height - halfHeight);
  camera.x = (-clampedX * camera.zoom + canvas.width / 2);
  camera.y = (-clampedY * camera.zoom + canvas.height / 2);

  layer.scale.set(camera.zoom, camera.zoom);
  layer.position.set(camera.x, camera.y);

The result: alt text

There are still a lot of unanswered questions about zooming in the Phaser forums (http://www.html5gamedevs.com/forum/14-phaser/) so I think this issue be reconsidered.

photonstorm commented 10 years ago

It's been resolved in the sense that you're doing something the camera was never designed to handle, so the fact it can't cope with it isn't surprising. Realistically this won't get revisited until we look at replacing the camera entirely with a multiple camera system.

If someone wants to provide a pull request that solves it (without modifying the core API) then I'd always look at that in the meantime.

hilts-vaughan commented 10 years ago

This should at the very least throw an exception then, not show weird behavior.

On Wed, Mar 19, 2014 at 6:34 PM, Richard Davey notifications@github.comwrote:

It's been resolved in the sense that you're doing something the camera was never designed to handle, so the fact it can't cope with it isn't surprising. Realistically this won't get revisited until we look at replacing the camera entirely with a multiple camera system.

If someone wants to provide a pull request that solves it (without modifying the core API) then I'd always look at that in the meantime.

Reply to this email directly or view it on GitHubhttps://github.com/photonstorm/phaser/issues/512#issuecomment-38115802 .

anissen commented 10 years ago

I'm not sure exactly what is and what isn't designed to be the responsibility of the camera system.

However, it would be very nice if the camera had some of the functionality similar to e.g. the HaxeFlixel camera. Example: http://haxeflixel.com/demos/FlxCamera/

photonstorm commented 10 years ago

There are lots of things that would be nice for it to do, but it's trivial for flixel because it can just happily work away on a single bitmap. We don't have that luxury in WebGL. The first ever version of Phaser had the most advanced camera of all - multicams, rotation, scaling, positioning anywhere. Something we had to drop all of in order to use Pixi.

Anyway I loathe frameworks throwing exceptions, that should only ever happen in truly nuclear situations, of which this isn't one.

hayesmaker commented 9 years ago

+1, cameras should zoom.. if @anissen could share that clamp function, maybe it can be implemented in Phaser.

photonstorm commented 9 years ago

There are lots of things it would be nice for the Camera to do, but it doesn't alter the fact it can't actually do it yet. It's why we wrote them entirely for Phaser 3.

hayesmaker commented 9 years ago

How many cameras do you own which can't zoom?

photonstorm commented 9 years ago

Irrelevant. The only one that matters here is the Phaser one, and it doesn't claim to be able to support it anywhere. It's not that the feature doesn't work, it's that it doesn't exist. Feel free to add it.