Isometric Tiled Map Object Coordinates - cocos2d-x-3.0

I'm getting a bit confused here about the coordinates of an object on a Tiled isometric map. I have an isometric map that is 25x25 tiles, each tile being 32x32 pixels (64x32 isometrically). I have an object on the map positioned at 128.0 x and 64.0 y. When running this in cocos2d-x, the coordinates are coming up as 128.0 x and 704.0 y? Where is this y coordinate coming from?
I tried comparing it to different y values which is as follows:
64.0 y in Tiled = 704.0 y in cocos2d-x
128.0 y in Tiled = 640.0 y in cocos2d-x
256.0 y in Tiled = 512.0 y in cocos2d-x
I honestly cannot figure out where this y-coordinate is coming from. Any help is appreciated.

Related

How to correctly find UV on sphere

I have a sphere and a texture for it.
Texture consist of 16 tiles of zoom = 2 from OSM. Tile size is 256x256.
At top and bottom I added space to cover area in ranges [90, 85.0511] and [-85.0511, -90], proportionally. So texture size was 1024x1083.
I also tried texture without these two spaces, its size was 1024x1024 (map tiles only).
The problem is that after UV mapping on Y-axis objects are smaller on equator and bigger on poles.
There are two types of formulas
u = (lon + 180) / 360; // lon = [-180, 180]
v = (lat + 90) / 180; // lat = [-85.0511, 85.0511]
----
u = Math.atan2(z, x) / (2 * Math.PI) + 0.5; // x, y, z are vertex coordinates
v = Math.asin(y) / Math.PI + 0.5;
I tried all 8 variations: two textures, two u-formulas and two v-formulas.
The result is like on image above, or worse.
What am I doing wrong? Is it about texture, or UV-formulas, or something else?
P.S.: for poles (vertices in lat range = [-90, -85.0511], [85.0511, 90]) in fragment shader I don't use color from texture, but just solid color
OSM uses the Web Mercator projection. See also on OSM wiki.
The conversion from world (x,y,z) to texture (u,v) coordinates would be:
lon = atan2(y, x)
lat = atan2(z, sqrt(x*x+y*y))
u = (lon + pi)/(2*pi)
v = (log(tan(lat/2 + pi/4)) + pi)/(2*pi)
(I assume that z points north like in WGS-84 and all coordinates are right-handed.)
This projection doesn't cover the entire sphere: as the latitude approaches the poles, the v coordinate blows up to infinity. Therefore extending the map to the north or south direction is not going to be helpful.
Instead keep the original square 1024x1024 texture and render a texture mapped sphere capped at the ±85.051129° latitute (that's where v = 0,1) using the above coordinate mapping.
Alternatively (and this is more in-line with Web Mercator spirit), render each tile regular in the UV coordinates, and calculate the XYZ coordinates by reversing the above transformation.

Can I project a rectangle to 2D by simply setting Z to 0?

I have a rectangle in 3D space that I need to project to 2D to the screen.
The camera is orthographic, so I figured - can I just set the Z coordinates of the 4 points of the rectangle to 0, so they would splat on the screen?
When I rotate a rectangle on the Y axis for instance, since the camera is orthographic - all I see is the rectangle in front of me getting narrower, because the X and Y components are being altered(along with the Z component).
But if I set the Z to 0 and leave the X and Y, it would still look the same on the orthographic camera.
The question is - is this a viable method? Are there cases where it breaks?
Yes, for building orthographic projection onto OXY plane it is enough to set z=0.
matrix is
(1 0 0 0)
(0 1 0 0)
(0 0 0 0)
(0 0 0 1)
When you rotate origin-centered axis-aligned rectangle about axis Y, it's projection will change width, but height remains the same.
Example: right top corner has coordinates (1, 1, 0). After rotation about Y-axis by angle Fi, it has 3d coordinates (Cos(Fi), 1, Sin(Fi)) and screen coordinates (Cos(Fi), 1)

convert pixel coordinates to map coordinates

I have an image A of dimension p x q. If I know the UTM coordinate of A(1,1) and A(p,q) and pixel size in meters.
How to convert the pixel coordinates to map coordinates in MATLAB?
Xsize = (1:p)*PixelSizeInMeter+UTM_x_onA11;
Ysize = (1:q)*PixelSizeInMeter+UTM_y_onA11;
figure;
surface(Xsize,Ysize,A);
Now you can plot your map using Xsize and Ysize. Since UTM is a Cartesian grid, life's quite easy: get the correct number of elements, multiply with the grid size and add the lower corner's coordinates to shift the plot to the correct location.

Unity Get Pixel Coordinate Values from RectTransform

I am in need of a way to obtain the pixel coordinates of a rect transform.
x position where 0 is the left of the screen and y position where 0 is the bottom of the screen.
You want to use Camera.WorldToScreenPoint.
Convert the object origin.
Convert the object origin+width.
Convert the object origin+height.
Your pixel coords are Array[origin.x, origin.y, origin.x+width, origin.y+height]
In 3d there are not pixels. Only a units in unity 3d.
float x = transform.x;
float y = transform.y;
You will get units in x, y.
If you working with textures you might use Texture2D, it has some methods for pixel working.

Draw circle using latitude and longitude

I want to plot a latitude and longitude using matlab. Using that latitude and longitude as center of the circle, I want to plot a circle of radius 5 Nm.
r = 5/60;
nseg = 100;
x = 25.01;
y = 55.01;
theta = 0 : (2 * pi / nseg) : (2 * pi);
pline_x = r * cos(theta) + x;
pline_y = r * sin(theta) + y;
hold all
geoshow(pline_x, pline_y)
geoshow(x, y)
The circle does not look of what I expected.
Drawing a circle on earth is more complex that it looks like.
Drawing a line or a poly line is simple, because the vertices are defined.
Not so on circle.
a circle is defined by all points having the same distance from center (in meters! not in degrees!!!)
Unfortuantley lat and lon coordinates have not the same scale.
(The distance between two degrees of latidtude is always approx. 111.3 km, while for longitude this is only true at the equator. At the poles the distance between two longitudes approach zero. In Europe the factor is about 0.6. (cos(48deg))
There are two solution, the first is more universal, usefull for nearly all problems.
convert spherical coordinate (of circle center) to cartesian plane with unit = 1m, using a transformation (e.g equidistant transformation, also called equirectangular transf., this transformation works with the cos(centerLat) compensation factor)
calculate points (e.g circle points) in x,y plane using school mathematics.
transform all (x,y) points back to spherical (lat, lon) coordinates, using the inverse transformation of point 1.
Other solution
1. write a function which draws an ellipse in defined rectangle (all cartesian x,y)
2. define bounding of the circle to draw:
2a: calculate north-south diameter of circle/ in degrees: this a bit tricky: the distance is define in meters, you need a transformation to get the latitudeSpan: one degrees of lat is approx 111.3 km (eart circumence / 360.0): With this meters_per_degree value calc the N-S disatcne in degrees.
2b: calculate E-W span in degrees: now more tricky: calculate like 2a, but now divide by cos(centerLatitude) to compensate that E-W distances need more degrees when moving north to have the same meters.
Now draw ellipseInRectangle using N-S and E_W span for heigh and width.
But a circle on a sphere looks on the projected monitor display (or paper) only like a circle in the center of the projection. This shows:
Tissot's Error Ellipse