What is angle in CATransform3DRotate method? - iphone

I am working on a application showing my heading and bearing to the location I want to go. I want to display a arrow direction to the direction user should have his heading to rich the desire location. There is nothing to get worry about this. What I want to do is I have degrees.. 0-360.
When I pass these degrees to the angle in bellow method this react strange. The Image get totate more than once.
I tried to get radians from the degrees and than tried but no success.
All I want is I have float value range between 0-360 and I wanted to rotate my image only once a 369 degree rotation.
I have passed radians too. For converting degrees to radians I used:
float radian = degrees * M_PI / 180
But this is also having same issue. All I want is image show work like a compass for all 360 degrees.
How to get angle for this method:
CATransform3DRotate (CATransform3D t, CGFloat angle, CGFloat x, CGFloat y, CGFloat z)'
Thank you in advance

You should pass radians there. For example, this will rotate image on 180 degrees:
switcher.transform = CGAffineTransformMakeRotation(M_PI);
if you have problems with converting degrees to radians, you can use following macroses:
#define DegreesToRadians(degrees) (degrees * M_PI / 180)
#define RadiansToDegrees(radians) (radians * 180 / M_PI)

Related

Finding out the change on the [x,y] plane using core motion and gyroscope

When I tilt the iphone on the xy plane how we can get how much it has been tilted?
IPhone is being held as vertical upright position
CMMotionManager
CMMotionManager works with NSOperationQuee.
CMMotionManager provides the startAccelerometerUpadatesToQueue:withHandler method to start Core Motion data collecting.
And this method uses a threaded context by implementing an NSOperationQueue and an execution block that confomrs to the CMAccelerometerHandler format.
Core Motion data is always provided as radians. If you want degrees you will need to convert the data using the following formulas:
CGFloat DegreesToRadians(CGFloat degrees) {
return degrees * M_PI / 180;
};
CGFloat RadiansToDegrees(CGFloat radians) {
return radians * 180 / M_PI;
};

Logic of using UISlider Xcode

I'm stuck at the moment controlling UISlider in XCode. I've made a horizontal slider in Interface Builder and manage to code the controls in xcode. Right now I'm just not sure how to code the logic. If the user slides the nib to the left, I'd it like it to rotate my image counter-clockwise and if the user slides it to the right, rotate the image clockwise.
Minimum value of slider I've set is 0 and max is 100. Initial value is 50.
For transformation I'm using CGAffineTransformMakeRotation(myAngle). I've set myAngle to float. As for the condition, here's a snippet of code:
mySlider.value = myAngle; // This is for CGAffine
if(myAngle < 50) {
myAngle -= 0.1;
}
if(myAngle > 50) {
myAngle += 0.1;
}
When I slide the nib, it only rotates anti-clockwise. What's the best logic can I use? Hope someone can help. Thanks.
-Hakimo
Note that the Doc says about the angle as,
The angle, in radians, by which this matrix rotates the coordinate system axes. In iOS, a positive value specifies counterclockwise rotation and a negative value specifies clockwise rotation. In Mac OS X, a positive value specifies clockwise rotation and a negative value specifies counterclockwise rotation.
I think something like the following should work. I am not sure though. (Considering that the minimumValue is 0).
float middleValue = slider.maximumValue / 2;
float value = middleValue - slider.value;
float degrees = (middleValue / 180) * value;
float radians = degrees * (3.14/180);
If you want a fixed position on the slider to correspond to a fixed angle of rotation, then you can do something like this:
float degreesToRadians = M_PI / 180.0;
CGFloat angle = (mySlider.value - 50) * degreesToRadians;
myView.transform = CGAffineTransformMakeRotation(angle);
On the other hand, if want more complex behavior, such as that the slider snaps to the middle except when being dragged, and how far it's dragged determines the speed of rotation of your view, then I would suggest using a timer. Set up an NSTimer to call a given selector, and in that selector do something like this:
- (void)timerFired:(NSTimer *)aTimer {
CGFloat angleDelta = (mySlider.value - 50) / 500;
// I'll assume myAngle is an instance variable.
myAngle += angleDelta;
myView.transform = CGAffineTransformMakeRotation(myAngle);
}
And somewhere else you need to write a method to set mySlider.value = 50 when the user stops dragging it, which you can do by adding a target/action for UIControlEventTouchUpInside and ...Outside on the slider.
If you want, you can make the rotation appear very smooth by using UIView animations each time you set the transform, using an animation duration equal to the NSTimer interval. If you do that, then I would actually suggest using the animation callback in place of the timer method, to avoid any possible crossover between the two events.

Iphone : OpenGL glLookAt : Getting the rotation angle

I am using glLookAt to control my camera view.
I use the following code to rotate around.
SPEED_TURN is a Const of 0.16 (speed we turn at)
GLfloat v[] = {[self centerAtIndex:0] - [self eyeAtIndex:0],
[self centerAtIndex:1] - [self eyeAtIndex:1],
[self centerAtIndex:2] - [self eyeAtIndex:2]};
[self setCenter:[self eyeAtIndex:0] + cos(SPEED_TURN / 2)*v[0] - sin(SPEED_TURN / 2)*v[2] atIndex:0];
[self setCenter:[self eyeAtIndex:2] + sin(SPEED_TURN / 2)*v[0] + cos(SPEED_TURN / 2)*v[2] atIndex:2];
My question is, how to I get the angle of the camera in degrees?
I tried this
rotAngleDegs = (cos(-SPEED_TURN)*v[0] - sin(-SPEED_TURN)*v[2]) * 180 / PI
However that give numbers from -620 to +620
I don't think you are trying the right functions to give you the angle. The functions "cos()" and "sin()" both take your angle and map it to [-1,1]. If SPEED_TURN is the angle in radians, then you would just use:
float angleInDegrees = (SPEED_TURN * 180) / M_PI;
Take the degress you want and multiply them by pi divided på 180. Then you get the radians to enter:
Radians = Degrees * (pi / 180)
To get the degrees from radians:
Degrees = Radians * (180 / pi)

how to rotate a UIView to face a point

I have a UIView that I want to rotate when I move my finger across the screen (like in a circle). I want the UIView to rotate so that it faces my touchpoint. I then want to shoot something from my UIView (like a bullet) in the direction that the UIView is facing. Any Ideas???
Glad I remember triginometry
-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{
float dX = touchPoint.x-objPos.x; // distance along X
float dY = touchPoint.y-objPos.y; // distance along Y
float radians = atan2(dY, dX); // tan = opp / adj
//Now we have to convert radians to degrees:
float degrees = radians*M_PI/360;
return degrees;
}
Once you have your nice method, just do this:
CGAffineTransform current = view.transform;
[view setTransform:CGAffineTransformRotate(current, [view degreesTorotateObjectWithPosition:view.frame.origin andTouchPoint:[touch locationInView:parentView]]
//Note: parentView = The view that your object to rotate is sitting in.
This is pretty much all the code that you'll need.The math is right, but I'm not sure about the setTransform stuff. I'm at school writing this in a browser. You should be able to figure it out from here.
Good luck,
Aurum Aquila

UIAccelerometer Detecting angle Problem

How to detect the iPhone angle?
I mean that when iPhone is straight (i.e) HomeButton is in bottom part, so how to detect 90 Degree from UIAccelerometer Method.
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
i try RadiansToDegrees(atan2(acceleration.y, acceleration.x)) but it gives -90 degree angle
any helpwould be appreciated.
If it's just an issue of sign, use
return - RadiansToDegrees(atan2(acceleration.y, acceleration.x));