scrolling math

Member
Posts: 102
Joined: 2005.01
Post: #1
I am trying to make a game that scrolls when you drag the board, so if you drag down say the board scrolls up, so the board comes with the mouse when you are dragging.

When you drag the board, the bottom left side of the board jumps too where the mouse is and it drags like i want it to, how can keep it from jumping to the mouse?

the math is simple here is my drag method.

Code:
- (void)mouseDragged:(NSEvent *)event
{    
    NSPoint loc = [event locationInWindow];
    int x = loc.x;
    int y = loc.y;
    
    mainCamera.y = y;
    mainCamera.x = x;
    
    [self setNeedsDisplay:YES];
}


my drawing method does glTranslatef(mainCamera.x, mainCamera.y, 0); right after the glLoadIdentity

what is the math i have to do to mainCamera?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Simple. On mouseDown, save the difference between the mouse location and the camera's origin. On mouseMoved, add the saved difference to your new camera origin coordinates.

Pseudocode:

Code:
int offsetX, offsetY;

void mouseDown() {
  offsetX = (cameraOrigin.x - mouseLoc.x);
  offsetY = (cameraOrigin.y - mouseLoc.y);
}

void mouseMoved() {
  cameraOrigin.x = (mouseLoc.x + offsetX);
  cameraOrigin.y = (mouseLoc.y + offsetY);
}

- Alex Diener
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #3
thanks that works beautifically

o wait for out-of-bounds protection do i just make sure that mainCamera.x/y is greater than 0 and less than the width/height of the game board?
Quote this message in a reply
Post Reply