Cursor Camera Movement

Started by Ewen, Fri 27/06/2025 11:47:19

Previous topic - Next topic

Ewen

Hello,

I have added a map screen to my game. The map itself is a room and the background image is currently the same screen resolution as the rest of the game (which is cool and works really well) but I was wondering if it is possible to import a larger version of the map and then to have the camera move across it with the mouse cursor. For example, if the mouse moves to the top of the screen, the map moves with it.

I know its easy to have the camera following a character as they walk through a room bit I'm not sure if movement can be assigned to mouse movement.

This seems like it should be possible but, as a beginner, I'm not sure where to start. Any thoughts on this would be very welcome :)

Thank you!

Ewen

Snarky

You can control what parts or a room are displayed using Game.Camera, either setting Game.Camera.X and Game.Camera.Y, or using Game.Camera.SetAt(x,y). So you could for example do a check in Room_RepExec() and increment or decrement the Game.Camera.X/Y values if the mouse is close to the edge of the screen, to create a scrolling effect.

Khris

Another way is to map the mouse position to the viewport directly. This works best if your map has a similar aspect ratio as your screen.

Code: ags
// add this above room_RepExec
int map_width = 1400;
int map_height = 900;

function MoveMap() {
  int sw = Game.Camera.Width, sh = Game.Camera.Height;
  Game.Camera.SetAt(mouse.x * (map_width - sw) / sw, mouse.y * (map_height - sh) / sh);
}

  // add this inside room_RepExec
  MoveMap();

Ewen

Thanks both of you for the suggestions. I went with Khris' suggestion and it works really well. The only issue is that, when the room loads there is a bit of a camera jump from one part of the map to another. This seems to be linked to the position of the invisible player when they enter the room and also the position of the cursor on the room before when clicking the button that opens the map.

Is there any way to set the starting position of the camera and the cursor so that it doesn't look jumpy on entering the room? I've played around with setting the cursor's position and the player position but its not quite there.

It is otherwise working brilliantly.

Crimson Wizard

Try calling this MoveMap() in the room's "before fade-in" event.

Ewen

Amazing! That did the job. Thanks so much for your help.

SMF spam blocked by CleanTalk