Camera deadzone help

114 views
Skip to first unread message

Gimmicky Apps

unread,
Mar 12, 2016, 5:57:16 AM3/12/16
to HaxeFlixel
Hi everyone,

I have hopefully a quick question about deadzones with the normal camera following.  I have a level that is screen-width and very tall.  I want the camera to follow normally, except when the character would cause the camera to scroll beyond the bottom of the initial screen.  In that case, I want the camera to stay on the initial screen, but when the character goes beyond the bottom, to let the camera scroll with the character at the bottom edge, until the "real" bottom of the level is reached.  Visually, what I'm looking for is:



What have I tried?

1. My interpretation of the deadzone help ("The camera will always keep the focus object inside the dead zone, unless it is bumping up against the camera bounds") was that I should set the deadzone to a very high negative Y value, and the bottom of it to the bottom of the screen.  (Above: from very top, down to red line.)  When I do that, the camera doesn't scroll at all, ever.  So I guess that's not the right interpretation.

2. Thinking I had the idea backwards, I also tried setting it from 0 to the real bottom.  (Above: from green line, down to very bottom.)  That made it so the camera scrolls correctly when the character goes up, but when it goes down, the camera also follows, keeping the character centred until near the real bottom, instead of letting the character push the bottom of the camera scroll starting at the initial screen.

So...how can this be done?  Or can it not?

Thanks for any help.

GA
Auto Generated Inline Image 1

Domagoj Štrekelj

unread,
Mar 13, 2016, 5:55:07 AM3/13/16
to HaxeFlixel
Hello,

I haven't tried anything even remotely similar, but there is no reason why this wouldn't be possible to do. I would try changing the deadzone to a more appropriate one when the character enters the "region of interest". The TOPDOWN follow style would be a good reference to begin with. Though I think you only need to set the deadzone to match the screen size in order to get the effect you desire.

Best of luck,

Domagoj

Gimmicky Apps

unread,
Mar 14, 2016, 4:34:45 PM3/14/16
to HaxeFlixel
Hi Domagoj,

Thanks for the suggestions.  Unfortunately, I forgot to mention that I had already tried something similar.  The result has a couple problems:

1) In transitioning from the upper part to the region of interest, there's a sudden jump.  (This may be solvable.  Since the character can rotate freely to any angle, we'd have to calculate the angled distance, and also reverse-calculate exactly where the camera would normally follow.  Not impossible, but neither does it strike me as being the user-friendly, forward-compatible, intended method.)

2) Once in the region of interest, the character is not followed using the "pushing the boundary" style I'm looking for, but instead screen-by-screen (camera suddenly jumps down a full screen when character reaches the bottom, suddenly putting character at the top of the next screen.)

So when you say "there is no reason why"...well, sure, in a sense... :)  But I still don't find a way for it to work out-of-the-box.

I guess nobody else had this particular set of requirements for any of their games?

Gimmicky

Domagoj Štrekelj

unread,
Mar 15, 2016, 4:50:30 AM3/15/16
to HaxeFlixel
It's odd that you're experiencing sudden transitions when going from top to bottom. I would have understood if you had experienced the camera suddenly "snapping" into a new position when moving from the lower to the upper part of the level. That's what happens when you naively switch from a looser to a tighter deadzone.

But it should be really simple. It just takes a little figuring out to get it to work the way you want to. Like I suggested, I changed the camera deadzone when the player moved into the region of interest (marked by an arbitrary Y position). Here is the result. From what I understand this is what you want to achieve, correct?

var deadzoneTight : FlxRect;
var deadzoneScreen : FlxRect;
var world : FlxRect;

// in create()

world = FlxRect.get(0, 0, 320, 540);
deadzoneTight = FlxRect.get((FlxG.width - player.width) / 2, (FlxG.height - player.height) / 2, player.width, player.height);
deadzoneScreen = FlxRect.get(0, 0, FlxG.width, FlxG.height);

FlxG.camera.follow(player);
FlxG.camera.deadzone = deadzoneTight;
FlxG.camera.setScrollBoundsRect(world.x, world.y, world.width, world.height);

// in update()

if (player.y > 180) FlxG.camera.deadzone = deadzoneScreen;
else FlxG.camera.deadzone = deadzoneTight;

Obviously, you wouldn't want to (re)set the camera deadzone every frame, but it's fine for demo purposes.

Gimmicky Apps

unread,
Mar 15, 2016, 6:02:24 AM3/15/16
to HaxeFlixel
Hi Domagoj,

The mp4 you linked to is exactly what I was trying to achieve.  I had misunderstood--since there was no deadzone originally (and that produced the correct upper-half behaviour), in my attempt I was setting the deadzone to the lower half of the level when the character was there, and otherwise setting it to null.  I'll have to give your method a shot.  Thanks very much!

Gimmicky

Gimmicky Apps

unread,
Mar 15, 2016, 9:59:44 AM3/15/16
to HaxeFlixel
BTW why "FlxRect.get()" instead of "new FlxRect()"?  I hadn't seen that idiom before, is there any advantage to it?

GA

Gimmicky Apps

unread,
Mar 15, 2016, 11:48:00 AM3/15/16
to HaxeFlixel
Never mind, I see now in the docs about recycling.  Cool.  -GA

Gimmicky Apps

unread,
Mar 15, 2016, 11:51:26 AM3/15/16
to HaxeFlixel
Hi again Domagoj,

I see now my fundamental misunderstanding about deadzones was that they're relative to the screen, not the world.  I suppose another way to achieve the effect would have been to mess with the scroll bounds.

I also see that my actual requirements were a bit more complex than I originally thought and put into the diagram!  But in the end I found a way to get an even better effect than I wanted, and I probably wouldn't have bothered figuring it out at this point without your encouragement, so thank you very much.

Yay!
GA

Domagoj Štrekelj

unread,
Mar 15, 2016, 1:53:42 PM3/15/16
to HaxeFlixel
Sorry for not replying earlier. For some reason I wasn't getting any notifications about this topic being updated. Otherwise I would have gotten back to you sooner.

I'm glad you figured it out, though!

Gimmicky Apps

unread,
Mar 15, 2016, 2:00:54 PM3/15/16
to HaxeFlixel
No problem, I should've just checked the docs first anyway for .get().  Meanwhile I put to use my learning experience by trying to improve the FlxCamera demo to make it easier for others who are learning in the future:

https://github.com/HaxeFlixel/flixel-demos/pull/224

-Gimmicky
Reply all
Reply to author
Forward
0 new messages