public static CCPoint3 Transform(CCPoint3 point, ref CCAffineTransform t) { Vector3 vec = point.XnaVector; Vector3.Transform(ref vec, ref t.xnaMatrix, out vec); return(new CCPoint3(vec)); }
public CCCamera(CCCameraProjection projection, CCSize targetVisibleDimensionsWorldspace, CCPoint3 targetInWorldspaceIn) : this(targetInWorldspaceIn) { cameraProjection = projection; if (cameraProjection == CCCameraProjection.Projection2D) { centerInWorldspace = new CCPoint3(targetInWorldspaceIn.X, targetInWorldspaceIn.Y, targetInWorldspaceIn.Z + defaultNearAndFarOrthoClipping.Near); orthographicViewSizeWorldspace = targetVisibleDimensionsWorldspace; } else { aspectRatio = targetVisibleDimensionsWorldspace.Width / targetVisibleDimensionsWorldspace.Height; centerInWorldspace = CalculatePerspectiveCameraCenter(targetVisibleDimensionsWorldspace, targetInWorldspace); /* Make sure the far clipping distance is longer than distance frame camera to target * Give ourselves a little extra distance buffer so that there's no clipping when rotating etc * * If users want to customise the near and far clipping bounds, they can do that and then call * UpdatePerspectiveCameraTargetBounds */ nearAndFarPerspectiveClipping.Far = Math.Max(Math.Abs((centerInWorldspace.Z - targetInWorldspaceIn.Z) * 3.0f), defaultNearAndFarPerspClipping.Far); } UpdateCameraMatrices(); }
public CCCamera(CCSize orthographicViewSizeWorldspaceIn, CCPoint3 cameraCenterPositionWorldspaceIn, CCPoint3 targetInWorldspaceIn) : this(cameraCenterPositionWorldspaceIn, targetInWorldspaceIn) { cameraProjection = CCCameraProjection.Projection2D; orthographicViewSizeWorldspace = orthographicViewSizeWorldspaceIn; UpdateCameraMatrices(); }
// Defaults for both 2d and 3d projections CCCamera(CCPoint3 targetInWorldspaceIn) { targetInWorldspace = targetInWorldspaceIn; nearAndFarOrthographicZClipping = defaultNearAndFarOrthoClipping; nearAndFarPerspectiveClipping = defaultNearAndFarPerspClipping; fieldOfView = defaultFieldOfView; aspectRatio = defaultAspectRatio; upDirection = new CCPoint3(Vector3.Up.X, Vector3.Up.Y, Vector3.Up.Z); }
public CCCamera(float perspectiveFieldOfViewIn, float perspectiveAspectRatioIn, CCPoint3 cameraCenterPositionWorldspaceIn, CCPoint3 targetInWorldspaceIn) : this(targetInWorldspaceIn) { cameraProjection = CCCameraProjection.Projection3D; centerInWorldspace = cameraCenterPositionWorldspaceIn; fieldOfView = perspectiveFieldOfViewIn; aspectRatio = perspectiveAspectRatioIn; UpdateCameraMatrices(); }
public void UpdatePerspectiveCameraTargetBounds(CCSize targetVisibleDimensionsWorldspaceIn, CCPoint3 targetWorldspaceIn) { cameraProjection = CCCameraProjection.Projection3D; aspectRatio = targetVisibleDimensionsWorldspaceIn.Width / targetVisibleDimensionsWorldspaceIn.Height; targetInWorldspace = targetWorldspaceIn; centerInWorldspace = CalculatePerspectiveCameraCenter(targetVisibleDimensionsWorldspaceIn, targetWorldspaceIn); UpdateCameraMatrices(); }
public CCOrbitCameraState(CCOrbitCamera action, CCNode target) : base(action, target) { AngleX = action.AngleX; AngleZ = action.AngleZ; DeltaAngleX = action.DeltaAngleX; DeltaAngleZ = action.DeltaAngleZ; DeltaRadius = action.DeltaRadius; Radius = action.Radius; // We are assuming the local camera will be centered directly at the target // => Add radius to the z position of local camera CCPoint3 fauxLocalCameraCenter = target.FauxLocalCameraCenter; fauxLocalCameraCenter.Z += Radius; target.FauxLocalCameraCenter = fauxLocalCameraCenter; //CameraCenter = fauxLocalCameraCenter; RadDeltaZ = CCMacros.CCDegreesToRadians(DeltaAngleZ); RadDeltaX = CCMacros.CCDegreesToRadians(DeltaAngleX); // Only calculate the SpericalRadius when needed. if (float.IsNaN(Radius) || float.IsNaN(AngleZ) || float.IsNaN(AngleX)) { float r, zenith, azimuth; SphericalRadius(out r, out zenith, out azimuth); if (float.IsNaN(Radius)) { Radius = r; } if (float.IsNaN(AngleZ)) { AngleZ = CCMacros.CCRadiansToDegrees(zenith); } if (float.IsNaN(AngleX)) { AngleX = CCMacros.CCRadiansToDegrees(azimuth); } } RadZ = CCMacros.CCDegreesToRadians(AngleZ); RadX = CCMacros.CCDegreesToRadians(AngleX); }
void SphericalRadius(out float newRadius, out float zenith, out float azimuth) { float ex, ey, ez, cx, cy, cz, x, y, z; float r; // radius float s; CCPoint3 cameraCenter = CameraCenter; CCPoint3 cameraTarget = CameraTarget; ex = cameraTarget.X; ey = cameraTarget.Y; ez = cameraTarget.Z; cx = cameraCenter.X; cy = cameraCenter.Y; cz = cameraCenter.Z; x = ex - cx; y = ey - cy; z = ez - cz; r = (float)Math.Sqrt(x * x + y * y + z * z); s = (float)Math.Sqrt(x * x + y * y); if (s == 0.0f) { s = float.Epsilon; } if (r == 0.0f) { r = float.Epsilon; } zenith = (float)Math.Acos(z / r); if (x < 0) { azimuth = (float)Math.PI - (float)Math.Sin(y / s); } else { azimuth = (float)Math.Sin(y / s); } newRadius = r; }
CCPoint3 CalculatePerspectiveCameraCenter(CCSize targetVisibleBounds, CCPoint3 target) { CCPoint3 newCenter = target; /* * Given our field of view and near and far clipping, need to find z position of camera center * The top coord of near bounding frustrum is y_p = - (near * y_eye) / z_eye = near * Tan(fov / 2) * Here, we want y_eye = 1/2 * bounds height * Solve for above for target z_eye * Finally, the center we're setting is in world coords (i.e. z_center = z_target - z_eye. * Note z_eye will generally have a negative value, so - z_eye > 0. */ float zEye = -(targetVisibleBounds.Height / 2.0f) * (1 / (float)Math.Tan(fieldOfView / 2.0f)); newCenter.Z -= zEye; return(newCenter); }
internal void UpdateVisibleBoundsRect() { if (Viewport == null || Camera == null || Viewport.ViewportInPixels == CCRect.Zero) { return; } if (Camera.Projection == CCCameraProjection.Projection2D && Camera.OrthographicViewSizeWorldspace == CCSize.Zero) { return; } var viewportRectInPixels = Viewport.ViewportInPixels; // Want to determine worldspace bounds relative to camera target // Need to first find z screenspace coord of target CCPoint3 target = Camera.TargetInWorldspace; Vector3 targetVec = new Vector3(0.0f, 0.0f, target.Z); targetVec = Viewport.XnaViewport.Project(targetVec, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); Vector3 topLeft = new Vector3(viewportRectInPixels.Origin.X, viewportRectInPixels.Origin.Y, targetVec.Z); Vector3 topRight = new Vector3(viewportRectInPixels.Origin.X + viewportRectInPixels.Size.Width, viewportRectInPixels.Origin.Y, targetVec.Z); Vector3 bottomLeft = new Vector3(viewportRectInPixels.Origin.X, viewportRectInPixels.Origin.Y + viewportRectInPixels.Size.Height, targetVec.Z); Vector3 bottomRight = new Vector3(viewportRectInPixels.Origin.X + viewportRectInPixels.Size.Width, viewportRectInPixels.Origin.Y + viewportRectInPixels.Size.Height, targetVec.Z); // Convert screen space to worldspace. Note screenspace origin is in topleft part of viewport topLeft = Viewport.XnaViewport.Unproject(topLeft, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); topRight = Viewport.XnaViewport.Unproject(topRight, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); bottomLeft = Viewport.XnaViewport.Unproject(bottomLeft, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); bottomRight = Viewport.XnaViewport.Unproject(bottomRight, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); CCPoint topLeftPoint = new CCPoint(topLeft.X, topLeft.Y); CCPoint bottomLeftPoint = new CCPoint(bottomLeft.X, bottomLeft.Y); CCPoint bottomRightPoint = new CCPoint(bottomRight.X, bottomRight.Y); visibleBoundsWorldspace = new CCRect( bottomLeftPoint.X, bottomLeftPoint.Y, (int)((bottomRightPoint.X - bottomLeftPoint.X)), (int)((topLeftPoint.Y - bottomLeftPoint.Y))); anchorPointInPoints = new CCPoint(visibleBoundsWorldspace.Size.Width * AnchorPoint.X, visibleBoundsWorldspace.Size.Height * AnchorPoint.Y); }
void UpdateVisibleBoundsRect() { if (Camera == null) { return; } if (Camera.Projection == CCCameraProjection.Projection2D && Camera.OrthographicViewSizeWorldspace == CCSize.Zero) { return; } // Want to determine worldspace bounds relative to camera target // Need to first find z screenspace coord of target CCPoint3 target = Camera.TargetInWorldspace; Vector3 targetVec = new Vector3(0.0f, 0.0f, target.Z); targetVec = Viewport.Project(targetVec, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); Vector3 topLeft = new Vector3(Viewport.X, Viewport.Y, targetVec.Z); Vector3 topRight = new Vector3(Viewport.X + Viewport.Width, Viewport.Y, targetVec.Z); Vector3 bottomLeft = new Vector3(Viewport.X, Viewport.Y + Viewport.Height, targetVec.Z); Vector3 bottomRight = new Vector3(Viewport.X + Viewport.Width, Viewport.Y + Viewport.Height, targetVec.Z); // Convert screen space to worldspace. Note screenspace origin is in topleft part of viewport topLeft = Viewport.Unproject(topLeft, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); topRight = Viewport.Unproject(topRight, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); bottomLeft = Viewport.Unproject(bottomLeft, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); bottomRight = Viewport.Unproject(bottomRight, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); CCPoint topLeftPoint = new CCPoint(topLeft.X, topLeft.Y); CCPoint bottomLeftPoint = new CCPoint(bottomLeft.X, bottomLeft.Y); CCPoint bottomRightPoint = new CCPoint(bottomRight.X, bottomRight.Y); visibleBoundsWorldspace = new CCRect( (float)Math.Round(bottomLeftPoint.X), (float)Math.Round(bottomLeftPoint.Y), (float)Math.Round(bottomRightPoint.X - bottomLeftPoint.X), (float)Math.Round(topLeftPoint.Y - bottomLeftPoint.Y)); visibleBoundsDirty = false; }
public void Transform(ref CCPoint3 point) { Transform(ref point.X, ref point.Y, ref point.Z); }
public CCPoint3 Transform(CCPoint3 point) { Transform(ref point.X, ref point.Y, ref point.Z); return(point); }