calculate geolocation distance btwn current pos and target?

Get help using Construct 2

Post » Mon Aug 01, 2016 6:28 am

How to find out distance between current geolocation coordinate and target geolocation coordinate on every tick? I want to show user how much far (in metres) he is from target object every time he walks towards the target object.
Any help plz
Image
B
85
S
31
G
2
Posts: 130
Reputation: 8,378

Post » Mon Aug 08, 2016 1:50 pm

What unit is your coordinate returned in? Or are you having trouble getting that?
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,867

Post » Tue Aug 23, 2016 7:54 am

i m getting lat and long for current and destination positions. I want it to show in metres only.
Image
B
85
S
31
G
2
Posts: 130
Reputation: 8,378

Post » Wed Aug 24, 2016 12:33 am

Lat. and Long. are not in meters. They are in degrees or other units.
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,867

Post » Tue Aug 30, 2016 9:39 am

gumshoe2029 wrote:Lat. and Long. are not in meters. They are in degrees or other units.

yes how to convert them to readable form and use it to calculate distance between two points?
Image
B
85
S
31
G
2
Posts: 130
Reputation: 8,378

Post » Tue Aug 30, 2016 4:12 pm

Haversine formula ?

Haversine formula:
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c

where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km);
note that angles need to be in radians!
Radians = Degrees * PI / 180


JavaScript:

var R = 6371e3; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();

var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c
;
B
67
S
24
G
7
Posts: 1,519
Reputation: 11,104

Post » Wed Aug 31, 2016 7:41 am

korbaach wrote:Haversine formula ?

Haversine formula:
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c

where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km);
note that angles need to be in radians!
Radians = Degrees * PI / 180


JavaScript:

var R = 6371e3; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();

var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c
;



Thanks a lot..but im just starter with construct 2..c.ant understand so much math. any simpler solution will be big big help. thanks in advance.
Image
B
85
S
31
G
2
Posts: 130
Reputation: 8,378

Post » Wed Aug 31, 2016 8:29 pm

jatin1726 wrote: ..any simpler solution will be big big help.


True..so true.. :D

here is a small example, with Browser.ExecJS : browserGeoDist.capx
(tested only in Chrome)

I tried to "translate" from javascript to C2 events...

Code: Select all
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c


but there is no function atan2(Y,X) in C2?!!

....or I overlooked something?
B
67
S
24
G
7
Posts: 1,519
Reputation: 11,104

Post » Thu Sep 01, 2016 2:08 am

Instead of atan2(y,x) use angle(0,0,X,y).
B
100
S
38
G
134
Posts: 5,550
Reputation: 85,313

Post » Thu Sep 01, 2016 3:41 pm

R0J0hound wrote:Instead of atan2(y,x) use angle(0,0,X,y).


Thanks @R0J0hound !

so..from
Code: Select all
atan2( √a, √(1−a) )

to
Code: Select all
angle(0,0,sqrt(1-a),sqrt(a))*(pi/180)


cool 8-)
--------------------------------------------
here:

Distance between London and Paris

Image

haversine2C2.capx
B
67
S
24
G
7
Posts: 1,519
Reputation: 11,104

Next

Return to How do I....?

Who is online

Users browsing this forum: dioking, jetamie and 5 guests