Implementing mouse look in my 3D Engine

Started by
26 comments, last by snisarenko 21 years, 3 months ago
Its Time to solve a mystery ..........
Ok so to get down to the bottom of this problem i did something simple.

Here is my code:

case WM_MOUSEMOVE:
{
mouseRotY+=(LOWORD(lParam)-mouseCenter.x)*mouseSpeed;
mouseRotX+=(HIWORD(lParam)-mouseCenter.y)*mouseSpeed;
return 0;
}

.... in my main program

SwapBuffers(hDC);
RenderSkyBox();
SetCursorPos(mouseCenter.x, mouseCenter.y);


Ok so what would you expect this program to do? Nothing, Right ?
Since I am allways reseting it to single position. But wait if i move the mouse maybe somewhere between case WM_MOUSEMOVE and SetCursorPos I could move my mouse and maybe in a little millisecond mouse motion will get to RenderSkyBox(); and rotate it a little, Right ? So if I run my programm and I don''t touch my mouse we are garanteed that the no rotation will happen, right?

Well ........ I run my program and I don''t touch my mouse.... gues what happens?......
For some weird reason my skybox is spining uppwards and only upwars and at very high speed.

Now could somebody explain why the hell this is happening ?
Advertisement
like i said before forget about WM_MOUSEMOVE. dellete it! all you need is this:

  POINT mousepPosition;  ...  SwapBuffers(hDC); // Swap Buffers (Double Buffering)  RenderSkyBox();  GetCursorPos(&mousepPosition); // save mouse position  SetCursorPos(mouseCenter.x,mouseCenter.y); // then move it to center  mouseRotY += (mousepPosition.x-mouseCenter.x)*mouseSpeed;  mouseRotX += (mousepPosition.y-mouseCenter.y)*mouseSpeed;  .. key stuff ..


just try it. is''t not that hard.
- JonoRule #1: If you build it, they will come -- to hack and cheat.
Thanks, I used your code and it worked perfectly.

But see i am still restless. I want to find out why my previous way didn't work

I just can't abandon it ........ it is stuck in my mind

[edited by - snisarenko on December 28, 2002 6:51:47 PM]
Didn''t read everything, but I have a question. How can a WndProc be called recursively when it''s necessary to call DispatchMessage to forward a message to the WndProc? Or do non-window specific messages get sent straight to the WndProc? That seems really suspicious, but perhaps it''s just me.

Also, GetCursorPos is lovely for things like this, as some suggested.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

ok. lets see if we can make some sense out of all this!

first things firat.
case WM_MOUSEMOVE:{  mouseRotY+=(LOWORD(lParam)-mouseCenter.x)*mouseSpeed;  mouseRotX+=(HIWORD(lParam)-mouseCenter.y)*mouseSpeed;  SetCursorPos(mouseCenter.x,mouseCenter.y);  return 0;}


this will be called recusively blocking the rest of the program from running. that is, every time SetCursorPos() is called a WM_MOUSEMOVE message is generated.

case WM_MOUSEMOVE:{  MouseX = HIWORD(lParam);  MouseY = LOWORD(lParam);  if(MouseX != MouseCenter.x || MouseY != MouseCenterY)  {    MouseRotX += (MouseX - MouseCenter.y) * MouseSpeed;    MouseRotY += (MouseY - MouseCenter.x) * MouseSpeed;    SetCursorPos(MouseCenter.x, MouseCenter.y);  }  return 0;}


this will stop the recursion as SetCursorPos() is only called if the mouse has moved from where it was put. however a WM_MOUSEMOVE message is sent every time the mouse is moved one pixel which can slow down things a bit if the mouse is moving fast.

the code by Steve132 will not work because there is no SetCursorPos() call made.

xiros code sould work but why use:
case WM_MOUSEMOVE:{  mouseY = LOWORD(lParam);  mouseX = HIWORD(lParam);  return 0;}


when you could just call GetCursorPos() ???

this has all been very confusing as there has been a lot of small misunderstandings and assumptions.

its good that everyone tried to help but i think maby a bit more detail in explanations and a bit more related code would realy help at times.</lecture>


- Jono AH
- JonoRule #1: If you build it, they will come -- to hack and cheat.
snisarenko:

mouseRotY+=(LOWORD(lParam)-mouseCenter.x)*mouseSpeed;
mouseRotX+=(HIWORD(lParam)-mouseCenter.y)*mouseSpeed;

You''re using x-position for y-movement. Is that intentional?

Ok... i think I understood... rotation around X and Y, right?

What I would suggest is to log these mouseRot values to a file...

Sorry about earlier m8...


AP : yep, it''s an optimization, not from a speed but from a precision point of view. For very slow cursor movements, you''ll get funny behavior if you put back the cursor in the middle each time. You might be setting it to (0,0) when it was at (1.5, 1.5), losing (0.5, 0.5) precision. I know there''s no "true" floating-point mouse movement, but I noticed jerky movement using that solution, that disappears if you only setcursorpos when out of a rectangle.

And also, this way you avoid asking the API to move the cursor, AND to process another mousemove message.

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more
''kay guys, just one question:
why did noone refer to NeHe''s tutorial 23?
i think it covers our problem quite all right...
(for those who do not know it: it uses the point - GetCursorPos - SetCursorPos - thing)
Greetings, SnAkE.

SnAkE''s Programming Resources
Ok everyone I already solved the problem with GetCursorPos & SetCursorPos.

Just for the hell of it did anyone figure out the mistery I posted on the previous page.

This topic is closed to new replies.

Advertisement