So far we've looked at determining GPS coordinates and working out if we are close to them or not using
a matter of degrees.
However what if you came to know the actual distance you from another GPS location or even the distance
you might have traveled on a local scale.
It's not a simple matter of taking one coordinate away from an other.
So he must turn to have a saw an equation this equation finds the shortest path between two geographical
points and looks like this.
As you can see it's not a matter of finding the distance of a straight line but rather an arc of a circle
and performs sort of cosign operations with the differences between the latitudes and longitude.
For a complete derivation of this formula see its Wikipedia entry.
Lucky for you I've converted this formula into a method that you can add stright into your GPA.
Control a script so open that up in unity and then what we want to do is right at the bottom of this
kirtle.
And in this particular function for have a on.
And here it is.
It is quite complex so I make sure that you do type everything in correctly.
So it's getting passed in latitude one longer chewed one.
So that's point location 1 and then latitude 2 and longer to 2 which would be the second location and
it's going to return the number of mages between these two geographical locations.
Now the formula requires a radius so this formula actually will find the distance on any sphere between
two points.
Now you might be going home on the earth isn't perfectly spherical and that's true but this does give
us close enough values for our own use.
Otherwise it becomes extremely complex to try and calculate the actual distances.
The other thing this won't take into consideration is if you go up and down in altitudes if you go up
a hill and down a hill it doesn't count that this is like an as the crow flies across the surface of
a perfectly flat spherical earth.
But we're talking about valleys here that are absolutely massive.
So a few little bumps in that here and there aren't going to affect you too much.
First of all we have the Earth's radius which is about this six million 370 one thousand.
Next we will fade in the latitude one and latitude two values and convert them from degrees which is
the number system we've been using so I feel for language in latitude into radians.
So a lot of the circulatory functions so on and so on.
You should know that they all use radians and therefore any degrees that you're working with will need
to be converted so you can say both of those being converted here.
Then it also requires the Delta latitude which is the changing latitude from one latitude to another.
Difference as you can see there and it also converted into radians and also the difference in the long
chewed valleys.
So Monis each other multiplied by degrees to radians to get that into radians then we're working at
a interim value cold I.
Now this is doing all this on and cost on multiplication.
So first of all we have sawn all the Delta latitude divided to multiply by the sign of a Delta latitude
or two plus the coasts on all the latitude one in radians multiplied by the coast sign of latitude two
in radians multiplied by the sign of the change in Longet you are right to multiply it by the sign of
the change in Longet you devote a boy to that then that value of i which is calculated here is plugged
into this C equation which is finding the A10 to all of square root of say with the square root of 1
minus I multiplied by to those values then multiplied by the Earth's radius which will give you a distance
in Majoras.
So again I'll say make sure you check all these values in the order that you've put them in so that
you calculating the correct value from this method.
OK let's go and build the use of this into our control.
So let's go up to the top.
The first thing we will need to store not only the current lot of chewed but the starting latitude so
that we've got values to compare it to said this latitude is always getting updated with the current
latitude starting latitude will have some kind of default value.
Now we will initially sit those down when we get the first lot of data when we've turned everything
on which is going to be when this message here comes out.
This is in now I named rightest G.P.A..
So at the bottom inside this statement let's just add those two things in and we don't need to clear
them again.
Start latitude is going to equal this value.
And the start long would choose.
A call that Valerie.
Now what I'm also going to do at the same time is grab all of that code and copy it and make another
little method up heat called reset.
So let's call it public void reset.
And I want to use this lighter and be able to cool it from outside to reset they start latitude and
longitude in case you've already started running the app and you've moved around a bit and then you
want to reset your starting position and then move it again to measure it.
Right now let's get back down to our update which is constantly updating.
Al latitude and longitude values to print them out and after we get the latest one I'm going to put
this in here.
Which is going to work out the distance between the leaded to where we currently are and the one that
was measured at the start so that's going to give us a float and we could actually print it out in here.
So I'm just going to change to this to distance traveled and talk in distance there and then we can
just get rid of the longer chaired we don't need that.
And you can take the time in there if you like just to see whether it has updated or when it last updated
for you.
Right.
So that is now all ready to go.
So just save that now before we continue let's add that reset button to make it testing a little bit
easier.
So go back into unity.
Now what I want you to do is go to the hierarchy right click in there go down to UI and we're going
to add Boston now that Boston is going to be very small and in the middle.
First thing we need to do.
Can you remember what you need to do with the canvas.
Yeah we need to go over to the Skyla and sit that to go with screen size so that we're working with
scalable buttons then we can select our button and resize it and reposition it.
Now I'm going to leave it in the middle of the screen actually.
But you could put it wherever you want.
Warning If you do have a android devices Samsung in particular I have put my button accidentally down
here before.
And of course when you press in that vicinity on your phone you also call up the other menus and things.
So that wasn't the greatest idea at the time.
In the middle.
Good for now.
And then you get Elize details that are going to print up along here.
Now let's resizes button this going to day view double click on the button select the little blue dot
bits and then just make it a bit bigger not the text of the button the button self.
OK.
And then when you do do that you'll need to actually go over to the wreck transform and then Center
and alt option center.
The text is now really tiny so I will click on the text.
We'll rename that to reset and just resize it a bit so that you can see it OK.
So now this button needs to run.
The CPS control is a function that we just created a race it.
So Dan the bottom go to click Plus you want to put the JPFO control into that space.
And then we find that reset function by following our nose through all of these menus is the racist.
All right so now you're pretty much ready to roll.
Now if y'all thing doesn't have the white kid chewed camera in it you can add that if you still got
the other camera in it that's fine as well.
I've got an image tracker in here that I was using with the bear and the granny swapping that over.
I might just take that out for this build we don't need it.
And now we will build it up and see how it runs.
So you can see here is me walking around my starting point getting further away and then coming back
again.
And you can see how that distance is outdated every now and then.
Now remember you've got G.P.A. dadda and it's not like centimeter precision perfect and it is going
to work a lot better for you if you're outside.
So take those things into consideration when you're developing using these distance values that you're
getting from your device but now you have an extra tool set in place where you can measure the distance
that someone has traveled while they're using you augment it at and then perform some particular tasks
in that based on that.
So using your new found skills on measuring distance.
What I'd like you to do is this little challenge create another experience using the same Mocca that
changes the three day number being displayed based on the distance you've traveled since you started
or pressed reset.
Use a 3-D models attached to this just to display a one at a distance less than three two at a distance
less than six.
Otherwise you want to display the number three.
So pause the video now.
And when we come back I'll show you how I did it.
First things first you need to download the resources that come with this.
Like just so you've got numbers zip.
If you open them up and put them into a folder called numbers you can see them here.
So you've got these purple three day numbers of one two and three and they will become the drawable
objects all the prefabs that you drag onto your tracker.
Now back at the top level scope here in the JPL control law what you'll need to do for that is to change
the distance that is calculated locally in sort of update lated we have that.
So distance is being calculated and declared here as a float at the same time.
What I've done so I can access it outside of this script is to declare it at the top so up the top.
We're going to put public static float.
Distance equals zero to begin with that will allow us to now easily access it outside of the script.
Now back in unity I've still got the Jeep thing I've added in the ticketed camera.
The canvas is still live from before.
So is the reset button still doing the same thing.
However I've just moved it up into the top left hand corner out of the middle so that it's not over
any augmented characters and it's down enough so that the other dot prints out above it.
Now the image Cheka has been added also to the image tracker.
The target collection of it to my bare Mocca which is the one that I'm using then attached to that as
usual is our trackable object and attached to that is the script that you need.
So there's no drawable in this case just a script that I have called Swap prefabs to and you will see
that it has an array of game objects that I can put in here which meshes of 1 2 and 3.
So I haven't even created prefabs out of those numbers at all.
I've just taken the meshes as I've come in here the models and just drag and drop them straight across
into those elements.
So let's now have a look at what this swap prefabs 2 looks like.
Now it's based on the same idea that we used before when we had granny and the bat changing based on
your GPS location.
But this time what I've done is put the prefabs into an array so that you could see how you might swap
between a lot of different prefabs say that you could have like you could have more than three so you
could make this is extendable as you'd like.
First of all we need to bring in the prefabs themselves so these are the meshes that we're passing through
the models that we're passing through into the inspector that you saw before then I also need to declare
another array of game objects to store the instantiated versions of these.
Then we're grabbing hold of the image trackable so that we can use the trackball itself as the parent
for each of these objects.
So with each of the prefabs in the start we used the length of that prefabs to set the length of the
objects array.
We then loop through all of them and then with each of those prefabs we instantiate one into each position
in the array here using the prefab itself all guess it's not really a prefab is it it's a model but
similar idea still has a position in everything and it's transformed position so if you want it to be
it 000 on your Mokka make sure that it's set to zero zero or zero on that model.
And then we have a zeroed rotation on that that particular object that you just created needs to have
its parent set to this which is this trackable and then of setting it to False.
Now what we need to think about with this is that when one becomes visible all of the others have to
be turned off the way we did the Baron granny where we turned the back on and we turned granny off we
explicitly did that.
And that was I KC's it was only two things but if you imagine that this array was like 10 things long
and then you turned one on and then you had to turn each of those other 10 off and then so you'd have
like 10 if else statements each one turning one thing on and turning all of the others of the code is
going to be really long and start to get really messy.
And as you keep increasing the size of the array you keep having to deal with adding more and more statements.
But what I've done instead is to just create a really quick method which is the next thing here and
it's called Turn on object we pass it the ID and this is the index into the object array of the object
that we want to make visible or to set to active now and saw this method.
We first of all check if that object is already active if it is then we just return is no point.
Leafing through everything turning some off and turning one on.
Now if it's not active we will then go into a forward is going to look through all of our objects when
it finds the object we want to turn on.
So the index of the object equals the day that we pass through we will sit that object to be active
which will make it visible on Mocca.
Otherwise if it's not that one then turn it off.
So this will take care of the process of turning one thing on and everything else off no matter how
big that array becomes with different objects.
Now the update is quite simple because we turned distance into public static that's back in the GP as
controller.
So you can see here all of the conditions set in update.
If G.P.A. control a distance is less than three then we will get object at index 0 2 and on.
So that's the one Kerith.
If JPL control a distance is less than six then we will have the second object turn on.
Otherwise we will turn on the third object.
Now we made G.P.A. control a distance public and also static so that allows us to access it in this
manner.
Hiya and JP as controller is already doing the calculation for the distance we haven't needed to edit
anything else in there except to just change the way that this variable has been declared.
So that makes our job easy.
Right.
So that's pretty much all there is to get that application to work.
Don't forget back in the hierarchy that you want to go to the inspector on your trackable way you've
attached that code.
Put the models in here is the elements for that prefabs array and you'll get the same experience that
I was able to show you before.