/// <summary> /// Using the passed world space coordinates, object width/height, and offset, converts the values to the projected screen space equivalent based on the projection type /// </summary> /// <param name="worldX"></param> /// <param name="worldY"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="offset"></param> /// <param name="projectionType"></param> /// <returns></returns> public static Vector WorldSpaceToScreenSpace(float worldX, float worldY, Vector offset, ScreenProjectionType projectionType) { if (projectionType == ScreenProjectionType.Isometric) { float screenSpaceX = worldX - worldY; float screenSpaceY = (worldX + worldY) / 2; Vector screenSpace = new Vector(screenSpaceX, screenSpaceY); Vector offsetScreenSpace = screenSpace + offset; return(offsetScreenSpace); } return(new Vector(worldX, worldY)); }
/// <summary> /// Using the screen space coordinates and offset, converts the values to the world space equivalent based on the projection type /// </summary> /// <param name="screenX"></param> /// <param name="screenY"></param> /// <param name="offset"></param> /// <param name="projectionType"></param> /// <returns></returns> public static Vector ScreenSpaceToWorldSpace(float screenX, float screenY, Vector offset, ScreenProjectionType projectionType) { // undo the offset when going from screen to world space screenX -= offset.X; screenY -= offset.Y; // undo the camera transformation when going from screen to world space screenX += Camera.Position.X; screenY += Camera.Position.Y; if (projectionType == ScreenProjectionType.Isometric) { float worldX = (2 * screenY + screenX) / 2; float worldY = (2 * screenY - screenX) / 2; Vector worldSpace = new Vector(worldX, worldY); return(worldSpace); } else if (projectionType == ScreenProjectionType.Orthogonal) { return(new Vector(screenX, screenY)); } else { throw new NotImplementedException("Projections other than Isometric are not supported"); } }