Author Topic: [Experiment] Custom Room Viewport and Camera  (Read 5667 times)

Re: [Experiment] Custom Room Viewport and Camera
« Reply #20 on: 04 Aug 2021, 08:39 »
Hi, I think I need help with the camera zooming. So I want the camera to rise up and then do a simple zoom in the middle of the screen, then return back to its original location.  My room is 320 x 320, and the game is 320 x 180.

Here's the simplified version of what I did:

Code: Adventure Game Studio
  1. Game.Camera.TweenY(1.0, 100, eEaseInOutSineTween, eBlockTween);
  2. Game.Camera.TweenSize(1.0, 160, 90, eEaseInOutSineTween, eNoBlockTween);

It works otherwise but the zooming (Game.Camera.TweenSize) is anchored to the upper right corner. So the zooming doesn't happen at the center of the screen where I would like it to happen. I tried to move the camera at the same time to counter the position change, but it didn't seem to work. What would be the correct way to zoom, so that it's centered? I am using Smooth Scroll module, but it should be disabled for this room.

Crimson Wizard

  • Local Moderator
    • Lifetime Achievement Award Winner
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Crimson Wizard worked on one or more games that won an AGS Award!
    •  
    • Crimson Wizard worked on one or more games that was nominated for an AGS Award!
Re: [Experiment] Custom Room Viewport and Camera
« Reply #21 on: 04 Aug 2021, 09:41 »
It works otherwise but the zooming (Game.Camera.TweenSize) is anchored to the upper right corner. So the zooming doesn't happen at the center of the screen where I would like it to happen. I tried to move the camera at the same time to counter the position change, but it didn't seem to work. What would be the correct way to zoom, so that it's centered? I am using Smooth Scroll module, but it should be disabled for this room.

A while ago I was writing a code for zooming Room Editor into the mouse cursor position. It aligns the zoom to the arbitrary point on screen. This is C#, but the math should be the same, so it's a matter of rewriting into AGS script:
https://github.com/adventuregamestudio/ags/blob/ags4/Editor/AGS.Editor/Panes/Room/RoomSettingsEditor.cs#L1091

Where this code sais "window" in AGS it should mean "(game) screen".
"Offset" - is a room offset, which is opposite to camera position: you can get camera position from it by taking these coordinates with a minus (Camera.X = -Offset.X, Camera.Y = -Offset.Y).
"Anchor" is a point in screen coordinates to which you align the zoom. In your case "Anchor" will be the center of the screen, calculated as (x = Screen.Width / 2, y = Screen.Height / 2).

EDIT: I think there's one thing extra that you do not need: this function returns offset in window (screen) coordinates, but you need to set camera so need room coordinates.
So probably you just skip the very last line in that code and use "newRoomLT" value to set camera (remember to take it with a minus).
« Last Edit: 04 Aug 2021, 09:50 by Crimson Wizard »

Re: [Experiment] Custom Room Viewport and Camera
« Reply #22 on: 04 Aug 2021, 12:23 »
A while ago I was writing a code for zooming Room Editor into the mouse cursor position. It aligns the zoom to the arbitrary point on screen. This is C#, but the math should be the same, so it's a matter of rewriting into AGS script:

Hey thanks CW! That's a lot of code. In my game I'm using zoom only in this one particular scene (it's a cutscene, so it's quite simple to control). I'm a bit hesitant to start working with your code, I guess I'm looking for a quick and dirty fix that would center the zoom effect. Is there any other way that to achieve that? Moving the camera while zooming didn't seem to work for some reason.

Crimson Wizard

  • Local Moderator
    • Lifetime Achievement Award Winner
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Crimson Wizard worked on one or more games that won an AGS Award!
    •  
    • Crimson Wizard worked on one or more games that was nominated for an AGS Award!
Re: [Experiment] Custom Room Viewport and Camera
« Reply #23 on: 04 Aug 2021, 12:49 »
That's a lot of code.

No, there's only 3 lines of math, everything else is comments.
Here's the version with all the comments and extra bits removed, and math split into more pieces for clarity

Code: Adventure Game Studio
  1. void RecalcCameraPos(Camera *cam, float oldScale, float newScale, int anchorX, int anchorY)
  2. {
  3.     int offx = -cam.X; int offy = -cam.Y;
  4.     int roomAnchorX = (anchorX + offx) / oldScale;
  5.     int roomAnchorY = (anchorY + offy) / oldScale;
  6.     int newRoomDistX = anchorX / newScale;
  7.     int newRoomDistY = anchorY / newScale;
  8.     offx = roomAnchorX  - newRoomDistX;
  9.     offy = roomAnchorY  - newRoomDistY;
  10.     cam.SetAt(-offx, -offy);
  11. }
  12.  
« Last Edit: 04 Aug 2021, 12:59 by Crimson Wizard »

Crimson Wizard

  • Local Moderator
    • Lifetime Achievement Award Winner
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Crimson Wizard worked on one or more games that won an AGS Award!
    •  
    • Crimson Wizard worked on one or more games that was nominated for an AGS Award!
Re: [Experiment] Custom Room Viewport and Camera
« Reply #24 on: 04 Aug 2021, 12:59 »
Alright, if you want to hardcode everything and use the simpliest possible code, then perhaps this

Code: Adventure Game Studio
  1. int x = (Game.Camera.Width - Room.Width) / 2;
  2. int y = (Game.Camera.Height - Room.Height) / 2;
  3. Game.Camera.SetAt(x, y);
  4.  

You have to call it in rep-exec (or rep-exec-always) while the tween is active.
« Last Edit: 04 Aug 2021, 13:01 by Crimson Wizard »

Re: [Experiment] Custom Room Viewport and Camera
« Reply #25 on: 04 Aug 2021, 13:33 »
Alright, if you want to hardcode everything and use the simpliest possible code, then perhaps this

Thanks again! I tried this, but for some reason, I can't seem to get it working.

I have this code when I activate the cutscene:

 
Code: Adventure Game Studio
  1.  bool scale;
  2.  
  3.   scale =true;
  4.   Game.Camera.TweenY(1.0, 100, eEaseInOutSineTween, eBlockTween);
  5.   Game.Camera.TweenSize(2.0, 160, 90, eEaseInOutSineTween, eBlockTween);
  6.   Game.Camera.TweenSize(2.0, 320, 180, eEaseInOutSineTween, eBlockTween);
  7.   Game.Camera.TweenY(1.0, -100, eEaseInOutSineTween, eBlockTween);
  8.   scale =false;
  9.  

And this in rep-exec-always:

Code: Adventure Game Studio
  1. if(scale) {
  2.     int x = (Game.Camera.Width - Room.Width) / 2;
  3.     int y = (Game.Camera.Height - Room.Height) / 2;
  4.     Game.Camera.SetAt(x, y);
  5.   }

When I do the action, the screen jumps instantly higher, and the scaling still happens at the corner... Maybe I'm missing something, I haven't done any camera setup. Also I'm using the v3.5.0, December 2019 version of AGS (I'm hesitant to upgrade when the game is so near the end).

Crimson Wizard

  • Local Moderator
    • Lifetime Achievement Award Winner
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Crimson Wizard worked on one or more games that won an AGS Award!
    •  
    • Crimson Wizard worked on one or more games that was nominated for an AGS Award!
Re: [Experiment] Custom Room Viewport and Camera
« Reply #26 on: 04 Aug 2021, 13:48 »
I made a mistake, it should be other way around:
Code: Adventure Game Studio
  1. int x = (Room.Width - Game.Camera.Width) / 2;
  2. int y = (Room.Height - Game.Camera.Height) / 2;
  3. Game.Camera.SetAt(x, y);
  4.  

You are doing TweenY too, perhaps you should also adjust camera position when TweenSize is active, not TweenY?

Also, not sure what are you intending this for, but you are calling TweenY with -100, yet the camera cannot go off the room bounds so that won't work.

Re: [Experiment] Custom Room Viewport and Camera
« Reply #27 on: 04 Aug 2021, 14:15 »
I made a mistake, it should be other way around:

Ok, much better now, I think I got it working! For some reson the Y value in the rep-exec-always made the screen jump up. I modified that bit and everything seems to work now:

Code: Adventure Game Studio
  1. if(scale) {
  2.     int x = (Room.Width - Game.Camera.Width) / 2;
  3.     int y = Game.Camera.Y;
  4.     Game.Camera.SetAt(x, y);
  5.   }

Quote
Also, not sure what are you intending this for, but you are calling TweenY with -100, yet the camera cannot go off the room bounds so that won't work.

Yes, that was my mistake, I have the camera actions like this now:

Code: Adventure Game Studio
  1.  scale =true;
  2.   Game.Camera.TweenY(2.0, Game.Camera.Y -100, eEaseInOutSineTween, eNoBlockTween);
  3.   Game.Camera.TweenSize(2.0, 160, 90, eEaseInOutSineTween, eBlockTween);
  4.   Game.Camera.TweenSize(2.0, 320, 180, eEaseInOutSineTween, eNoBlockTween);
  5.   Game.Camera.TweenY(2.0, Game.Camera.Y +100, eEaseInOutSineTween, eBlockTween);
  6.   scale =false;

So the camera goes up 100 pixels, and starts to zoom in, then it zooms out and goes down 100 pixels. And the view is nicely centered now. I still have to check if the smooth scroll parallax effect works, if I turn it back on. If it doesn't, I'll have to move my parallax objects manually, but it shouldn't be a problem.

Thanks CW!  ;-D