Reputation: 203
I wrote a mouse function for the camera. It works good. But the only problem is at the start, when I do first move with mouse it jumps up-left and then it works good. I don't understand why it happens.
Here the mouse function.
float lastx = 0.0;
float lasty = 0.0;
void mouseMovement(int x, int y)
{
lastx = (float)x - lastx;
lasty = (float)y - lasty;
if((float)x > lastx)
Camera.RotateY((-lastx)*0.01);
else
Camera.RotateY(lastx*0.01);
if((float)y > lasty)
Camera.RotateX(lasty*0.01);
else
Camera.RotateX(-lasty*0.01);
lastx = (float)x;
lasty = (float)y;
Display();
}
Upvotes: 1
Views: 9393
Reputation: 2836
I'm guessing, If you're tracking all mouse movement you'll need to either catch when the mouse enters or leaves the window, or simply discard any jumps larger than a certain amount.
Let's say that the mouse leaves the right edge of the window, and reenters the left edge of the window. The code you've shown is going to jump.
Unless there's a reason not to, I'd make the last variables static and add an if statement similar to this(not tested :) )...
void mouseMovement(int x, int y)
{
static float lastx = 0.0;
static float lasty = 0.0;
lastx = (float)x - lastx;
lasty = (float)y - lasty;
if((abs((int)lastx)>10)||(abs((int)lasty)>10)
{
lastx = (float)x;
lasty = (float)y;
return;
}
//the rest remains the same
The logic goes... Any event it sees, that isn't near the last event it saw, just resets the variables and waits for another event.
That really isn't an elegant solution for a mouse control, but at this point in your coding it is probably understandable, and will do what you want.
Upvotes: 1
Reputation: 39400
Move the cursor to the middle of the screen before you start accepting data. And ensure that lastx
and lasty
are properly initialized.
I am 90% sure it's FPP camera, and you move the cursor to the middle of the screen manually. So it happens it starts in a down-right corner of the window, then is moved, which reports as a delta. It can also be the fact that lastx
and lasty
are initialized on first read, and prior to that they contain garbage.
Upvotes: 1