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
B
50
S
15
Posts: 75
Reputation: 3,737

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
Company name changed to avoid Facebook-type shenanigans

"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
22
S
6
G
1
Posts: 1,415
Reputation: 4,824

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.
B
50
S
15
Posts: 75
Reputation: 3,737

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
Company name changed to avoid Facebook-type shenanigans

"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
22
S
6
G
1
Posts: 1,415
Reputation: 4,824

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?
B
50
S
15
Posts: 75
Reputation: 3,737

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
;
Image
B
65
S
23
G
7
Posts: 1,505
Reputation: 10,896

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.
B
50
S
15
Posts: 75
Reputation: 3,737

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?
Image
B
65
S
23
G
7
Posts: 1,505
Reputation: 10,896

Post » Thu Sep 01, 2016 2:08 am

Instead of atan2(y,x) use angle(0,0,X,y).
B
92
S
32
G
107
Posts: 5,280
Reputation: 69,971

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
Image
B
65
S
23
G
7
Posts: 1,505
Reputation: 10,896

Next

Return to How do I....?

Who is online

Users browsing this forum: jorgmaquoi and 22 guests