Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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

- (void)viewDidLoad
{
    [super viewDidLoad];
    speed = 1.0;
    ballMovment = 0.0;
    switchDirection = NO;
    downDirection = YES;
    displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveRock)];
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // 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

Thanks much for any advise.
 

Attachments

  • Ball bounce.zip
    126.3 KB · Views: 149
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
 
Last edited:
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.

:eek: That game is awesome :D
 
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?
 
How about UIDynamicAnimator?

You could achieve what you want with a few lines of code
 
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!
 
As mentioned this was included in UIKit Dynamics in iOS7. Let s know how you get on with it.
 
'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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.