Log in or register to post comments

Position prefab above imageview in specific pixel coordinates

August 31, 2012 - 4:37am #1

Hi, i have this project where there is an imageview (map of a city) and i need to display above it some pins in specific locations using vuforia for unity.

I get latitude and longtitude of places and then i convert them, using the mercator projection, to pixels as described in Map Coordinates - Google. Then, i instatiate the prefab :

marker = (Transform) Instantiate(Resources.Load(("Marker"), typeof(Transform)), position, Quaternion.identity); 

where position is a vector3 object with x value the latitude in pixels, y value the longtitude in pixels and z zero. When i build and run the project i can see the prefab created and added as a child to my imagetarget, but not in the correct position(actually it's positioned way above the imagetarget and the ARCamera). Is there a specific way to position the prefab correctly in the imageview for an AR application? I also tried to use the `worldToScreenPoint(new Vector3(x,y,z))` with no luck..

How can i position the prefab correctly, using vuforia for unity, in order to show up inside the imageview and in the right location.

Thank you in advance for you answers and your help will be appreciated

Position prefab above imageview in specific pixel coordinates

May 8, 2014 - 3:29pm #11

Noticed that this doesn't seem to work with Cloud Targets though...

Urk - just realized it does if I remove the Turn Off Behaviour script...

Position prefab above imageview in specific pixel coordinates

September 6, 2012 - 2:47am #10

You're welcome!

Wink

Position prefab above imageview in specific pixel coordinates

September 5, 2012 - 7:45pm #9

Nice work!

Thanks for posting that solution.

Position prefab above imageview in specific pixel coordinates

September 4, 2012 - 3:05am #8

Finally, it took me a few hours but i' ve managed to make it work, so i am writting the answer in case someone has the same issue..

First of all you have to find the bounds of your map (Unity Coordinates bounds and Google maps bounds). For unity:

GameObject imagetarget = GameObject.Find("ImageTarget");

print(imagetarget.renderer.bounds.min);

print(imagetarget.renderer.bounds.max);

For Google maps:

google.maps.event.addListener(map, 'idle', function() {
                  var bounds =  map.getBounds();
                  var ne = bounds.getNorthEast();
                  var sw = bounds.getSouthWest();
                  alert(ne +" , "+ sw);
         });

Now we know the Lat/Lon values of the four vertices of the map in which Unity values are corresponding.So we can find the X,Y values of any point in the map if we know its Lat/Lon values.

Converting to XY coordinate:

//midLat/lon, distLat/lon are constant values, compute them once.

midLat = (swLat + neLat)/2;

distLat = (neLat - swLat)/2;

midLon = (swLon + neLon)/2;

distLon = (neLon - swLon)/2;

 

point (pLat, pLon);

y = Z * (pLat - midLat)/ distLat;

x = X * (plon - midLon)/distLon;

, Where Z, X are the values you get from Unity coordinates bounds.

Hope this helps and thank you for your help!

Position prefab above imageview in specific pixel coordinates

September 3, 2012 - 4:53am #7

Just an idea:

If you already have the max and min values of your map than i think it should be quite easy to map unity units with the longitude and latitude.

Just divide the length of your map with 360 and the height with 180 and use this value as one degree. Then you should be able to transfer the different coordinates.

Position prefab above imageview in specific pixel coordinates

September 3, 2012 - 3:02am #6

So i have to convert the lat/lon values in its parent coordinate system (Unity Coordinate System).

To be more specific.  I figured out how to get the bounds in world space of my imagetarget by doing this

print ("min: "+renderer.bounds.min);
print ("max: "+renderer.bounds.max);

 I also know the bounds in lat/lon (North East and South West) values of my map.

So now i know for instance that this vector (-100, 0, -71.3) is the (33.2392250, 27.53046408) which represents the North East bound of my map. Is now any way to convert the lat/lon values of the POIs?

Thank you both for your answers

 

Position prefab above imageview in specific pixel coordinates

September 2, 2012 - 11:42pm #5

I don't know if that's the problem or you already know this, but:

Child object get positioned according to the parents object pivot point (its "own" coordinate system). You better click on the parent object (should be the marker) and check where that is.

Position prefab above imageview in specific pixel coordinates

August 31, 2012 - 2:01pm #4

There's the issue.  That Vector3 position is placing your content extremely far away from your target because you're pushing it away from the target's relative position by 9401350.0 units on x and 6456885.0 on Y. 

I'd try manually placing a gameobject into the scene via the Editor and place it in a location that you know the lat/long for.  Note it's transform.position on XYZ and then compare the lat/long with the XYZ value of the manually placed gameobject.  I bet there is a radical difference between the world space unit positioning relative to the trackable and the conversion units you're using for lat/long.

You'll probably need to convert the values using a different conversion as this one if creating XY coords that are in the millions of units.

Position prefab above imageview in specific pixel coordinates

August 31, 2012 - 6:43am #3

Yes of course,

Let's say that i have a POI which is located in 38.2459, 21.7311 (Lat, Lon) on the map. After the conversion, i get the pixels which are:

x = 9401350.0, y = 6456885.0. So the position variables would be:

Vector3 position = new Vetor3 ( 9401350.0, 6456885.0, 0 )

The map (imagetarget) is saved in zoom 16 that's why the x and y values are big. The conversion is correct but i don't know how to tell to Unity how to do it.

I don't know if i am thinking of it right. I can understand that the numbers don't seem so right relative to the tracker but i am new in Unity and i am trying to figure it out how this can be done.

Thank you again!

Position prefab above imageview in specific pixel coordinates

August 31, 2012 - 6:18am #2

Sounds like your position Vector3 that you're passing into the instantate call is incorrectly positioning your object relative to the tracker.  Can we see a sample of what your position variable looks like?

 

Log in or register to post comments