Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-21-2010, 09:31 AM   #1 (permalink)
Ascending Mt. SDK
 
Join Date: Dec 2009
Posts: 64
Default ballgame question

Hi all,

I'm playing around with a ballgame. What I want for now is a ball that starts out bouncing around the screen, but gradually comes to a stop - as if experiencing friction. Here is my code so far:

Code:
#define kFriction 0.2

#define kBallSpeedX 12
#define kBallSpeedY 12

@implementation BallGameViewController

@synthesize ball;
@synthesize ballVelocity;
@synthesize friction;

...

- (void)viewDidLoad {
    [super viewDidLoad];
	ballVelocity = CGPointMake(kBallSpeedX,kBallSpeedY);
	friction = CGPointMake(kFriction, kFriction);
	
	[NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}

-(void)gameLoop  {
	
	ball.center = CGPointMake(ball.center.x +ballVelocity.x, ball.center.y +ballVelocity.y);
	
	if(ballVelocity.x > 0 ) {
		ballVelocity.x -= friction.x;
		
	}
	if (ballVelocity.y > 0) {
	
		ballVelocity.y -= friction.y;
		
	}
	
	if (ball.center.x >self.view.bounds.size.width  || ball.center.x < 0) {
		
		ballVelocity.x = -ballVelocity.x;	
	}
	if (ball.center.y >self.view.bounds.size.height || ball.center.y < 0 ) {
		
		ballVelocity.y = - ballVelocity.y;
	}
	
	

}
What I get, however, is a ball that bounces around, gradually slows to a near stop, and then drifts really slowly into the top left corner of the screen.

Can anyone see what I'm doing wrong?

Thanks!
gbaldwin9 is offline   Reply With Quote
Old 03-21-2010, 11:59 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Imagine that the velocity at the start of your game loop is (0.1, 0.1). What happens to the velocity during the game loop? Then imagine how your game loop behaves every subsequent time it's called.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 03-21-2010, 12:25 PM   #3 (permalink)
Ascending Mt. SDK
 
Join Date: Dec 2009
Posts: 64
Default

Quote:
Originally Posted by Kalimba View Post
Imagine that the velocity at the start of your game loop is (0.1, 0.1). What happens to the velocity during the game loop? Then imagine how your game loop behaves every subsequent time it's called.
OK, since I was decrementing (is that a word?) it by .2, when friction is applied to a speed of .1 it becomes negative on both the x and y coordinates and then, being below zero, no more friction is applied and it just drifts up and to the left.

Thanks.

Still, what I've written -while it now comes to a complete stop - isn't as realistic as I'd like, particularly near the end. What happens now is that as the ball slows it sort of flattens out on the x axis . That is, it seems that it seems that it reaches 0 y speed before it reaches 0 x speed and the result is that it always end traveling exactly horizontally right at the end rather than ending on a diagonal path.

Is there a better way to apply friction?

Last edited by gbaldwin9; 03-21-2010 at 12:28 PM.
gbaldwin9 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 244
22 members and 222 guests
@sandris, ADY, beleg_1998, Dani77, diyora, FAED, fredidf, F_Bryant, iDifferent, JamesCahall, JasonR, mer10, Oral B, prchn4christ, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:08 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0