Scroll Room with Look Cursor

Started by ddq, Mon 31/08/2009 06:16:14

Previous topic - Next topic

ddq

For my game, I would like in scrolling rooms to have the camera follow the cursor when it is in look mode. Not completely strictly, but as if the cursor and player character are two endpoints of a line that is the length of the screen, so that the camera will not pan far enough so that the player character is not visible, with a few dozen pixel margin.

Though it sounds a bit complicated, to me at least, I'm putting this question in the beginner's tech forum because it sounds like it could be implemented with some pretty simple scripting that I would almost certainly botch. But tell me if it's impossible so I can cry myself to sleep.

Akatosh

You will probably want the SetViewport functions for that. Here's an untested example that may or may not work:

Code: ags

//repeatedly_execute
int minx=player.x-10;
int miny=player.y+10;
int scrx=mouse.x+(System.ScreenWidth/2);
int scry=mouse.y+(System.ScreenHeight/2);

if (minx > scrx) minx=scrx;
if (miny > scry) miny=scry;

SetViewport(minx, miny);


What this code does (or shoud do) is that the mouse cursor is kept in the middle of the screen, unless that would cause the player character to go offscreen. Here's hoping it actually works...

Khris

#2
I'll give it a shot:

Code: ags
// inside rep_ex

  int minx, maxx;
  int margin = 20;
  int sw = System.ViewportWidth;
  int a,b;

  if (mouse.Mode == eModeLookat) {

    minx = player.x - sw + margin;
    if (minx < 0) minx = 0;
    maxx = player.x + sw - margin;
    if (maxx > Room.Width - sw) maxx = Room.Width - sw;

    a = minx;
    b = (maxx - minx)*100/sw;

    SetViewport((mouse.x*b)/100+a, GetViewportY());

  }
  else ReleaseViewport();


Edit: beaten by Akatosh
My code doesn't scroll in y direction but allows the mouse to go to the screen borders.

ddq

Actually, neither worked perfectly, but after synthesizing the best of both methods, I was able to create . . .


Absolutely nothing!

Well, maybe a little bit. I fixed a few issues with your code, but created some new ones.

Here's a video showing how it works right now; the password is ags:
http://vimeo.com/6370029

The main problem is that it's going all the way to the room edge and pushing the character off screen. Furthermore, the "active panning zone" seems to be only on the left half of the screen, (i.e., the origin of the x-axis for the camera panning is at 1/4 the screen width rather than 1/2). As seen in the video, it also tends to screw up near screen edges.

I also made the camera not pan while the character is walking.

Here is my current, NOT COMPLETELY WORKING code:
Code: ags

  //repeatedly_excecute
  int minx, maxx;
  int margin = 20;
  int sw = System.ViewportWidth/2;
  int a,b;

  if (mouse.Mode == eModeLook && player.Moving == false) {

    minx = sw - player.x + margin;
    if (minx < 0) minx = 0;
    
    maxx = player.x + sw - margin;
    if (maxx > Room.Width - sw + player.x) maxx = Room.Width - sw;

    a = minx;
    b = ((maxx - minx)*100)/sw;

    SetViewport((mouse.x*b)/100 + a, GetViewportY());
  }
  else ReleaseViewport();


Khris

#4
I tested this using a 320 game and it worked fine. Actually, I was afraid you'd use a highres game and that things wouldn't work as they should. I did check all the relevant commands for whether they use 320 units or highres coords but I must have overlooked something.

EDIT: Hmm, I tried it with a 640 game and my code does work exactly like it should.
EDIT2: 800 also.
EDIT3: http://www.youtube.com/watch?v=DE_jC7Zr0T8

ddq

#5
Interesting. It might be that with the size of your room and character, it works, but because my character takes up more of the room and my rooms are larger, it scrolls all the way to the room edge. Just guessing here. I'll keep trying some stuff, but aside from scrolling past the character, your code works correctly.

EDIT: Actually, it seems that scrolling to the left works perfectly, only scrolling to the right goes all the way to the edge of the room. The plot thickens!

Khris

You have put a "/2" into the declaration of dw and changed minx. I assume you changed those after trying out my original code? :=

I'm asking because neither the character's size nor the room width should matter.

ddq

I did try a /2 at one point and I think it ended up in my posted code. But as of the last post, I am using your unaltered code.

SMF spam blocked by CleanTalk