Advertisement
Dimenticare

Game Maker Mouse to 3D Vector

Nov 21st, 2018
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// void update_mouse_vector(xfrom, yfrom, zfrom, xto, yto, zto, xup, yup, zup, fov, aspect);
  2. // Pretty suret his is an adapted version of code written by a
  3. // guy called Yourself on the game maker forums, but i can't
  4. // find the original thread.
  5. var mm,dX,dY,dZ,uX,uY,uZ,vX,vY,vZ,mX,mY,mZ,width,height,tFOV,asp;
  6. asp=argument10;
  7.  
  8. // normalize TO vector
  9. dX = argument3-argument0;
  10. dY = argument4-argument1;
  11. dZ = argument5-argument2;
  12. mm = sqrt(dX*dX+dY*dY+dZ*dZ);
  13. dX /= mm;
  14. dY /= mm;
  15. dZ /= mm;
  16.  
  17. // fix UP vector and normalize it
  18. uX = argument6;
  19. uY = argument7;
  20. uZ = argument8;
  21. mm = uX*dX+uY*dY+uZ*dZ;
  22. uX -= mm*dX;
  23. uY -= mm*dY;
  24. uZ -= mm*dZ
  25. mm = sqrt(uX*uX+uY*uY+uZ*uZ);
  26. uX /= mm;
  27. uY /= mm;
  28. uZ /= mm;
  29.  
  30.  
  31. // make x vector using TO and UP
  32. vX = uY*dZ-dY*uZ;
  33. vY = uZ*dX-dZ*uX;
  34. vZ = uX*dY-dX*uY;
  35.  
  36. // convert angle to screen width and height
  37. // not sure why this is pi/360 instead of pi/180 but that's the way
  38. // it came and i don't want to touch it
  39. tFOV = tan(argument9*pi/360);
  40. uX *= tFOV;
  41. uY *= tFOV;
  42. uZ *= tFOV;
  43. vX *= tFOV*asp;
  44. vY *= tFOV*asp;
  45. vZ *= tFOV*asp;
  46.  
  47. // add UP*MOUSE_Y and X*MOUSE_X vector to TO vector
  48. mX = dX+uX*(1-2*MOUSE_Y/HH)+vX*(2*MOUSE_X/WW-1);
  49. mY = dY+uY*(1-2*MOUSE_Y/HH)+vY*(2*MOUSE_X/WW-1);
  50. mZ = dZ+uZ*(1-2*MOUSE_Y/HH)+vZ*(2*MOUSE_X/WW-1);
  51. mm = sqrt(mX*mX+mY*mY+mZ*mZ);
  52.  
  53. // normalize mouse direction vector
  54. // if you're using gms2 just return [mX/mm, mY/mm, mZ/mm], because
  55. // array literals are nice, otherwise write this script yourself
  56. return array_compose(mX/mm, mY/mm, mZ/mm);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement