[Experiment] Custom Room Viewport and Camera

Started by Crimson Wizard, Mon 22/10/2018 02:11:02

Previous topic - Next topic

rongel

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: ags
Game.Camera.TweenY(1.0, 100, eEaseInOutSineTween, eBlockTween);
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.
Dreams in the Witch House on Steam & GOG

Crimson Wizard

#21
Quote from: rongel on Wed 04/08/2021 08:39:57
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).

rongel

Quote from: Crimson Wizard on Wed 04/08/2021 09:41:28
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.
Dreams in the Witch House on Steam & GOG

Crimson Wizard

#23
Quote from: rongel on Wed 04/08/2021 12:23:10
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: ags

void RecalcCameraPos(Camera *cam, float oldScale, float newScale, int anchorX, int anchorY)
{
    int offx = -cam.X; int offy = -cam.Y;
    int roomAnchorX = (anchorX + offx) / oldScale;
    int roomAnchorY = (anchorY + offy) / oldScale;
    int newRoomDistX = anchorX / newScale;
    int newRoomDistY = anchorY / newScale;
    offx = roomAnchorX  - newRoomDistX;
    offy = roomAnchorY  - newRoomDistY;
    cam.SetAt(-offx, -offy);
}


Crimson Wizard

#24
Alright, if you want to hardcode everything and use the simpliest possible code, then perhaps this

Code: ags

int x = (Game.Camera.Width - Room.Width) / 2;
int y = (Game.Camera.Height - Room.Height) / 2;
Game.Camera.SetAt(x, y);


You have to call it in rep-exec (or rep-exec-always) while the tween is active.

rongel

Quote from: Crimson Wizard on Wed 04/08/2021 12:59:09
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: ags
 bool scale;

  scale =true;
  Game.Camera.TweenY(1.0, 100, eEaseInOutSineTween, eBlockTween);
  Game.Camera.TweenSize(2.0, 160, 90, eEaseInOutSineTween, eBlockTween);
  Game.Camera.TweenSize(2.0, 320, 180, eEaseInOutSineTween, eBlockTween);
  Game.Camera.TweenY(1.0, -100, eEaseInOutSineTween, eBlockTween);
  scale =false;


And this in rep-exec-always:

Code: ags
if(scale) {
    int x = (Game.Camera.Width - Room.Width) / 2;
    int y = (Game.Camera.Height - Room.Height) / 2;
    Game.Camera.SetAt(x, y);
  }


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).
Dreams in the Witch House on Steam & GOG

Crimson Wizard

I made a mistake, it should be other way around:
Code: ags

int x = (Room.Width - Game.Camera.Width) / 2;
int y = (Room.Height - Game.Camera.Height) / 2;
Game.Camera.SetAt(x, y);


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.

rongel

Quote from: Crimson Wizard on Wed 04/08/2021 13:48:58
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: ags
if(scale) {
    int x = (Room.Width - Game.Camera.Width) / 2;
    int y = Game.Camera.Y;
    Game.Camera.SetAt(x, y);
  }


QuoteAlso, 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: ags
 scale =true;
  Game.Camera.TweenY(2.0, Game.Camera.Y -100, eEaseInOutSineTween, eNoBlockTween);
  Game.Camera.TweenSize(2.0, 160, 90, eEaseInOutSineTween, eBlockTween);
  Game.Camera.TweenSize(2.0, 320, 180, eEaseInOutSineTween, eNoBlockTween);
  Game.Camera.TweenY(2.0, Game.Camera.Y +100, eEaseInOutSineTween, eBlockTween);
  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
Dreams in the Witch House on Steam & GOG

Crimson Wizard

This is a standard feature since AGS 3.5.0, more questions could be asked in Tech Support section. Closing this thread now.

SMF spam blocked by CleanTalk