Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package robotrace;
- /**
- * Implementation of a camera with a position and orientation.
- */
- class Camera {
- /** The position of the camera. */
- public Vector eye = new Vector(10f, 6f, 5f);
- /** The point to which the camera is looking. */
- public Vector center = Vector.O;
- /** The up vector. */
- public Vector up = Vector.Z;
- /**
- * Updates the camera viewpoint and direction based on the
- * selected camera mode.
- */
- public void update(GlobalState gs, Robot focus) {
- switch (gs.camMode) {
- // First person mode
- case 1:
- setFirstPersonMode(gs, focus);
- break;
- // Default mode
- default:
- setDefaultMode(gs);
- }
- }
- /**
- * Computes eye, center, and up, based on the camera's default mode.
- */
- private void setDefaultMode(GlobalState gs) {
- //set the center to the according GlobalState central point
- this.center = gs.cnt;
- //the vector we will use to find the eye position
- Vector helpVectorX = Vector.X;
- //projection vector on XY plane
- Vector projectionVectorXY = this.rotateVectorCC(helpVectorX, up.normalized(), gs.theta);
- //normal vector of Z-projectionVectorXY Plane
- Vector normalZPr = projectionVectorXY.cross(up);
- //unit vector towards camera
- Vector eyeDirection = this.rotateVectorCC(projectionVectorXY, normalZPr.normalized(), gs.phi).normalized();
- //normal vector according to which the second rotation has to be done
- this.eye = eyeDirection.scale(gs.vDist);
- }
- //does the rotation calculations
- private Vector rotateVectorCC(Vector vec, Vector axis, double theta){
- double x, y, z;
- double u, v, w;
- x = vec.x(); y = vec.y(); z = vec.z();
- u = axis.x(); v = axis.y(); w = axis.z();
- double xPrime = u * (u*x + v*y + w*z)*(1d - Math.cos(theta))
- + x*Math.cos(theta)
- + (-w*y + v*z)*Math.sin(theta);
- double yPrime = v*(u*x + v*y + w*z)*(1d - Math.cos(theta))
- + y*Math.cos(theta)
- + (w*x - u*z)*Math.sin(theta);
- double zPrime = w*(u*x + v*y + w*z)*(1d - Math.cos(theta))
- + z*Math.cos(theta)
- + (-v*x + u*y)*Math.sin(theta);
- return new Vector(xPrime, yPrime, zPrime);
- }
- /**
- * Computes eye, center, and up, based on the first person mode.
- * The camera should view from the perspective of the robot.
- */
- private void setFirstPersonMode(GlobalState gs, Robot focus) {
- //normalized vector pointing at the robot position
- //Vector robotVector = new Vector(focus.xcord, focus.ycord, focus.zcord).normalized();
- //vector which represents the normalized eye-vision of the robot
- Vector n = focus.direction.normalized();
- //vector which represents the actual eye-vision of the robot
- Vector eyeDirection = new Vector(n.x() + focus.xcord, n.y() + focus.ycord, n.z() + focus.ROBOT_HEIGHT + 1);
- //vector for the new center calculation which has the center in front of the robot
- Vector cV = n.scale(gs.vDist);
- //coordinates of the new center
- this.center = new Vector(cV.x() + focus.xcord, cV.y() + focus.ycord, cV.z() + focus.ROBOT_HEIGHT + 1);
- //new camera position
- this.eye = eyeDirection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement