Calculate resultant acceleration from x and y. Calculate angle of resultant acceleration.
-
Where did you get the x, and y acceleration from? GPS doesnt have x an y, it has lat and lon whoich is not related to a cartesian x,y; Espcially the scaling of both axes differ by a factor of cos(lat), thatg is 0.6 in Europe. – AlexWien Mar 21 '13 at 18:37
-
OK, and what so you do with the z axis of the accelerometer, that is missing in your question. – AlexWien Mar 21 '13 at 20:18
-
Do you want only the tangential (speed changes along route) component, or also the centripetal component of acceleration? – heltonbiker Mar 21 '13 at 20:23
3 Answers
You can use vector maths to calculate the vector's length and angle:
length = sqrt(x * x + y * y)
angle = atan2(y, x) //this might be changed depending on your angle definitions

- 32,049
- 4
- 39
- 70
-
If `x` and `y` are orthogonal parts of the acceleration, then `length` is the overall acceleration. – Nico Schertler Mar 21 '13 at 13:15
-
@user2052706: Acceleration is a vector. A vector is both length (magnitude) and direction. The statements in this answer calculate the length of the vector and the direction of the vector. The angle is in radians counterclockwise from the positive x direction (counterclockwise is from the positive x direction toward the positive y direction). – Eric Postpischil Mar 21 '13 at 13:32
-
But note, that the angle is not the geograhpical heading, which uses 0° as North and is clockwise; this formula gives mathematical direction which uses 0° as East and is counter clockwise. – AlexWien Mar 21 '13 at 18:43
You dont need the x and y acceleration, just calculate the acceleration as speed change per time.
For Gps i reccommend to use the speed attribute of the location, which is much more accurate then the positions.
If you dont have speed, then calculate speed as distance per time.
Distance: use any distance formula you find for calculate distance between two lat/lon coordinates.
If you want to use the values you got from the acceleration sensor, then you should use 3-axis acceleration vector:
See also how-do-i-get-the-total-acceleration-from-3-axes
One further tipp to check if implementation is plausible:
Sum up all your (one dimension) accelerations, they shoud be near 0.
-
Any reasons for the downvote? I have implemeted that and its working perfect, so if anybody claims that my answer is not correct, please comment why! – AlexWien Mar 21 '13 at 18:34
-
@mr0matt its a fine idea to use the acceleration sensor, but only if you are able to remove the gravity vector. But since it is so easy to use GPS to caluclate the acceleration, and you dont have to remove any gravity, or calibrate, its fine to compare the results! Further it deponds for what you need it! Which accleration do you want to measure? That of the phone or that of the vehicle? If you bump over a rough road, you can get high pulsating accelerration. You have to define a filter to get the acceleration frequency you want. To measure whether a vehicle/phone is standstill or not – AlexWien Mar 21 '13 at 20:04
-
acc sensor is fine. For vehilce acceleration the GPS approach give you correct results within 3 hours of work. For the acc sensor, tell me later how much time you needed. – AlexWien Mar 21 '13 at 20:06
-
If you tunr your phone by 90° you get an accelration of 1g! so the phone must be fixed mountd in the car. Further GPS speed is acurate to 0.1km/h so this give a quite acurate accleration if the speed is over 10km/h. – AlexWien Mar 21 '13 at 20:10
-
I upvoted the question. If anyone can "get" the speed reading from the GPS itself (I mean the actual GPS speed of a given fix, not the value calculated from successive positions), that is the most precise value one can get for the ACCELERATION OF A VEHICLE ALONG A PATH. Accelerometers are not very imprecise for vehicle acceleration, whose magnitude and frequency content is very different from a cell phone being moved around by a person (while walking, gaming, talking during a call, etc). – heltonbiker Mar 21 '13 at 20:21
If you want the direction of the acceleration with respect to the world coordinate system, then you have to register for both TYPE_ACCELEROMETER
and TYPE_MAGNETIC_FIELD
. Using the results you call getRotationMatrix
and then multiply the accelerometer values by this matrix will give you the coordinates of the acceleration in term of the world coordinate system. The first 2 coordinates would be the x and y coordinates.
Actually you better also register for TYPE_GRAVITY
and pass the results together with the magnetic field results to the getRotationMatrix
, so that you do not have to filter accelerometer values yourself.

- 18,033
- 3
- 50
- 54