android - Unity3D - Get smooth speed and acceleration with GPS data -
i'm using unity3d , created simple distance, speed , acceleration calculator using latitude , longitude of last position. i'm calculating last distance, speed , acceleration in each gps update (approximately once per second). (2-3 second interval) latitude , longitude values changes rapidly (in clear weather , no obstacles). that's why speed , acceleration values gets unreal results. example, @ stable 40 km/h speed, speed value becomes 60 km/h , returns 40 km/h within 2-3 second. i'm here ask how can avoid inaccurate , rapid gps data changes?
i'm using nexus 5 device
there code:
using unityengine; using system.collections; using unityengine.ui; using unityengine.scenemanagement; public class manager : monobehaviour { public text longitude, latitude, lonatext, latatext, lonbtext, latbtext; public text result, overallresult, speedtext, lasttimetext, timertext, accelerationtext, speed0text; float lona, lonb, lata, latb, overalldistance, lastdistance, timer, lasttime, speed, speed0, acceleration; bool firsttime, allowtimer; public audiosource audio; void awake() { overalldistance = 0; lastdistance = 0; timer = 0; lasttime = 0; speed = 0; speed0 = 0; firsttime = true; allowtimer = true; } ienumerator start() { // first, check if user has location service enabled if (!input.location.isenabledbyuser) yield break; // start service before querying location input.location.start(1, 1); // wait until service initializes int maxwait = 20; while (input.location.status == locationservicestatus.initializing && maxwait > 0) { yield return new waitforseconds(1); maxwait--; } // service didn't initialize in 20 seconds if (maxwait < 1) { print("timed out"); yield break; } // connection has failed if (input.location.status == locationservicestatus.failed) { print("unable determine device location"); yield break; } else { // access granted , location value retrieved print("location: " + input.location.lastdata.latitude + " " + input.location.lastdata.longitude + " " + input.location.lastdata.altitude + " " + input.location.lastdata.horizontalaccuracy + " " + input.location.lastdata.timestamp); longitude.text = input.location.lastdata.longitude.tostring(); latitude.text = input.location.lastdata.latitude.tostring(); lona = input.location.lastdata.longitude; lata = input.location.lastdata.latitude; } // stop service if there no need query location updates continuously //input.location.stop(); } void update() { longitude.text = input.location.lastdata.longitude.tostring(); latitude.text = input.location.lastdata.latitude.tostring(); timer += time.deltatime; timertext.text = timer.tostring(); if (lona != input.location.lastdata.longitude || lata != input.location.lastdata.latitude) { audio.play(); calculatedistances(lona, lata, input.location.lastdata.longitude, input.location.lastdata.latitude); // last distance , overall distances lona = input.location.lastdata.longitude; lata = input.location.lastdata.latitude; lasttime = timer; lasttimetext.text = lasttime.tostring(); timer = 0; speed0 = speed; speed0text.text = speed0.tostring(); calculatespeed(); calculateacceleration(); } } public static float radians(float x) { return x * mathf.pi / 180; } public void calculatedistances(float firstlon, float firstlat, float secondlon, float secondlat) { lonatext.text = firstlon.tostring(); latatext.text = firstlat.tostring(); lonbtext.text = secondlon.tostring(); latbtext.text = secondlat.tostring(); float dlon = radians(secondlon - firstlon); float dlat = radians(secondlat - firstlat); float distance = mathf.pow(mathf.sin(dlat / 2), 2) + mathf.cos(radians(firstlat)) * mathf.cos(radians(secondlat)) * mathf.pow(mathf.sin(dlon / 2), 2); float c = 2 * mathf.atan2(mathf.sqrt(distance), mathf.sqrt(1 - distance)); lastdistance = 6371 * c * 1000; result.text = lastdistance.tostring() + " meters"; overalldistance += lastdistance; // bu 1 anliq 6.000.000-dan boyuk qiymet ala biler startcoroutine(overall()); } ienumerator overall() { if (firsttime) { firsttime = false; yield return new waitforseconds(2); if (overalldistance > 6000000) { overalldistance = 0; lastdistance = 0; } } overalldistance += lastdistance; overallresult.text = overalldistance.tostring() + " meters"; } void calculatespeed() { speed = lastdistance / lasttime * 3.6f; speedtext.text = speed.tostring(); } void calculateacceleration() { acceleration = (speed - speed0) / lasttime; accelerationtext.text = acceleration.tostring(); } }
thanks , tip
ps. please edit question if made grammar , logical mistake.
Comments
Post a Comment