Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. package robotrace;
  2.  
  3. /**
  4. * Implementation of a camera with a position and orientation.
  5. */
  6. class Camera {
  7.  
  8. /** The position of the camera. */
  9. public Vector eye = new Vector(10f, 6f, 5f);
  10.  
  11. /** The point to which the camera is looking. */
  12. public Vector center = Vector.O;
  13.  
  14. /** The up vector. */
  15. public Vector up = Vector.Z;
  16.  
  17. /**
  18. * Updates the camera viewpoint and direction based on the
  19. * selected camera mode.
  20. */
  21. public void update(GlobalState gs, Robot focus) {
  22.  
  23. switch (gs.camMode) {
  24.  
  25. // First person mode
  26. case 1:
  27. setFirstPersonMode(gs, focus);
  28. break;
  29.  
  30. // Default mode
  31. default:
  32. setDefaultMode(gs);
  33. }
  34. }
  35.  
  36. /**
  37. * Computes eye, center, and up, based on the camera's default mode.
  38. */
  39. private void setDefaultMode(GlobalState gs) {
  40.  
  41. //set the center to the according GlobalState central point
  42. this.center = gs.cnt;
  43.  
  44. //the vector we will use to find the eye position
  45. Vector helpVectorX = Vector.X;
  46.  
  47. //projection vector on XY plane
  48. Vector projectionVectorXY = this.rotateVectorCC(helpVectorX, up.normalized(), gs.theta);
  49.  
  50. //normal vector of Z-projectionVectorXY Plane
  51. Vector normalZPr = projectionVectorXY.cross(up);
  52.  
  53. //unit vector towards camera
  54. Vector eyeDirection = this.rotateVectorCC(projectionVectorXY, normalZPr.normalized(), gs.phi).normalized();
  55.  
  56. //normal vector according to which the second rotation has to be done
  57. this.eye = eyeDirection.scale(gs.vDist);
  58.  
  59. }
  60.  
  61. //does the rotation calculations
  62. private Vector rotateVectorCC(Vector vec, Vector axis, double theta){
  63. double x, y, z;
  64. double u, v, w;
  65.  
  66. x = vec.x(); y = vec.y(); z = vec.z();
  67. u = axis.x(); v = axis.y(); w = axis.z();
  68.  
  69. double xPrime = u * (u*x + v*y + w*z)*(1d - Math.cos(theta))
  70. + x*Math.cos(theta)
  71. + (-w*y + v*z)*Math.sin(theta);
  72.  
  73. double yPrime = v*(u*x + v*y + w*z)*(1d - Math.cos(theta))
  74. + y*Math.cos(theta)
  75. + (w*x - u*z)*Math.sin(theta);
  76.  
  77. double zPrime = w*(u*x + v*y + w*z)*(1d - Math.cos(theta))
  78. + z*Math.cos(theta)
  79. + (-v*x + u*y)*Math.sin(theta);
  80.  
  81. return new Vector(xPrime, yPrime, zPrime);
  82. }
  83.  
  84.  
  85. /**
  86. * Computes eye, center, and up, based on the first person mode.
  87. * The camera should view from the perspective of the robot.
  88. */
  89. private void setFirstPersonMode(GlobalState gs, Robot focus) {
  90.  
  91. //normalized vector pointing at the robot position
  92. //Vector robotVector = new Vector(focus.xcord, focus.ycord, focus.zcord).normalized();
  93. //vector which represents the normalized eye-vision of the robot
  94. Vector n = focus.direction.normalized();
  95.  
  96. //vector which represents the actual eye-vision of the robot
  97. Vector eyeDirection = new Vector(n.x() + focus.xcord, n.y() + focus.ycord, n.z() + focus.ROBOT_HEIGHT + 1);
  98.  
  99. //vector for the new center calculation which has the center in front of the robot
  100. Vector cV = n.scale(gs.vDist);
  101.  
  102. //coordinates of the new center
  103. this.center = new Vector(cV.x() + focus.xcord, cV.y() + focus.ycord, cV.z() + focus.ROBOT_HEIGHT + 1);
  104.  
  105. //new camera position
  106. this.eye = eyeDirection;
  107.  
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement