UIAccelerometer Detecting angle Problem - iphone

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));

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;
};

apply force to a body in touchesMoved direction in box2d iphone

I am new to box2d. I have a problem i.e
when i touch the body and touchmoved(In touchmoved method i don't want move the body)upto some place and touch released.
in touchend i apply force like this...
b2Vec2 force = b2Vec2(0.0,15.0);
ballBody->SetLinearVelocity(force);
and find out the angle b/w touchbegin point to touchend point in this way
float angleRadians = atan2(touchBegin.x - touchEnd.x, touchBegin.y - touchEnd.y);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
cocosAngle1 = -1 * angleDegrees;
in that time,how to apply force to the body with cocosAngle1(touchend direction). Like paper in "Paper Toss" game.
how to apply force in touchmoved direction of body?...
please explain to me...

What is angle in CATransform3DRotate method?

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)

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

How to rotate image around center point automatically with finger touch

On iPhone, how to implement rotating image around the center point using finger touch ?
Just like wheel, if you put finger on the iPhone screen , then move suddenly, then the image becoming rotating around center point just like the wheel, after a while, it becomes more and more slow , finally stop.
Who can help to give some pieces of codes (Object-C) or some suggest ?
I was working with a "spin the bottle"-app yesterday. On the window I have a ImageView with an bottle that's suppose to response to touches and rotate the way the user swipes his finger. I struggled to get my ImageView to rotate during the touch-events (TouchesBegan, Touchesoved, TouchesEnd). I used this code in TouchesMoved to find out the angle in witch to rotate the image.
public override void TouchesMoved (NSSet touches, UIEvent evt)
{
PointF pt = (touches.AnyObject as UITouch).LocationInView(this);
float x = pt.X - this.Center.X;
float y = pt.Y - this.Center.Y;
double ang = Math.Atan2(x,y);
// yada yada, rotate image using this.Transform
}
THIS IS IMPORTANT! When the ImageView rotates, even the x & y-coordinates changes. So touching the same area all the time would give me different values in the pt and prePt-points. After some thinking, googeling and reading I came up with an simple solution to the problem. The "SuperView"-property of the ImageView.
PointF pt = (touches.AnyObject as UITouch).LocationInView(this.SuperView);
Having that small change in place made it alot easier, no i can use the UITouch-metohs LocationInView and PreviousLocationInView and get the right x & y coordinates. Her is parts of my code.
float deltaAngle;
public override void TouchesMoved (NSSet touches, UIEvent evt)
{
PointF pt = (touches.AnyObject as UITouch).LocationInView(this.Superview);
float x = pt.X - this.Center.X;
float y = pt.Y - this.Center.Y;
float ang = float.Parse(Math.Atan2(dx,dy).ToString());
//do the rotation
if (deltaAngle == 0.0) {
deltaAngle = ang;
}
else
{
float angleDif = deltaAngle - ang;
this.Transform = CGAffineTransform.MakeRotation(angleDif);
}
}
Hope that helped someone from spending hours on how to figure out how to freaking rotate a bottle! :)
I would use the affine transformations - yuou can assign a transformation to any layer or UI element using the transform property.
You can create a rotation transform using CGAffineTransform CGAffineTransformMakeRotation( CGFloat angle) which will return a transformation that rotates an element. The default rotation should be around the centerpoint.
Be aware, the rotation is limited to 360 degrees, so if you want to rotate something more than that (say through 720 degrees) - you have to break the rotation into several sequences.
You may find this SO article useful as well.
The transform property of a view or layer can be used to rotate the image displayed within. As far as the spinning part goes, you just track the location and movement of touches in your view with touchesBegan, touchesMoved, and touchesEnded.
Use the distance and time between the touches updates to calculate a speed, and use that to set a rotational velocity. Once you start the image spinning, update the position periodically (with an NSTimer, maybe), and reduce the rotational velocity by some constant.