I'm trying to calculate the angle of the click i am making in relationship to the middle of the screen. But maybe i am confused on how atanf is suppsoed to work.
CGPoint pt = [self convertTouchToNodeSpace:[touches anyObject]];
float adj = pt.x - 512;
float opposite = pt.y - 384;
float combined = opposite / adj;
float tan = atanf(combined);
but when i try to NSLog Tan, i just get some giant number like 0.1253649
thoughts?
The right way to convert vector to angle is through atan2 function:
float angle = atan2f (pt.y - 384, pt.x - 512) * 180 / PI;
PS: Are you using cocos2d engine? It has ccpToAngle(...) function.
Related
I have a Ring(width 25px) as UIView. When User selects any where on the ring, I want to Calculate the Angle between the the Points Selected on a fixed point on the circle considering the Center of Circle.
I have found few examples but they are not taking center into consideration.
What is the Optimum way to do this ?
You'll have to handle the code yourself (I'm a Java developer), but the simplest way to get the angle between two points on a circle (measured against its center) is to recall the geometry of the situation. The triangle formed by any two points on the circumference of a circle and the circle's center is necessarily isosceles.
An isosceles triangle, recall, has (at least) two sides of the same length -- the radial segments to your two points. Bisecting the angle results in a radial segment which is perpendicular to and bisects the line connecting the two points. This forms a pair of right triangles, with the radius as the hypotenuse, and half the distance between the two points as the 'opposite' side.
Moving the factor of two to the denominator and recognizing what twice the radius is, simply calculate the distance between the two points (on the circumference), and divide it by the diameter. The value you get is the sine of the half-angle (you desire the whole angle). Take the arcsine, and you'll have half your angle:
θ/2 = sin-1(d/D)
With d as the distance between the two points, and D as the diameter of the circle. Since the diameter is given, and the distance between the two points is simple to calculate, getting to this point should be easy, and then you just need to double the value calculated to get the whole angle between the two points.
This might help you, I have used same thing in one of my projects.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
beginPoint = [[[event allTouches] anyObject] locationInView:self];
currentAngle = 0.0f;
centerPoint = self.center;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
endPoint = [[[event allTouches] anyObject] locationInView:self];
float fromAngle = atan2(beginPoint.y - self.center.y, beginPoint.x - self.center.x);
float toAngle = atan2(endPoint.y - self.center.y, endPoint.x - self.center.x);
float newAngle = [self wrapd:currentAngle + (toAngle - fromAngle) min:0 max:2 * 3.14];
currentAngle = newAngle;
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
[self setTransform:cgaRotate];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
endPoint = [[[event allTouches] anyObject] locationInView:self];
float fromAngle = atan2(beginPoint.y - self.center.y, beginPoint.x - self.center.x);
float toAngle = atan2(endPoint.y - self.center.y, endPoint.x - self.center.x);
float newAngle = [self wrapd:currentAngle + (toAngle - fromAngle) min:0 max:2 * 3.14];
currentAngle = newAngle;
}
- (double) wrapd:(double)_val min:(double)_min max:(double)_max {
if(_val < _min) return _max - (_min - _val);
if(_val > _max) return _val - _max; /*_min - (_max - _val)*/;
return _val;
}
As we know
((center.x + radius*cos(theta)) ,(center.y + radius*sin(theta))) ~ (X,Y)
where (X,Y) belongs to the any circumference point
so you can calculate the Angle i.e. theta for any Circumference point as:
X= center.x + radius*cos(theta)
cos(theta) = (X - center.x)/radius .......... eqn-1
similarly
Y= center.y + radius*sin(theta)
sin(theta) = (Y - center.y)/radius .......... eqn-2
By dividing eqn-2 by eqn-1 we have
tan(theta) = (Y - center.y)/(X - center.x) -----------------Final Equation
I am working on an app that has a rotating image (the user tapps and drags and the image rotates in a circle tracking their finger). What I am trying to keep track of is how many times the user makes a complete circle. An additional "hitch" is that I also need to know if the user is circling clockwise vs counter clockwise.
Here is the Code that is rotating the image... Please feel free to request additional information.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
long double rotationNumber = atan2(touchPoint.y - originY, touchPoint.x - originX);
totalRotationCount ++;
schedulingWheel.transform = CGAffineTransformMakeRotation(rotationNumber);
offset = (rotationNumber * 100)/14;
dateRibbon.center = CGPointMake(offset, 24);
}
Thanks for the help!
My solution isn't elegant and there might be a cleaner solution I'm missing but this is what I did recently. The trick is to keep track of the angle from the last time touchesMoved: is called. Then, add the delta of your current angle and the stored angel value to your total.
The problem is the "boundaries" that atan2 creates needed ugly code to overcome. Say your lastAngle is 359 and you cross the origin so your next angle is 1. The difference will not be 2 but -358, so whenever you cross that boundary your total will be reset to 0.
Here is what I did:
CGFloat angle = atan2f(center.y - point.y, point.x - center.x);
//Translate to Unit circle
if (angle > 0) {
angle = (M_PI - angle) + M_PI;
} else {
angle = fabsf(angle);
}
CGFloat delta = angle - lastAngle;
//Adjust for boundaries
if (fabsf(delta) > (2*M_PI)-fabsf(delta)) {
BOOL greaterThanZero = (delta > 0);
delta = (2*M_PI)-fabsf(delta);
if (greaterThanZero) {
delta = -1 * delta;
}
}
totalAngle += delta;
lastAngle = angle;
The big/ugly conditional under "Adjust for boundaries" basically just looks to see if there is a shorter angle to get to the new point (So, 2 instead of -258) & assumes that if there is it probably means you crossed that origin and adjusts the delta accordingly.
I translated the Atan2 results so that it represents a full unit circle from 0 to 2π. Bonus side affect, it then accounts for clockwise/counter clockwise movement better than the standard -π to π of Atan2.
To find out what is the total number of the rotations simply sum all the rotation angles in either directions. For clockwise the value of the rotation angle will be positive and for the counter clockwise it will be negative. Then divide it by a pi (~3.14) to get your total number of rotations.
long double rotationNumber = atan2(touchPoint.y - originY, touchPoint.x - originX);
long double totalRotationsAngle += rotationNumber;
Then whenever you want to get the number of full rotations:
double numberOfRotations = floor(totalRotationsAngle/M_PI);
I'm trying to implement a small radar that plots targets based on latitude and longitude coordinates similar to the radar in the Layar AR iPhone app. I have the compass and locationManager working to get the lat/lon, heading and distance between two points. However I'm having trouble plotting the points onto the x-y plane. Could you point me in the right direction(so-to-speak)?
This is the method that I am using to plot but the results are not correct:
-(void) addTargetIndicatorWithHeading:(float)heading andDistance:(float)distance{
//draw target indicators
//need to convert radians and distance to cartesian coordinates
float radius = 50;
float x0 = 0.0;
float y0 = 0.0;
//convert heading from radians to degrees
float angle = heading * (180/M_PI);
//x-y coordinates
float x1 = (x0 + radius * sin(angle));
float y1 = (y0 + radius * cos(angle));
TargetIndicator *ti = [[TargetIndicator alloc] initWithFrame:CGRectMake(x1, y1, 5, 5)];
[self addSubview:ti];
[ti release];
}
I guess the problem lies within the present view's origin coordinate not being added to ur coordinate.
just modify your x1 and y1 by adding the origin.x and origin.y of the current view to which you add ti as a subview.
I figured out what was wrong but I'm don't know the reasoning behind it. First I should not have been converting the radians to degrees. This gives me the correct positioning but it is rotated 180 degrees. So to fix it, I subtract the radians from PI.
Here is the solution:
-(void) addTargetIndicatorWithHeading:(float)heading andDistance:(float)distance{
//draw target indicators
//need to convert radians and distance to cartesian coordinates
float radius = 50;
//origin offset
float x0 = 50.0;
float y0 = 50.0;
//convert heading from radians to degrees and rotate by 180 deg
float angle = M_PI - heading;
float x1 = (x0 + radius * sin(angle));
float y1 = (y0 + radius * cos(angle));
TargetIndicator *ti = [[TargetIndicator alloc] initWithFrame:CGRectMake(x1, y1, 5, 5)];
[self addSubview:ti];
[ti release];
}
I am making an iphone app where a ball will roll around the screen based on how the user tilts the device. If the device is lies flat on the table theoretically the ball would not move. If the device is tilted standing completely upward the I want the ball to roll straight down at maximum speed. The speed depends on how far from the flat position the device is tilted. Also, it also works for if the user tilts right or left or up or combinations of the four. I am using the accelerometer right now and the ball moves and it works okay, I am just not real familiar with physics. If someone has any suggestions on how to get this to work smoothly please let me know.
Thanks!
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float xx = -[acceleration x];
float yy = [acceleration y];
float z = -[acceleration z];
z = 1 - z;
NSString * zaxis = [NSString stringWithFormat:#"%f", z];
lblz.text = zaxis;
lbly.text = [NSString stringWithFormat:#"%f", yy];
lblx.text = [NSString stringWithFormat:#"%f", xx];
CGFloat newx;
CGFloat newy;
if (yy > 0)
{
newy = ball.center.y - ((1 - yy) * z);
}
else
{
newy = ball.center.y + ((1 - yy) * z);
}
if (xx > 0)
{
newx = ball.center.x - ((1 - xx) * z);
}
else
{
newx = ball.center.x + ((1 - xx) * z);
}
CGPoint newPoint = CGPointMake(newx, newy);
ball.center = newPoint;
If you want to make it look more realistic and leverage existing stuff, look at some of the existing physics engines and 2d frameworks, Box2d and Cocos2d, but there are many others.
I think the key thing you are messing here is the difference between acceleration and velocity. You want the 'amount of tilt' to work as an acceleration. Each frame the balls Velocity should change by the acceleration, then the balls position should change by the balls velocity.
So just in X it should be something like:
float accelX = acceleration.x;
mVel.x += accelX; \\mVel is a member variable you have to store
ball.center.x += mVel.x;
---More Complex version
Now the more I think about it, it might not be the 'amount of tilt' you want to be the acceleration. You might want the amount of tilt to be the 'Target Velocity.' But you still want to use an acceleration to get there.
mTargetVel.x = acceleration.x;
//Now apply an acceleration to the velocity to move towards the Target Velocity
if(mVel.x < mTargetVel.x) {
mVel.x += ACCEL_X; //ACCEL_X is just a constant value that works well for you
}
else if(mVel.x > mTargetVel.x) {
mVel.x -= ACCEL_X;
}
//Now update the position based on the new velocity
ball.center.x += mVel.x;
i have implementing moving image using UIAccelerometer.I have used below code.
code:
float gravX = (acceleration. x * kFilteringFactor) + (gravX * (1 - kFilteringFactor));
float gravY = (acceleration. y * kFilteringFactor) + (gravY * (1 - kFilteringFactor));
float moveX = acceleration. x - gravX;
float moveY = acceleration. y - gravY;
CGPoint MoveCenter = [moveImage center];
float Movex = moveX * 30 + MoveCenter. x;
float Movey = moveY * 30 + MoveCenter. y;
moveImage.center = CGPointMake(Movex, Movey);
In this code there is one problem.If i move device on top side then image is moving on left side,if device is move left side then image is moving top side.can you give me advice.
Maybe is a problem of the coordinates. I'm not sure this is the problem but in Quartz the (0,0) is on bottom left while on UIKit is on top left.
Try to change the coordinates with frame.origin.x and frame.origin.y instead of using CGPoint.