Author Topic: Moving the camera (on scrolling)  (Read 945 times)

Offline Magicorange

  • byte
  • *
  • Posts: 8
    • View Profile
Moving the camera (on scrolling)
« on: February 27, 2014, 12:36:54 pm »
Hello,

I would like to move the camera on the x/y axis using the onScroll listener, but I'm having difficults using the camera.moveCamera method :

Code: [Select]
SimpleVector touchOrigin = Interact2D.reproject2D3DWS(map.getCam(), fb, (int)xStart, (int)yStart).normalize();
SimpleVector touchEnd = Interact2D.reproject2D3DWS(map.getCam(), fb, (int)xEnd, (int)yEnd).normalize();
SimpleVector moveDist = touchEnd.calcSub(touchOrigin);
cameraMovement = moveDist;
cameraChanged = true;

The idea is to project the touch origin (xStart yStart) and the current touch position (xEnd yEnd) in the world space so I can calculate the distance between both and use it to move the camera.


Code: [Select]
if(cameraChanged)
{
map.getCam().moveCamera(cameraMovement, 2);
cameraChanged = false;
}


The thing is that I'm getting incoherent results, like the camera going upwards while I'm scrolling to the left etc.
It's probably an easy math problem but I really can't figure what exactly it is, I thought that substracting the two vectors would work but maybe I'm wrong.
I've tried to use camera.moveCamera using the already defined directions (Camera.CAMERA_MOVERIGHT, CAMERA_MOVELEFT ...) when we have xStart < or > xEnd, yStart < > yEnd but I'm getting more or less the same problem.

I'm still trying to find a solution, and I've already searched for this in other topics but I couldn't find a real solution at the moment.

Any idea on how I could do this please ?

Thanks in advance ! :)

Edit : I just noticed I've posted in the wrong section (I'm using JPCT AE !), sorry !
« Last Edit: February 27, 2014, 04:35:33 pm by Magicorange »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Moving the camera (on scrolling)
« Reply #1 on: February 27, 2014, 05:41:30 pm »
Not sure...i would expect this to work if the camera hasn't been rotated before. Has it? Apart from that, it think that it's overcomplicated to do it this way. Why don't do use the 2d delta vector instead for the camera's translation? You could simply move the camera along it's x-axis if there is a change in the 2d x value and along it's y-axis if there is one in y direction. If you are using the get?Axis()-methods in Camera for this, it even doesn't matter if the camera has been rotated or not. I don't see the need for reprojection into 3d ATM.

Offline Magicorange

  • byte
  • *
  • Posts: 8
    • View Profile
Re: Moving the camera (on scrolling)
« Reply #2 on: February 27, 2014, 05:56:03 pm »
Well, the camera hasn't been rotated, just slightly moved on its Z axis when it's been created.
But I'm not sure to get it, do you mean I should directly make a vector using the x,y of the touch positions (something like [xEnd-xStart, yEnd-yStart,0]) and add it to the X/Y axis vectors to translate the camera ?
« Last Edit: February 27, 2014, 06:11:26 pm by Magicorange »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Moving the camera (on scrolling)
« Reply #3 on: February 27, 2014, 06:10:24 pm »
No. I mean to move it by a the resulting vector of getXAxis() and getYAxis() with a speed value that is based on the delta in x and y direction. With 'based on', i mean scaled by some factor (one has to try what works best here). That's the most compatible way to do it, because it takes rotations of the camera into account, even if there are none ATM.

Offline Magicorange

  • byte
  • *
  • Posts: 8
    • View Profile
Re: Moving the camera (on scrolling)
« Reply #4 on: February 27, 2014, 06:40:20 pm »
Ok, I've tried to do as you said, it seems to be working now :)
I post my code in case someone wants to do the same thing :

Code: [Select]

deltaX = xEnd - xStart;
deltaY = yEnd - yStart;
cameraChanged = true;

Code: [Select]
@Override
public void onDrawFrame(GL10 gl) {

if(deltaX != 0)
{
SimpleVector xAxisVec = map.getCam().getXAxis();
map.getCam().moveCamera(xAxisVec, deltaX/50f);
deltaX = 0;
}

if(deltaY != 0)
{
SimpleVector yAxisVec = map.getCam().getYAxis();
map.getCam().moveCamera(yAxisVec, deltaY/50f);
deltaY = 0;
}
...
}


At first I thought it was still not working because it scrolled again the wrong way (going up/down instead of left for example), you know why ? When the listener detects a scroll event it calls the function that does the calculation with the parameters in this order :  xStart,yStart, xEnd, yEnd, and the said function was expecting the parameters in that order : xStart,xEnd,yStart,yEnd,  :o
Lol !

Anyway, thanks for your help, your method works fine, that's easier without vectors for sure (I'll retry mine anyway, just to see if the wrong parameters order had a big influence), I might post in this thread again if I have questions about camera.
I will try to set bounds for the camera, in order not to let it go outside of the map, so if I have a problem I'll ask you :)

And good job for JPCT/AE, I'm beginning with it, I find it really nice (even if I'm awkward with maths & vectors and such ^^) ;)
« Last Edit: February 27, 2014, 06:44:39 pm by Magicorange »