# Calculate Gravity

Discussion in 'iOS Programming' started by larswik, May 24, 2014.

1. ### larswik macrumors 68000

Joined:
Sep 8, 2006
#1
I was working on part of a game and was creating gravity with a bouncing ball. I included the project file so you can see what it is doing. It seems to work pretty well except for the hang time before the UIImageView reverses directions and heads back down again. If the UIImageView reaches the top, bottom or a certain speed it reverses direction.

This is just part of me learning new things since I had time this weekend to code again. Is there a better way to calculate gravity in programming?

The reason I am doing this is that I would like to start on a game I played in the early 80's. Some of you might remember the 2 canons with a mountain between them. Every game had a new wind speed and the goal was to destroy your opponents canon by angling the canon and shooting . So I thought this weekend I would play around with creating gravity.

Code:
```#import "ViewController.h"

float calDistance(float num1, float num2);

@implementation ViewController

{
speed = 1.0;
ballMovment = 0.0;
switchDirection = NO;
downDirection = YES;
}

{
// Dispose of any resources that can be recreated.
}

-(void)moveRock{
UIImageView *view = (UIImageView*) [self.view viewWithTag:5];
CGRect viewRect = view.frame;

if (downDirection) {
speed *= 1.04;
}
else{
speed *= 0.95; // speed for up direction to slow down
}
// if the speed is below a speed of 0.3 switch directions
float distance = calDistance(ballMovment, speed);
if (distance < 0.3 && !downDirection) {
switchDirection = YES;
}

if (viewRect.origin.y < 0 || viewRect.origin.y > 540 || switchDirection) {
speed = speed * -1; // flip direction
downDirection = (downDirection) ? NO : YES;
switchDirection = NO;
}
// Update the rect
CGRect newRect = CGRectMake(viewRect.origin.x, viewRect.origin.y + speed, viewRect.size.width, viewRect.size.height);
view.frame = newRect;
}

float calDistance(float num1, float num2){
// This calculates the speed of the ball
float number = 0.0;

number = num1 - num2;
if (number < 0) {
number = number * -1;
}

NSLog(@"called %.2f",number);
return number;
}

@end

```

#### Attached Files:

• ###### Ball bounce.zip
File size:
126.3 KB
Views:
36
2. May 24, 2014
Last edited: May 24, 2014

### DennisBlah macrumors 6502

Joined:
Dec 5, 2013
Location:
The Netherlands
#2
Code:
```// if the speed is below a speed of 0.3 switch directions
float distance = calDistance(ballMovment, speed);
if (distance < 0.3 && !downDirection) {
switchDirection = YES;
}
```
The 'hang time' is determined by your switchdirection part see above.
If you raise the distance < 0.3 it will come down quicker

And to make it bounce nice on the bottom of the screen change the following:
Code:
```viewRect.origin.y > 540
```
to
Code:
```viewRect.origin.y > (self.view.frame.size.height - viewRect.size.height)
```
Hope it helps
Cheers

3. ### DennisBlah macrumors 6502

Joined:
Dec 5, 2013
Location:
The Netherlands
#3
That game is awesome

4. ### larswik thread starter macrumors 68000

Joined:
Sep 8, 2006
#4
Yes, increasing that speeds up the hang time. If you download the program and run it you will see that they ball looks normal at first but as the bounces get smaller and smaller it seems that it should speed up to look more normal.

That is why I am wondering if there is some algorithm to calculate acceleration? I found this site http://www.uh.edu/engines/epi1534.htm that talks about acceleration 32 feet per second per second. I am wondering if there is a more simple way to write what I wrote?

5. ### JohnsonK macrumors regular

Joined:
Mar 6, 2014
#5

You could achieve what you want with a few lines of code

6. ### larswik thread starter macrumors 68000

Joined:
Sep 8, 2006
#6
HA! Of course there is a class for everything. I am still running 6.1 ios on my phone and that is ios 7, but thank you very much for sharing that. When I upgrade I will implement that.

Thanks!

7. ### PBG4 Dude macrumors 68030

Joined:
Jul 6, 2007
#7
Especially when they added stuff like the bomb that will disintegrate the dirt under your enemy and stuff.

I believe the game was originally called Tank Wars but I could be wrong.

8. ### mfram macrumors 65816

Joined:
Jan 23, 2010
Location:
San Diego, CA USA
9. ### DannyBres macrumors 65816

Joined:
Oct 30, 2007
Location:
UK
#9
As mentioned this was included in UIKit Dynamics in iOS7. Let s know how you get on with it.

10. ### IDMah macrumors 6502

Joined:
May 13, 2011
#10
'real' motion..

Helpful bit: Maybe Try Cocos2d.. should have a 6.1 version. it also does collisions I believe for ground bounces..

http://www.raywenderlich.com/4756/how-to-make-a-catapult-shooting-game-with-cocos2d-and-box2d-part-1

and this for some math: http://en.wikibooks.org/wiki/Game_Creation_with_XNA/Mathematics_Physics/Ballistics

now my 2 cents ..

In animation school we learned ..
Give people what they expect, not always what is real.

When Computer graphics were in their infancy everything was shiny and
mathematically perfect. Well reality 'Ain't' like that. It wasn't until
people started adding dirt and bubbles to CG that the object looked "Real"

Also we are use to seeing motion blur (calculate a few subframes before and after the actual frame and smush them together) which helps a lot in the realism department.

So to summarize.. Yes don't be afraid to cheat to make it look "right"

Good luck happy xcoding.

ps. Switch to xcode5 wasn't that painful. because I made a partition with mavericks or you can boot off
external.

11. ### larswik thread starter macrumors 68000

Joined:
Sep 8, 2006
#11
Thanks, I have switched to xcode5 already. But I really don't like the new look of Apples IOS7 which is the main reason I still run 6.1 on my iphone.

But I have already found the mathematical equation for a cannon shot and modified the gravity to make it a little more cartoon like. It is turning out quite nice and I am learning more and having fun with it.