# atan2() & slope question

Discussion in 'iOS Programming' started by larswik, Jun 4, 2014.

1. ### larswik macrumors 68000

Joined:
Sep 8, 2006
#1
I am having an issue with the computer firing back a cannon ball. It keeps hitting the hill. In the photo the 2 red "X"'s are located at the cannon ball start, or the object.frame.origin.x and y. I wanted to make sure my 2 points were correct. You can also see the cannon ball striking the frame of the hill.

When I researched this it was the slope, or RISE over RUN. I built a method to convert the slope to degrees. In the RISE it was normally Y2 - Y1 but since Apple screen coordinates start at the top and work down they were reserved, so I flipped them to Y1- Y2. Other wise it was shooting backwards of something.

Code:
```#define PI 3.1415926536

-(float)collectDataForShot_Castel:(NSMutableDictionary*)data{
CGPoint canXY = canRect.origin;
CGPoint hilXY = hillRect.origin;

float rise = (canXY.y - hilXY.y);
float run = hilXY.x - canXY.x;

float degree = atan2(rise,run) * 180 / PI;

return degree;
}
```
Once that Method is called it adds a velocity executes the fire method. located here which basicly just converts the Degrees to Radians.

Code:
```-(void)fireCannonBall{
openFinished = YES;
incomingSoundPlayed = NO;

if (userTurn) {
[velSlider setHidden:YES];
[angSlider setHidden:YES];

[fireButton setEnabled:NO];
[fireButton setHidden:YES];

angle_R *= 3.1415926536 / 180; //Convert to radians
}
else{
angle_L *= 3.1415926536 / 180; //Convert to radians
}
[cannonAudio play];
}
```
The Objects values at the time of the calculations are "Red X's"
Which gives me a degree value of 29.619 before converting to Radians which is .516956

I can see with my own eyes that 29 degrees is lower then it should be. It looks closer to 45 give or take.

Any help would he appreciated.

File size:
73.4 KB
Views:
75
2. ### carnite macrumors newbie

Joined:
Jun 5, 2014
#2
I got the same result as you with those figures.

If I put my protractor up on my monitor, the angle between the origin of the cannon (which isn't where your red mark is, btw, unless you have a much smaller view and aren't clipping) and the origin of the hill is about 29 degrees.

tan is opp/adj which is 112/197 = 0.5685. So tan^-1 = 29 degrees

45 degrees would take it into the sun.

(The origin is at the top left of the view.)

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#3
I can't contribute to the actual problem, but I am curious as to why you are converting from radians to degrees and back and not using PI in the method?

Does the cannonball travel in a straight line without any kind of gravity?

B

4. ### larswik thread starter macrumors 68000

Joined:
Sep 8, 2006
#4
Ya, its frustrating and this is the 3rd day I have been looking at the math. If you are also coming up with 29 degrees then the math must be right. To test it I turned off the gravity and wind speed so it shoots in a straight line

Balamw - Looking at the code you pointed out I might have found the answer. But can't test it till I get home tonight. I just noticed that in the method that returns degrees I divided 180 / PI

Code:
```float degree = atan2(rise,run) * 180 / PI;
```
But in the fire method below I convert PI / 180

Code:
```angle_L *= 3.1415926536 / 180; //Convert to radians
```
I think the problem is there. The different conversions and back again from methods. Like carnite said the math appears to be correct, the problem must be in these conversions.

I'll post what I find out!

Thanks!

5. ### larswik thread starter macrumors 68000

Joined:
Sep 8, 2006
#5
Solved it. I was also begin converted again like you pointed out balawm. Once that was fixed and I stopped it from reconverting that solved the problems.

Thanks for your help point that out.