scrolling math
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.
my drawing method does glTranslatef(mainCamera.x, mainCamera.y, 0); right after the glLoadIdentity
what is the math i have to do to mainCamera?
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?
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:
- Alex Diener
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
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?
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?

