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

Adjust camera scroll bounds to account for zoom #1889

Merged
merged 1 commit into from
Aug 21, 2016

Conversation

JoeCreates
Copy link
Member

@JoeCreates JoeCreates commented Jul 20, 2016

Currently there is an issue with FlxCamera's scroll bounds if you zoom. If you are zoomed then move to the edge of the scroll bounds, you will not be able to see a part of the side of the map, because the scroll bounds are only correct at a zoom of 1.

For example, if your camera is targeting a player, and your player is right beside the scroll bounds, then you attempt to zoom on the player, the player will go off-screen in spite of it being your camera's target.

This pull request adds the required adjustments to correctly apply the scroll bounds irrespective of the current zoom. When you are zoomed, your camera's size is effectively shrunk, and it is this shrunk camera that is prevented from passing the scroll bounds, rather than the camera's size at a zoom of 1.

@Gama11
Copy link
Member

Gama11 commented Jul 20, 2016

What's the best way to test this change? Any of the demos?

@JoeCreates
Copy link
Member Author

JoeCreates commented Jul 20, 2016

Arrows to move, space to zoom. Note what happens when the sprite is near the bounds when you zoom.

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.math.FlxMath;
import flixel.tweens.FlxEase;
import flixel.tweens.FlxTween;
import flixel.util.FlxColor;
import flixel.util.FlxSpriteUtil;

class TestState extends FlxState {
    var spr = new FlxSprite();
    var tween:FlxTween;

    override public function create():Void {
        super.create();
        FlxG.camera.setScrollBoundsRect(0, 0, 700, 700, true);
        FlxG.camera.follow(spr);
        var bg = new FlxSprite(20, 20);
        bg.makeGraphic(660, 660, FlxColor.fromHSB(0, 0.3, 0.3));
        for (x in 0...5) {
            for (y in 0...10) {
                FlxSpriteUtil.drawRect(bg, x * 132 + y % 2 * 66, y * 66, 66, 66, FlxColor.fromHSB(0, 0.3, 0.5));
            }
        }
        spr.scale.set(3, 3);
        add(bg);
        add(spr);

    }

    override public function update(dt:Float):Void {
        super.update(dt);
        spr.velocity.set();
        if (FlxG.keys.pressed.LEFT) spr.velocity.x = -200;
        if (FlxG.keys.pressed.RIGHT) spr.velocity.x = 200;
        if (FlxG.keys.pressed.UP) spr.velocity.y = -200;
        if (FlxG.keys.pressed.DOWN) spr.velocity.y = 200;
        if (FlxG.keys.justPressed.SPACE) {
            if (tween != null) tween.cancel();
            tween = FlxTween.tween(FlxG.camera, {zoom: 2}, 0.5, {ease: FlxEase.quadInOut});
        } else if (FlxG.keys.justReleased.SPACE) {
            if (tween != null) tween.cancel();
            tween = FlxTween.tween(FlxG.camera, {zoom: 1}, 0.5, {ease: FlxEase.quadInOut});
        }

        spr.x = FlxMath.bound(spr.x, FlxG.worldBounds.left, FlxG.worldBounds.right - spr.width);
        spr.y = FlxMath.bound(spr.y, FlxG.worldBounds.top, FlxG.worldBounds.bottom - spr.height);
    }
}

@JoeCreates
Copy link
Member Author

@Gama11 I re-added some changes which I had somehow lost when making this branch, but it should be good to pull again, now.

@JoeCreates
Copy link
Member Author

Note that the FlxCamera demo doesn't exhibit the issue as it effectively performs this calculation itself when you zoom in (https://github.com/HaxeFlixel/flixel-demos/blob/master/Features/FlxCamera/source/PlayState.hx#L164). Doing it that way means that you have to separately store your own values for "bounds at 1x zoom" and use these to recalculate the scroll bounds every time you change the zoom.

This change is therefore breaking, although I think it should be included in flixel. The solution in the FlxCamera is more of a workaround.

@Gama11 Gama11 added this to the 4.2.0 milestone Aug 4, 2016
@Gama11 Gama11 merged commit 0f6ac6d into HaxeFlixel:dev Aug 21, 2016
@Gama11
Copy link
Member

Gama11 commented Aug 21, 2016

Thanks for the fix and the demo code, showcased the issue very nicely. I think the breaking aspect here is ok, as it's essentially a bugfix.

That zoom-in-effect in the demo code is quite nice visually, could you add that to the FlxCamera demo in some capacity? :) Perhaps it could replace the current zooming controls, which seem a bit awkward...

Gama11 added a commit to HaxeFlixel/flixel-demos that referenced this pull request Aug 21, 2016
Aurel300 pushed a commit to larsiusprime/haxeflixel that referenced this pull request Apr 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants