How is it possible to take the x,z values produced by the accelerometer and translate it as values
that will represent a point in 360 degrees of the iphone rotation? (LANDSCAPED)
it can be -2 to 2 (0 for the middle point) and it can be 0 to 360, as long as it represents a value for the whole iphone rotation.
I need it for a Landscape game im making
what is the best solution in that case?
Use the atan2() function. To get a value in degrees:
#include <math.h>
...
float degrees = atan2(x, y) * 180 / 3.14159;
Related
I'm working on a spritekit game using swift and I implemented a joystick using this library. I'm having a hard time trying to figure out how to calculate the degree of the rotation. the library gives you this information when you move the joystick around
joystick.trackingHandler = { jData in
// something...
// jData contains angular && velocity (jData.angular, jData.velocity)
}
I don't need to rotate the player since the game is a 4 directional jrpg style, so i'll just be triggering movement based on a range of degrees.
does anybody have any useful articles or information on turning the velocity returned into a degree?
I've seen people using atan2 but it seems to only accept Double's and the velody returned is of type CGPoint.
The angular value in jData contains the angle in radians.
To convert, use the following code for values between -180 and +180:
let degrees = jData.angular * 360 / (2 * M_PI)
and this for values between 0 and +360:
var radians = jData.angular
radians = radians > 0 ? radians : (2 * M_PI + radians)
let degrees = radians * 360 / (2 * M_PI)
adapted from https://stackoverflow.com/a/1311134/968577
I am trying to convert radians to degrees. I already know:
1 radian = 180/PI This does give me degrees.
You can check out my code here: github
Here is my problem:
I am getting radian data that only goes from 0 to 1.5 and then back to 0 then from 0 to -1.5 and back to 0. So when I do the conversion I get 0 to 90 and back to 0 and then 0 to -90 and back to 0.
What I want is to be able to go from 0 to 360 as I move the pitch of the Iphone. I am pretty sure its an easy map and when I see it i will slam my hand into my forehead.
Thanks for the help!
I think your issue is a typo.
The function in your repo is:
var degrees = (radians * M_PI)
but as you noted at the beginning of your question, it should be
var degrees = radians * (180.0 / M_PI)
double = rollingZ = acceleration.x;
double = rollingX = acceleration.y;
if (rollingZ > 0.0) {
self.centerCoordinate.inclination = atan(rollingX / rollingZ) + M_PI / 2.0; //LINE 1
}
else if (rollingZ < 0.0) {
self.centerCoordinate.inclination = atan(rollingX / rollingZ) - M_PI / 2.0; // LINE 2
}
else if (rollingX < 0) {
self.centerCoordinate.inclination = M_PI/2.0; //atan returns a radian
}
else if (rollingX >= 0) {
self.centerCoordinate.inclination = 3 * M_PI/2.0;
Im just trying to fully understand this piece of code. I'm looking to build AR apps on the iphone and this code has the function of calculating the angle of inclination of the device using the accelerometer readings.
My understanding is this:
Assuming a portrait orientation if i roll the device forward the x axis of the accelerometer increases towards a negative number of -1.0 (i.e. the device is laid flat with the screen facing up). If i tilt the device towards me the x axis value increases towards a value of 1.0 (until the device is flat facing the ground).
The y axis changes up and down its axis between -1.0 and 0.0 (0 implies the device is horizontal).
If we take some example readings say x = 0.5 (a -45 degree angle, tilting the device towards me) and y = 0.8. If i plotted this on a cartesian coordinate graph with y (rollingX as the vertical axis) and x (rollingZ as the horizontal) and draw a line between them i understand that i can use the reverse tangent function (atan) to calculate the angle. My confusion comes on line 1. I dont understand why that line adds 90 degrees (in radians) to the calculated angle given by the atan function?
I just cant seem to visualise on a graph whats going on. If someone could shed some light on this - that would be much appreciated.
I suppose that these +90 degrees or -90 degrees (in case of negative rollingZ) are added to bring inclination value to widely used Polar coordinate system with angle between -180 and 180 degrees.
Assuming that you have Z line projecting upward when you look at the screen of the device and Z line looking at you from the screen, the result of calculations above vill give you an angle between screen plane and horizontal plane.
Let us assume that acceleration value is positive when it is goes "inside" the device:
1) Device is in vertical position, we have rollingZ = 1, rollingX = 0. The code returns 90 degrees.
2) Device is tilted towards user. Let rollingZ be 0.7 and rollingX be -0.7. This will give us 45 degree angle.
3) Device is in upside-down position, now we have rollingZ = -1 and rollingX = 0, and it is -90 degrees.
For example,
sin 33.35 = 0.5523 in degree
sin 33.35 = 0.9347 in radian
As Xcode gives answers by default in radian.
So is there any way to get answer in degree ???
Yes, you multiply radians by a constant, 180/pi, to get degrees. That's because there are 2 * pi radians in a complete circle of 360 degrees (so half a circle is pi radians and 180 degrees).
Just keep in mind that's a conversion you have to apply to the input of the sine function (the angle), not the output (which is a length).
The pi constant can be used by including math.h and using the M_PI symbol.
A full circle in degrees exists of 360 degrees, and a full circle in radians is 2*pi. So, to convert radians to degrees, you divide by pi and multiply by 180.
radians * 180 / M_PI
If you would convert from degrees to radians, for example to provide degrees for a animation, use this:
degrees * M_PI / 180
I am using #DEFINEs for this myself:
#define DEGREES(radians)((radians)*180/M_PI)
#define RADIANS(degree)((degree)*M_PI/180)
Simply use by saying RADIANS(degree) or DEGREES(radians) in any part of your code, where you replace the degree and radians by the degree or radian value you had. This also keeps it more readable in my opinion if you are not used to radians.
Get the value in radians, then do:
CGFloat degrees = (radians * 180) / M_PI;
M_PI is a #define located in math.h.
Fortunately, the Objective-C language is a proper superset of C, which includes multiplication and division operators.
There are two views:
viewA and viewB. Both are rotated.
The coordinate system for rotation is weird: It goes from 0 to 179,999999 or -179,99999 degrees. So essentially 179,99999 and -179,99999 are very close together!
I want to calculate how much degrees or radians are between these rotations.
For example:
viewA is rotated at 20 degrees
viewB is rotated at 30 degrees
I could just do: rotationB - rotationA = 10.
But the problem with this formula:
viewA is rotated at 179 degrees
viewB is rotated at -179 degrees
that would go wrong: rotationB - rotationA = -179 - 179 = -358
358 is plain wrong, because they are very close together in reality. So one thing I could do maybe is to check if the absolute result value is bigger than 180, and if so, calculate it the other way around to get the short true delta. But I feel this is plain wrong and bad, because of possible floating point errors and unprecision. So if two views are rotated essentially equally at 179,99999999999 degrees I might get a weird 180 or a 0 if I am lucky.
Maybe there's a genius-style math formular with PI, sine or other useful stuff to get around this problem?
EDIT: Original answer (with Mod) was wrong. would have given 180 - right answer in certain circumstances (angles 30 and -20 for example would give answer of 130, not correct answer of 50):
Two correct answers for all scenarios:
If A1 and A2 are two angles (between -179.99999 and 179.99999,
and Abs means take the Absolute Value,
The angular distance between them, is expressed by:
Angle between = 180 - Abs(Abs(A1 - A2) - 180)
Or, using C-style ternary operator:
Angle between = A1 < 180 + A2? A1 - A2: 360 + A1 - A2
Judging from the recent questions you've asked, you might want to read up on the unit circle. This is a fundamental concept in trigonometry, and it is how angles are calculated when doing rotations using CGAffineTransforms or CATransform3Ds.
Basically, the unit circle goes from 0 to 360 degrees, or 0 to 2 * pi (M_PI is the constant used on the iPhone) radians. Any angle greater than 360 degrees is the same as that angle minus a multiple of 360 degrees. For example, 740 degrees is the same as 380 degrees, which is the same as 20 degrees, when it comes to the ending position of something rotated by that much.
Likewise, negative degrees are the same as if you'd added a multiple of 360 degrees to them. -20 degrees is the same as 340 degrees.
There's no magic behind any of these calculations, you just have to pay attention to when something crosses the 0 / 360 degree point on the circle. In the case you describe, you can add 360 to any negative values to express them in positive angles. When subtracting angles, if the ending angle is less than the starting angle, you may also need to add 360 to the result to account for crossing the zero point on the unit circle.
Let's try this again:
There are two angles between A and B. One of them is
θ1 = A - B
The other is
θ2 = 360 - θ1
So just take the minimum of those two.
In addition to Brad Larson's excellent answer I would add that you can do:
CGFloat adjustAngle(angle) { return fmod(angle + 180.0, 360.0); }
...
CGFloat difference = fmod(adjustAngle(angle1) - adjustAngle(angle2), 360.0);
Take the difference, add 360, and mod by 360.