I have used this code to get the G-Force value. But the problem is When I keep my iPhone on table still it show 0.99 value.Can any one tell me the reason? Code is
sx = acceleration.x * kFilteringFactor + sx * (1.0 - kFilteringFactor);
sy = acceleration.y * kFilteringFactor + sy * (1.0 - kFilteringFactor);
sz = acceleration.z * kFilteringFactor + sz * (1.0 - kFilteringFactor);
float aValue = sqrt(sx*sx+sy*sy+sz*sz);
[gLabel setText:[NSString stringWithFormat:#"%.2f",aValue]];
At least in Android that is correct and is because of Gravity. The only way to get 0 would be in a free fall. IF it is on a table then there is a force applied to "not be falling" which is of the same
magnitude of the Gravity (1G) and oppose direction.
Related
I'm new in Processing and I have a kind of easy question.
I have a mouseX added to a wave so on the left there's less wave than on the right. But on the right the wave is excessive. Do you know how to fix this?
int wave = int(sin(frameCount * 0.02 + ( x * y ) * 0.01) * mouseX);
sin() returns a value in the range [-1.0, 1.0]. Define the height at the left (h_left) and the height at the right (h_right) and interpolate between the values by the function lerp(), according to the relative x coordinate of the mouse ((float)mouseX/width):
float h_left = 100.0;
float h_right = 200.0;
float weight = (float)mouseX/width;
float height = lerp(h_left, h_right, weight);
int wave = int(sin(frameCount * 0.02 + ( x * y ) * 0.01) * height/2.0);
I came across this 2D noise function in the Book of Shaders
float noise(vec2 st) {
vec2 integerPart = floor(st);
vec2 fractionalPart = fract(st);
float s00 = random(integerPart);
float s01 = random(integerPart + vec2(0.0, 1.0));
float s10 = random(integerPart + vec2(1.0, 0.0));
float s11 = random(integerPart + vec2(1.0, 1.0));
float dx1 = s10 - s00;
float dx2 = s11 - s01;
float dy1 = s01 - s00;
float dy2 = s11 - s10;
float alpha = smoothstep(0.0, 1.0, fractionalPart.x);
float beta = smoothstep(0.0, 1.0, fractionalPart.y);
return s00 + alpha * dx1 + (1 - alpha) * beta * dy1 + alpha * beta * dy2;
}
It is clear what this function does: it generates four random numbers at the vertices of a square, then interpolates them. What I am finding difficult is understanding why the interpolation (the s00 + alpha * dx1 + (1 - alpha) * beta * dy1 + alpha * beta * dy2 expression) works. How is it interpolating the four values when it does not seem to be symmetric in the x and y values?
If you expand the last line, it's:
return s00 * (1-alpha) * (1-beta) +
s10 * alpha * (1-beta) +
s01 * (1-alpha) * beta +
s11 * alpha * beta;
Which is symmetric in x and y. If you add up the weights:
alpha * beta + (1-alpha) * beta + alpha * (1-beta) + (1-alpha) * (1-beta)
= (alpha + 1-alpha) * beta + (alpha + 1-alpha) * (1-beta)
= beta + 1-beta
= 1
so it's an affine combination of the values at the corners
For skeletal animation using colladas, I need to linearly interpolate between 2 matrices. I saw somewhere that I can use quaternions to interpolate between matrices, but that only works for the rotational component, and I need to preserve the transform as well. Here is my code, which works, except for the translation part:
float total = (orderedBones[i]->Animation->keyFrames[nextKeyFrame] - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1]) * 100.0;
float progress = orderedBones[i]->Animation->accumTime - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1] * 100.0;
float interpolation = progress / total;
glm::quat firstQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame - 1]);
glm::quat secondQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame]);
glm::quat finalQuat = glm::slerp(firstQuat, secondQuat, interpolation);
orderedBones[i]->Animation->interpoltaedMatrix = glm::mat4_cast(finalQuat);
Is there any way that I can do this?
I ended up solving my question through a bit more web surfing. For future reference, heres how to do it.
The transformation component is stored in a 4x4 matrix like this:
r r r t
r r r t
r r r t
0 0 0 1
where r is the rotational component and t is the translation component. Because of this, we can represent the translation component as a vector. 2 Vectors can be linearly interpolated, so we interpolate those two vectors and then shove them back into the rotation matrix when they're done. Heres the final code, but its a bit messy:
float total = (orderedBones[i]->Animation->keyFrames[nextKeyFrame] - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1]) * ANIMATION_MULTIPLICATION_CONST;
float progress = orderedBones[i]->Animation->accumTime - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1] * ANIMATION_MULTIPLICATION_CONST;
float interpolation = progress / total;
glm::quat firstQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame - 1]);
glm::quat secondQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame]);
glm::quat finalQuat = glm::slerp(firstQuat, secondQuat, interpolation);
orderedBones[i]->Animation->interpoltaedMatrix = glm::mat4_cast(finalQuat);
glm::vec4 transformComp1 = glm::vec4(
orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][0][3],
orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][1][3],
orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][2][3],
orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][3][3]);
glm::vec4 transformComp2 = glm::vec4(
orderedBones[i]->Animation->Matrices[nextKeyFrame][0][3],
orderedBones[i]->Animation->Matrices[nextKeyFrame][1][3],
orderedBones[i]->Animation->Matrices[nextKeyFrame][2][3],
orderedBones[i]->Animation->Matrices[nextKeyFrame][3][3]);
glm::vec4 finalTrans = (float)(1.0 - interpolation) * transformComp1 + transformComp2 * interpolation;
// good for now, although in future the 2 transformation components need to be interpolated
orderedBones[i]->Animation->interpoltaedMatrix[0][3] = finalTrans.x;
orderedBones[i]->Animation->interpoltaedMatrix[1][3] = finalTrans.y;
orderedBones[i]->Animation->interpoltaedMatrix[2][3] = finalTrans.z;
orderedBones[i]->Animation->interpoltaedMatrix[3][3] = finalTrans.w;
Hope that answers anybody else's questions :)
This function is working for me:
glm::mat4 interpolate(glm::mat4& _mat1, glm::mat4& _mat2, float _time)
{
glm::quat rot0 = glm::quat_cast(_mat1);
glm::quat rot1= glm::quat_cast(_mat2);
glm::quat finalRot = glm::slerp(rot0, rot1, _time);
glm::mat4 finalMat = glm::mat4_cast(finalRot);
finalMat[3] = _mat1[3] * (1 - _time) + _mat2[3] * _time;
return finalMat;
}
I am using the equations and data for Mars here
http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf
and the solution for the eccentric anomaly kepler equation given here at the top of page four
http://murison.alpheratz.net/dynamics/twobody/KeplerIterations_summary.pdf
And checking the output by modifying the date in the get_centuries_past to the following dates and looking at page E-7 for the actual x,y,z coordinates of Mars (sample data below, but link for the curious:
http://books.google.com/books/about/Astronomical_Almanac_for_the_Year_2013_a.html?id=7fl_-DLwJ8YC)
date 2456320.5 is 2013, 1, 28 and should output
x = 1.283762
y = -0.450111
z = -0.241123
date 2456357.5 is 2013, 3, 6 and should output
x = 1.300366
y = 0.533593
z = 0.209626
date 2456539.500000 is 2013, 9, 4 and should output
x = - 0.325604
y = 1.418110
z = 0.659236
I tested mean anomaly equation and it was fine. However, I cannot get a good set of x,y,z coordinates. I have been tweaking my kepler and coordinate function but cannot get them to match the tables in the astronomical almanac.
Any suggestions or advice on solving the positions of the stars is greatly appreciated. The code below can be put in a .rb file and running it on the command line will output the x,y,z values.
def get_centuries_past_j2000()
#second number is from DateTime.new(2000,1,1,12).amjd.to_f - 1 the modified julian date for the J2000 Epoch
#Date.today.jd.to_f - 51544.5
(DateTime.new(2013,1,28).amjd.to_f - 51544.5)/36525
end
class Planet
attr_accessor :semi_major_axis, :semi_major_axis_delta, :eccentricity, :eccentricity_delta,
:inclination, :inclination_delta, :mean_longitude, :mean_longitude_delta, :longitude_of_perihelion,
:longitude_of_perihelion_delta, :longitude_of_ascending_node, :longitude_of_ascending_node_delta, :time_delta
def initialize(semi_major_axis, semi_major_axis_delta, eccentricity, eccentricity_delta,
inclination, inclination_delta, mean_longitude, mean_longitude_delta, longitude_of_perihelion,
longitude_of_perihelion_delta, longitude_of_ascending_node, longitude_of_ascending_node_delta, time_delta)
#semi_major_axis = semi_major_axis + (semi_major_axis_delta * time_delta)
#eccentricity = eccentricity + (eccentricity_delta * time_delta)
#inclination = inclination + (inclination_delta * time_delta)
#mean_longitude = mean_longitude + (mean_longitude_delta * time_delta)
#longitude_of_perihelion = longitude_of_perihelion + (longitude_of_perihelion_delta * time_delta)
#longitude_of_ascending_node = longitude_of_ascending_node + (longitude_of_ascending_node_delta * time_delta)
#argument_of_perhelion = #longitude_of_perihelion - #longitude_of_ascending_node
end
def mean_anomaly
((#mean_longitude - #longitude_of_perihelion)%360).round(8)
end
def eccentric_anomaly
mod_mean_anomaly = mean_anomaly
if mod_mean_anomaly > 180
mod_mean_anomaly = mod_mean_anomaly - 360
elsif mod_mean_anomaly < -180
mod_mean_anomaly = mod_mean_anomaly + 360
end
e34 = #eccentricity**2
e35 = #eccentricity*e34
e33 = Math.cos(mod_mean_anomaly*Math::PI/180)
mod_mean_anomaly + (-0.5 * e35 + #eccentricity + (e34 + 1.5 * e33 * e35) * e33) * Math.sin(mod_mean_anomaly*Math::PI/180)
end
def J2000_ecliptic_plane
x_prime = #semi_major_axis * (Math.cos(eccentric_anomaly*Math::PI/180) - #eccentricity)
y_prime = #semi_major_axis * Math.sqrt(1-#eccentricity**2) * Math.sin(eccentric_anomaly*Math::PI/180)
z_prime = 0
x = x_prime * (Math.cos(#argument_of_perhelion*Math::PI/180) * Math.cos(#longitude_of_ascending_node*Math::PI/180) - Math.sin(#argument_of_perhelion * Math::PI/180) * Math.sin(#longitude_of_ascending_node * Math::PI/180) * Math.cos(#inclination * Math::PI/180)) + y_prime * (-Math.sin(#argument_of_perhelion* Math::PI/180) * Math.cos(#longitude_of_ascending_node * Math::PI/180) - Math.cos(#argument_of_perhelion * Math::PI/180) * Math.sin(#longitude_of_ascending_node * Math::PI/180) * Math.cos(#inclination * Math::PI/180))
y = x_prime * (Math.cos(#argument_of_perhelion*Math::PI/180) * Math.sin(#longitude_of_ascending_node*Math::PI/180) + Math.sin(#argument_of_perhelion * Math::PI/180) * Math.cos(#longitude_of_ascending_node * Math::PI/180) * Math.cos(#inclination * Math::PI/180)) + y_prime * (-Math.sin(#argument_of_perhelion* Math::PI/180) * Math.sin(#longitude_of_ascending_node * Math::PI/180) + Math.cos(#argument_of_perhelion * Math::PI/180) * Math.cos(#longitude_of_ascending_node * Math::PI/180) * Math.cos(#inclination * Math::PI/180))
z = x_prime * Math.sin(#argument_of_perhelion*Math::PI/180) * Math.sin(#inclination*Math::PI/180) + y_prime * Math.cos(#argument_of_perhelion*Math::PI/180) * Math.sin(#inclination*Math::PI/180)
return x, y, z
end
end
time = get_centuries_past_j2000
mars = Planet.new(1.52371034, 0.00001847, 0.09339410, 0.00007882, 1.84969142, -0.00813131, -4.553443205, 19140.30268499, -23.94362959, 0.44441088, 49.55952891, -0.29257343, time)
puts time
puts mars.mean_anomaly
puts mars.eccentric_anomaly
puts mars.J2000_ecliptic_plane
This may be helpful although I don't agree with arguments of perihelion for Earth. Longitude of perihelion is fine. The inclination is so small that it doesn't really apply for Earth as it does for other planets. And finding values for Omega is challenging. The perihelion is ever changing. FYI in 1248 AD it coincided with the winter solstice.
Firstly IAU has free SOFA C and FORTRAN lib's with standardized astronomical functions. Matric tables are included in certain routines so you don't have to go look them up.
But if you are so inclined to use old school methods then this site has what you need http://www.stjarnhimlen.se/comp/tutorial.html
NOVA C and JAVA, MICA, JPL catalogs, Jean Meeus book, AA USNO and a host of others beside wikipedia have tons of information. It looks like you want rectangular values so I think Paul Schlyter can help you out.
SOFA has these as well but documentation on how to use them is not going to teach the techniques. It will take a lot of research to understand them.
It looks like you are using Ruby and there is a wrapper gem for the SOFA lib named Celes. Just gem install celes and you'll have it.
Try looking at the fundamental arguments which all begin with fa:
** iauFal03 mean anomaly of the Moon
** iauFaf03 mean argument of the latitude of the Moon
** iauFaom03 mean longitude of the Moon's ascending node
** iauFame03 mean longitude of Mercury
** iauFave03 mean longitude of Venus
** iauFae03 mean longitude of Earth
** iauFama03 mean longitude of Mars
** iauFaju03 mean longitude of Jupiter
** iauFasa03 mean longitude of Saturn
** iauFaur03 mean longitude of Uranus
** iauFapa03 general accumulated precession in longitude
Have fun!
Edit Update:
Two functions in this gem that will give you heliocentric and barycentric x,y,z for Earth.
p is position and v is velocity.
h is heliocentric and b is barycentric.
pvh = Celes.epv00(jd_now, 0.0)[0]
pvb = Celes.epv00(jd_now, 0.0)[1]
sc = Celes.pv2s(pvh)
sc means spherical coordinates.
As you can see, all you need to provide is a JD time value.
So lots of good stuff in that gem and the SOFA C code.
I have yet to learn how to use them all.
I'm wondering if there's a way to calculate the distance of two GPS coordinates without relying on Google Maps API.
My app may receive the coordinates in float or I would have to do reverse GEO on the addresses.
Distance between two coordinates on earth is usually calculated using Haversine formula. This formula takes into consideration earth shape and radius. This is the code I use to calculate distance in meters.
def distance(loc1, loc2)
rad_per_deg = Math::PI/180 # PI / 180
rkm = 6371 # Earth radius in kilometers
rm = rkm * 1000 # Radius in meters
dlat_rad = (loc2[0]-loc1[0]) * rad_per_deg # Delta, converted to rad
dlon_rad = (loc2[1]-loc1[1]) * rad_per_deg
lat1_rad, lon1_rad = loc1.map {|i| i * rad_per_deg }
lat2_rad, lon2_rad = loc2.map {|i| i * rad_per_deg }
a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))
rm * c # Delta in meters
end
puts distance([46.3625, 15.114444],[46.055556, 14.508333])
# => 57794.35510874037
You can use the geokit ruby gem. It does these calculations internally, but also supports resolving addresses via google and other services if you need it to.
require 'geokit'
current_location = Geokit::LatLng.new(37.79363,-122.396116)
destination = "37.786217,-122.41619"
current_location.distance_to(destination)
# Returns distance in miles: 1.211200074136264
You can also find out the bearing_to (direction expressed as a float in degrees between 0-360) and midpoint_to (returns an object you can run .latitude and .longitude methods on).
Just a little shorter & separated parameter version of #Lunivore's answer
RAD_PER_DEG = Math::PI / 180
RM = 6371000 # Earth radius in meters
def distance_between(lat1, lon1, lat2, lon2)
lat1_rad, lat2_rad = lat1 * RAD_PER_DEG, lat2 * RAD_PER_DEG
lon1_rad, lon2_rad = lon1 * RAD_PER_DEG, lon2 * RAD_PER_DEG
a = Math.sin((lat2_rad - lat1_rad) / 2) ** 2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin((lon2_rad - lon1_rad) / 2) ** 2
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1 - a))
RM * c # Delta in meters
end
I'm not sure of any prepackaged solution, but it seems a fairly straightforward calculation: http://www.movable-type.co.uk/scripts/latlong.html
You can use the loc gem like this :
require 'loc'
loc1 = Loc::Location[49.1, 2]
loc2 = Loc::Location[50, 3]
loc1.distance_to(loc2)
=> 123364.76538823603 # km
Look at gem Geocoder(railscast)
If you store your coordinates in db, it calculate distance using database. But works good in other cases too.
Converted the accepted answer to Swift 3.1 (works on Xcode 8.3), in case anyone needs it:
public static func calculateDistanceMeters(departure: CLLocationCoordinate2D, arrival: CLLocationCoordinate2D) -> Double {
let rad_per_deg = Double.pi / 180.0 // PI / 180
let rkm = 6371.0 // Earth radius in kilometers
let rm = rkm * 1000.0 // Radius in meters
let dlat_rad = (arrival.latitude - departure.latitude) * rad_per_deg // Delta, converted to rad
let dlon_rad = (arrival.longitude - departure.longitude) * rad_per_deg
let lat1_rad = departure.latitude * rad_per_deg
let lat2_rad = arrival.latitude * rad_per_deg
let sinDlat = sin(dlat_rad/2)
let sinDlon = sin(dlon_rad/2)
let a = sinDlat * sinDlat + cos(lat1_rad) * cos(lat2_rad) * sinDlon * sinDlon
let c = 2.0 * atan2(sqrt(a), sqrt(1-a))
return rm * c
}
require 'rgeo'
point_1 = RGeo::Cartesian.factory.point(0, 0)
point_2 = RGeo::Cartesian.factory.point(0, 2)
p point_1.distance(point_2)
# => 2.0