   Problem with Mouse Movements
  I posted this in the Beginners Forum than realized it might get answered here since it is basically DX related, so here it is copy and pasted.Hello, I am fairly new to Direct X and C# game programming and I am currently working on making my first small game that requires tracking mouse movements.The problem I am having is that with my if statements, it is only performing the first one. If I comment the if statement out above it the next one works fine in my 3D world. Basically I am trying to make it so the Camera's LookAt target is updated as I move the mouse (much like it does in several FPS).Here is the function I wrote that is being called at the top of my render function;public void readMouseInput()   {  Vector3 Move;  if (frm.mice.CurrentMouseState.X > 0)  { Move = frm.CameraLookAt; Move.X -= 1f; Move = new Vector3(Move.X, Move.Y, Move.Z); frm.CameraLookAt = Move;  }  if (frm.mice.CurrentMouseState.X < 0)  { Move = frm.CameraLookAt; Move.X += 1f; Move = new Vector3(Move.X, Move.Y, Move.Z); frm.CameraLookAt = Move;  }  if (frm.mice.CurrentMouseState.Y > 0)  { Move = frm.CameraLookAt; Move.Y -= 1f; Move = new Vector3(Move.X, Move.Y, Move.Z); frm.CameraLookAt = Move;  }  if (frm.mice.CurrentMouseState.Y < 0)  { Move = frm.CameraLookAt; Move.Y += 1f; Move = new Vector3(Move.X, Move.Y, Move.Z); frm.CameraLookAt = Move;  }   }This is the override function for the mouseMove which is having the same problem as above; protected override void OnMouseMove(MouseEventArgs e)   {  //if (0 != (e.X | e.Y))  //{ if (e.X >= 1)cameraLookAt.X -= 1f; if (e.X <= -1)cameraLookAt.X += 1f; if (e.Y >= 1)cameraLookAt.Y += 1f; if (e.Y <= -1)cameraLookAt.Y -= 1f;  //}  base.OnMouseMove(e);   }I believe the problem has something to do with the fact that the rendered calls the function so fast it only has time to perform the first If statements...but that makes me think there would be a memory leak and stack overflow which there aren't. Any suggestions would be great thank you ahead of time.   
  First of all, if you really want a fps camera you should rotate the target(look at) around the camera, not just move it. For simple camera, the X displacement of the mouse cursor incr/decrease the yaw and the Y displacement incr/decrease the pitch of the camera. Then, declare 2 floats yaw and pitch to store these values. At each update of your camera, compute the x and y displacement of the mouse cursor and do yaw -= mouseDispX*sensitivity and pitch -= mouseDispY*sensitivity where sensitivity is a float(a value of 0.018f may work well, experiment)(note: if the -= doesn't bevahe correctly change to +=). Now that you have the yaw and pitch, its just compute the target(or look at) position like this:cameraLookAt.X = cosf(yaw)*cosf(pitch);cameraLookAt.Y = sinf(pitch);cameraLookAt.Z = sinf(yaw)*cosf(pitch);The target position is relative to the camera position here. And this is good because you need to move in the direction the target is pointing. With this you get a 'free-look' camera:if(moveForward)    cameraPosition += cameraSpeed * camera.LookAt * dt;if(moveBackward)    cameraPosition -= cameraSpeed * camera.LookAt * dt;//put the target at the correct positioncameraLookAt += cameraPosition;Where cameraPosition is the camera position (a Vector3), cameraSpeed is a float and dt is the delta time from the last frame to this frame which is also a float. Then, to finally build the view matrix:D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &cameraLookAt, &cameraUp);I've written it in C++ but I think you can translate it into C#(I'm not a C# programmer :P ). Hope this helps.   ;  Unfortunately I am very new to camera's and DirectX all together so I am afraid I do not completely understand what you are saying but I am going to fiddle with it and see what I can do. Thank you very much for your assistance.   ;  So I have done a few hours of research and this is what I can make of your code xissburg;public void readMouseInput3()   {  MouseState currentMouseState = frm.mice.CurrentMouseState;  float pitch = currentMouseState.Y - previousMouseState.Y;  float yaw = currentMouseState.X - previousMouseState.X;  float sensitivity = 0.018f;  Vector3 temp;  temp = frm.CameraLookAt;  if (currentMouseState.X != previousMouseState.X)  { yaw += currentMouseState.X * sensitivity;  }  if (currentMouseState.Y != previousMouseState.Y)  { pitch += currentMouseState.Y * sensitivity;  }  temp.X = (float)( Math.Cos(yaw) * Math.Cos(pitch));  temp.Y = (float)(Math.Sin(pitch));  temp.Z = (float)(Math.Sin(yaw) * Math.Cos(pitch));  frm.CameraLookAt = new Vector3(temp.X, temp.Y, temp.Z);  cam.updateCamera();   }unfortunately I must be missing a part of it because when I move the mouse around on my scene it just kind of vibrates the scene, I am thinking it might have something to do with something else in my code resetting the camera but I may be missing something from your description as well. As for the Keyboard stuff this is what I could make of it so far (still needs work on my half);private float cameraSpeed;   private float dt = System.DateTime.Now.Millisecond;   public void readKeyInput2()   {  KeyboardState keys = di.GetCurrentKeyboardState();  if (keys[Key.Up])  { frm.CameraPosition += cameraSpeed * frm.CameraLookAt * dt;  }  if (keys[Key.Back])  { frm.CameraPosition -= cameraSpeed * frm.CameraLookAt * dt;  }  frm.CameraLookAt += frm.CameraPosition;   }Any suggestions would be fantastic and greatly appreciated...especially about the mouse part.Thank you in advance :)   ;It seems that the only wrong thing was that you were not assigning the current mouse state to the previous. (use [ source ][ /source ](without the spaces) tags when posting code):public void readMouseInput3(){    MouseState currentMouseState = frm.mice.CurrentMouseState;    float pitch = currentMouseState.Y - previousMouseState.Y;    float yaw = currentMouseState.X - previousMouseState.X;    //assign the current to the previous or you'll have wrong displacement    //values in the next frame    previousMouseState.X = currentMouseState.X;    previousMouseState.Y = currentMouseState.Y;    //its not a good idea to declare this as local. Make this a property of your    //class, or your Camera class    float sensitivity = 0.018f;    if (currentMouseState.X != previousMouseState.X)   yaw += currentMouseState.X * sensitivity;    if (currentMouseState.Y != previousMouseState.Y)   pitch += currentMouseState.Y * sensitivity;    //sorry but I like to do things somewhat 'compact' :P    frm.CameraLookAt = new Vector3((float)( Math.Cos(yaw) * Math.Cos(pitch)),(float)(Math.Sin(pitch)),(float)(Math.Sin(yaw) * Math.Cos(pitch)));    cam.updateCamera();}And for moving://also declare these as global for the classprivate float cameraSpeed = 2.f;private float time = System.DateTime.Now.Millisecond;...//multiply by 0.001f to get the time in secondsfloat t = System.DateTime.Now.Millisecond*0.001f;//get the deltatimefloat dt = t - time;//assign the current to the previoustime = t;public void readKeyInput2(){    KeyboardState keys = di.GetCurrentKeyboardState();    if (keys[Key.Up])   frm.CameraPosition += cameraSpeed * frm.CameraLookAt * dt;    if (keys[Key.Back])   frm.CameraPosition -= cameraSpeed * frm.CameraLookAt * dt;    frm.CameraLookAt += frm.CameraPosition;}Now I think it works correctly. Try that out and let me know.   ;  Thank you again Xissburg, you have been a great help. Unfortunately the code still doesn't work and after spending quite some time looking through the rendering process I have noticed that once the mouse moves, the camera's Target is updated but it returns to the default target location after that frame. I am not sure why this is occurring because after the program loads the camera's default is never called again. Due to this, this is what is causing the vibration look when I continue to move the mouse. I am glad that you pointed out that I was missing the assignment steps because that would have just caused issue's down the line, aswell Thank you for showing me how to make "correct" script blocks in posts...I was curious how.This is my simple update Camera function which I have been using and I believe is the translation for what stated aswell; "D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &cameraLookAt, &cameraUp);"The only difference is my LookAtLH only takes on 3arguments.public void updateCamera()   {  frm.getGraphics.Transform.View = Matrix.LookAtLH(frm.CameraPosition, frm.CameraLookAt, frm.CameraUpFactor);   }Again any help would be great, already what you have shown me is fantastic.   ;Quote://put the target at the correct positioncameraLookAt += cameraPosition;I think you forgot to what I mentioned in my first post. The cameraLookAt is the 'target position relative to the camera position' and it is a unity length vector. If you skip this step, the target vector(thinking about it as a point) will always be on the surface of a sphere of radius 1 with its center at the origin. Then you can do this:public void updateCamera(){    frm.getGraphics.Transform.View = Matrix.LookAtLH(frm.CameraPosition, frm.CameraPosition + frm.CameraLookAt,  frm.CameraUpFactor);}When you sum the CameraPosition to the CameraLookAt, the target will now be at its correct position  for the world. Hope it works correctly now. Let me know.   ;  I think that somewhat corrected the problem, from what I can see it appears that the view frustrum is now being updated by the mouse. I say what I can see because now that my update camera function looks like this;[script] public void updateCamera()   {  frm.getGraphics.Transform.View = Matrix.LookAtLH(frm.CameraPosition, frm.CameraPosition + frm.CameraLookAt, frm.CameraUpFactor);   }[/script]....The screen flickers. If I don't move the mouse there are no images present but if I move it, my meshes and what not flicker on then off again once the frame exits. Sorry for being such a pain about this but like I said I really don't understand camera's and what not yet, I just finished learning how to build UI's, worlds etc. Anyway your thoughts are greatly appreciated Xissburg, thank you very much for your help thus far.Oh yes I almost forgot; Quote://put the target at the correct positioncameraLookAt += cameraPosition; This line of code is at the end of my inputKeyBoard function.   ;  Not sure about what may be happening. If you post more code I think it'll be easier to help. Here is the code of a simple Camera class I did some time ago. It uses the same principles discussed here(its DirectX, C++ and Raw Input)://this func handles input. It updates the yaw and pitch and the key flagsvoid CAMERA::HandleInput(LPARAM lParam){UINT dwSize = 0;GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));BYTE* lpb = new BYTE[dwSize];if(dwSize != GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)))OutputDebugString(L"GetRawInputData() did not return the correct size.");RAWINPUT* raw = (RAWINPUT*)lpb;if(raw->header.dwType == RIM_TYPEKEYBOARD){RAWKEYBOARD* rkb = &raw->data.keyboard;if(rkb->Message == WM_KEYDOWN){DWORD key = (DWORD)rkb->VKey;if(key == ctrlKey[CK_MOVEFORWARD])ctrlCombFlags |= ctrlFlags[CK_MOVEFORWARD];if(key == ctrlKey[CK_MOVEBACKWARD])ctrlCombFlags |= ctrlFlags[CK_MOVEBACKWARD];if(key == ctrlKey[CK_STRAFELEFT])ctrlCombFlags |= ctrlFlags[CK_STRAFELEFT];if(key == ctrlKey[CK_STRAFERIGHT])ctrlCombFlags |= ctrlFlags[CK_STRAFERIGHT];}else if(rkb->Message == WM_KEYUP){DWORD key = (DWORD)rkb->VKey;if(key == ctrlKey[CK_MOVEFORWARD])ctrlCombFlags &= ~ctrlFlags[CK_MOVEFORWARD];if(key ==  ctrlKey[CK_MOVEBACKWARD])ctrlCombFlags &= ~ctrlFlags[CK_MOVEBACKWARD];if(key == ctrlKey[CK_STRAFELEFT])ctrlCombFlags &= ~ctrlFlags[CK_STRAFELEFT];if(key == ctrlKey[CK_STRAFERIGHT])ctrlCombFlags &= ~ctrlFlags[CK_STRAFERIGHT];if(key == VK_ESCAPE)PostQuitMessage(0);}}   //here goes the yaw and pitch updateelse if(raw->header.dwType == RIM_TYPEMOUSE){RAWMOUSE* rm = &raw->data.mouse; //since raw input gives me the displacement, theres no need to use that 'current - previous'yaw -= float(rm->lLastX)*sensitivity;pitch -= float(rm->lLastY)*sensitivity;if(rm->usButtonFlags == RI_MOUSE_WHEEL)roll -= float(rm->usButtonData)*sensitivity;}delete[] lpb;}//this func updates the camera position based on the pressed keys and the target directionvoid CAMERA::UpdateCamera(){   //clamp the pitch so that -0.5*pi < pitch < 0.5*piif(pitch >  1.56f) pitch =  1.56f;else if(pitch < -1.56f) pitch = -1.56f;   //the velocity is recomputed then turn it into a 0 vectorvelocity = D3DXVECTOR3(0.f, 0.f, 0.f);   //compute the target   target.x = cosf(yaw)*cosf(pitch);target.y = sinf(pitch);target.z = sinf(yaw)*cosf(pitch);   //check the pressed keys and perform the proper operationif(ctrlCombFlags & ctrlFlags[CK_MOVEFORWARD])velocity += target;if(ctrlCombFlags & ctrlFlags[CK_MOVEBACKWARD])velocity -= target;   //here I implement strafing. just cross the camera up vector with the target and you get the strafing vectorif(ctrlCombFlags & ctrlFlags[CK_STRAFERIGHT]){D3DXVECTOR3 strafeAxis;D3DXVec3Cross(&strafeAxis, &up, &target);D3DXVec3Normalize(&strafeAxis, &strafeAxis);velocity += strafeAxis;}if(ctrlCombFlags & ctrlFlags[CK_STRAFELEFT]){D3DXVECTOR3 strafeAxis;D3DXVec3Cross(&strafeAxis, &up, &target);D3DXVec3Normalize(&strafeAxis, &strafeAxis);velocity -= strafeAxis;}   //after this, the velocity vector will be the sum of all velocities(longitudinal and lateral velocities)   //get the delta time in secondsstatic long time = timeGetTime();long t = timeGetTime();float dt = (t-time)*0.001f;time = t;   //normalize the velocity so that when moving both forward and strafing to the right for example the velocity is not increasedD3DXVec3Normalize(&velocity, &velocity);   //scale the unit length vector velocity by the speed of the cameravelocity *= speed;   //integrate the positionposition += velocity*dt;   //set the target at the correct position for the worldtarget += position;   //create the view matrixD3DXMatrixLookAtLH(&view, &position, &target, &up);}I suspect that there may be something in incorrect order in your code, not sure. Maybe basing in my code you can understand that better. Hope this helps and let me know what happened :P cya   ;  Hey Xissburg thank you again for another reply. I am a little frustrated about other things right now so I unfortunately cannot really undstand your code but I will go through it tomorrow. There may be something wrong with my order of operations but on the other hand I don't know because if my camera looks like this; frm.getGraphics.Transform.View = Matrix.LookAtLH(frm.CameraPosition, frm.CameraLookAt, frm.CameraUpFactor);It is wrong but my terrain and Meshes are shown but when you move the mouse the scene appears to vibrate. As soon as I make my Update Camera's look at = frm.CameraPosition + frm.CameraLookAT....the scene updates correctly while moving the mouse but like I said the meshes/terrain are not rendered while the mouse is not moving and blink when when the mouse is moving. My mouse input function now looks like this which I have gone through and it appears to be correct. public void readMouseInput3()   {  MouseState currentMouseState = frm.mice.CurrentMouseState;  float pitch = currentMouseState.Y - previousMouseState.Y;  float yaw = currentMouseState.X - previousMouseState.X;  //assign the current to previous  previousMouseState = currentMouseState;  Vector3 temp;  temp = frm.CameraLookAt;  if (currentMouseState.X != previousMouseState.X) yaw += currentMouseState.X * sensitivity;  if (currentMouseState.Y != previousMouseState.Y) pitch += currentMouseState.Y * sensitivity;  temp.X = (float)( Math.Cos(yaw) * Math.Cos(pitch));  temp.Y = (float)(Math.Sin(pitch));  temp.Z = (float)(Math.Sin(yaw) * Math.Cos(pitch));  frm.CameraLookAt = new Vector3(temp.X, temp.Y, temp.Z);  cam.updateCamera();   } Anyway I appreciate your help so much and I will examine your c++ class tomorrow. I know some c++ (only been writting it for about 3months, so that may be some insight to how much some is haha) but unfortunately I do not know any of DirectX for c++. Like I said I will run through your code tomorrow.Oh before I forget as well here is how simple my rendering process is, I am just overring the Paint control of the windows form(I am still a noob so this is the way I do it for now). protected override void OnPaint(PaintEventArgs e)   {  //actions.readKeyInput(); //actions.readMouseInput();//cam.updateCamera();  actions.readKeyInput2(); actions.readMouseInput3();   this.Text = string.Format(FrameRate.CalculateFrameRate().ToString());  d3d.Clear(ClearFlags.Target, Color.Yellow, 1.0f, 0);  d3d.BeginScene();  #region Meshes  twoMach1.renderAllMeshes();  #endregion  d3d.EndScene();  d3d.Present();  this.Invalidate();   }Thank you again for all your help.   