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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-08-2010, 10:16 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Vienna, Austria
Posts: 4
Default make movements smoother

With the help of the thread RandomRocks I was able to create an application which creates multiple instances of snowflakes and animates them on the screen.

Unfortunately, the movements of the snowflakes seem a bit mechanic. Here is the code I use so far

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSMutableArray *array = [[NSMutableArray alloc] init];
	self.snowArray = array;
	[array release];
	
	[self createSnowFlakes];
	
	[NSTimer scheduledTimerWithTimeInterval: 0.06 target: self selector: @selector(moveSnowFlakes) userInfo: nil repeats: YES];

}

-(void)createSnowFlakes{
	
    UIImage *snowImage = [UIImage imageNamed:@"snow25.png"];
    UIImageView *newView;
	
    for (int i = 0; i< 10; i++){
        newView= [[UIImageView alloc] initWithImage: snowImage];
		
        int x = arc4random()%320; 
        int y = -arc4random()%480;
        newView.center = CGPointMake (x,y);
		
        [self.view addSubview: newView];
        [snowArray addObject: newView];
        [newView release];
    }
	
}

-(void)moveSnowFlakes{
	
	for (UIImageView *snow in snowArray) {
		 
		CGPoint newCenter = snow.center;
		newCenter.y = newCenter.y +2;
		
		int leftOrRight = arc4random() % 4;
		if (leftOrRight == 0) {
			// left
			newCenter.x = newCenter.x - 1;
		} else {
			newCenter.x = newCenter.x + 1;
		}

		
		if (newCenter.y > 480){ 
			newCenter.y=0; 
			newCenter.x = arc4random()%320;
		}
		if (newCenter.x > 320){
			newCenter.x =0;
		} else if (newCenter.x < 0) {
			newCenter.x = 320;
		}
		
		snow.center = newCenter;
	}
}
When I decrease the timer value, the snowflakes move smoother but also faster. How would I make them move slowly but smooth?
black666 is offline   Reply With Quote
Old 02-08-2010, 12:07 PM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by black666 View Post
With the help of the thread RandomRocks I was able to create an application which creates multiple instances of snowflakes and animates them on the screen.

Unfortunately, the movements of the snowflakes seem a bit mechanic. Here is the code I use so far

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSMutableArray *array = [[NSMutableArray alloc] init];
	self.snowArray = array;
	[array release];
	
	[self createSnowFlakes];
	
	[NSTimer scheduledTimerWithTimeInterval: 0.06 target: self selector: @selector(moveSnowFlakes) userInfo: nil repeats: YES];

}

-(void)createSnowFlakes{
	
    UIImage *snowImage = [UIImage imageNamed:@"snow25.png"];
    UIImageView *newView;
	
    for (int i = 0; i< 10; i++){
        newView= [[UIImageView alloc] initWithImage: snowImage];
		
        int x = arc4random()%320; 
        int y = -arc4random()%480;
        newView.center = CGPointMake (x,y);
		
        [self.view addSubview: newView];
        [snowArray addObject: newView];
        [newView release];
    }
	
}

-(void)moveSnowFlakes{
	
	for (UIImageView *snow in snowArray) {
		 
		CGPoint newCenter = snow.center;
		newCenter.y = newCenter.y +2;
		
		int leftOrRight = arc4random() % 4;
		if (leftOrRight == 0) {
			// left
			newCenter.x = newCenter.x - 1;
		} else {
			newCenter.x = newCenter.x + 1;
		}

		
		if (newCenter.y > 480){ 
			newCenter.y=0; 
			newCenter.x = arc4random()%320;
		}
		if (newCenter.x > 320){
			newCenter.x =0;
		} else if (newCenter.x < 0) {
			newCenter.x = 320;
		}
		
		snow.center = newCenter;
	}
}
When I decrease the timer value, the snowflakes move smoother but also faster. How would I make them move slowly but smooth?
Find the line in your code that alters the position of the flakes (to achieve the "falling" animation effect), then tweak that line and observe how changing the value one way or the other affects the visual appearance of the falling speed.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 02-09-2010, 09:15 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Chicago
Posts: 29
Default

Quote:
Originally Posted by black666 View Post

Code:
-(void)moveSnowFlakes{
	
	for (UIImageView *snow in snowArray) {
		 
		CGPoint newCenter = snow.center;
		newCenter.y = newCenter.y +2;
		
		int leftOrRight = arc4random() % 4;
		if (leftOrRight == 0) {
			// left
			newCenter.x = newCenter.x - 1;
		} else {
			newCenter.x = newCenter.x + 1;
		}
}
So every frame your flakes can reverse horizontal direction? That could make the movement look really jerky. Try assigning an x-velocity to each flake at creation and keeping it constant, or changing it less frequently.
DaveM is offline   Reply With Quote
Old 02-09-2010, 09:20 AM   #4 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Vienna, Austria
Posts: 4
Default

I changed the line that moved the flakes down 2 pixels to moving them down only 1 pixel. It was a bit smoother but still didn't feel natural.

Then I figured out, that snowflakes actually don't change their direction every 2nd or 3rd pixel which was the reason why it all seemed a bit off.
I now simply store the direction each flake is going (left or right) .. I then configured the chance of a flake changing it's direction to about 5-10% and now it all seems exactly the way it should be.

Funny..first I thought I'm missing some magical paramter on how to smoothen image movements programmatically, but I actually didn't really think about how snow flakes move in real life.
black666 is offline   Reply With Quote
Old 02-09-2010, 09:22 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Vienna, Austria
Posts: 4
Default

Quote:
Originally Posted by DaveM View Post
So every frame your flakes can reverse horizontal direction? That could make the movement look really jerky. Try assigning an x-velocity to each flake at creation and keeping it constant, or changing it less frequently.
Yep, that's exactly what happened / made it look unnatural.
black666 is offline   Reply With Quote
Old 02-09-2010, 02:04 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 223
Default

the most efficient way to handle this is inside a vertex shader.

but if you're on gles1, then the cpu must do it. The trick is to alter acceleration randomly at random intervals with seemingly no pattern.

define a struct or class for snowflake, containing x,y (and z if 3d) position. Initialize them within a random floating point range to fill your screen, random velocity (dx, dy dz), and random acceleration (ddx, ddy, ddz), the acceleration range should be small as it will quickly move your flakes. Lastly define a random time within say 0.1 to 0.5 seconds for the "shift"

Now during processing/update method, loop through all the flakes doing an x+=dx*delta and dx+=ddx*delta for each x,y,z of the flake, and a time -= delta where delta is the time between animation frame updates in seconds. If time goes negative, reinitialize the flake's acceleration like above with random values only and set a new timeout. Lastly if a flake falls down off the screen, respawn it at the top

that'll do it
mlfarrell is offline   Reply With Quote
Old 02-09-2010, 02:15 PM   #7 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

IMO, at this introductory level, dealing with an acceleration vector as part of the equation is beyond the scope of what needs to be learned. Besides, aren't snowflakes and raindrops already at terminal velocity by the time they reach the surface of the Earth?
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 02-09-2010, 03:54 PM   #8 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 223
Default

Quote:
Originally Posted by Kalimba View Post
IMO, at this introductory level, dealing with an acceleration vector as part of the equation is beyond the scope of what needs to be learned. Besides, aren't snowflakes and raindrops already at terminal velocity by the time they reach the surface of the Earth?
its not about realism, its about gameism :-). If it looks cool, then do it
mlfarrell is offline   Reply With Quote
Old 02-09-2010, 04:21 PM   #9 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Quote:
Originally Posted by mlfarrell View Post
its not about realism, its about gameism :-). If it looks cool, then do it
Also, if there is a lot of wind, it can give the effect that snow is falling faster.
ZunePod is offline   Reply With Quote
Old 02-09-2010, 04:37 PM   #10 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by mlfarrell View Post
its not about realism, its about gameism :-). If it looks cool, then do it
Quote:
Originally Posted by ZunePod View Post
Also, if there is a lot of wind, it can give the effect that snow is falling faster.
As a game developer, I totally get it. My point is that for the newb who hasn't quite got hold of the basics to get things moving in the first place, the introduction of an acceleration vector will only complicate things. Let's help the OP get walking before asking them to run the 100m hurdles...
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 02-09-2010, 04:40 PM   #11 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Quote:
Originally Posted by Kalimba View Post
As a game developer, I totally get it. My point is that for the newb who hasn't quite got hold of the basics to get things moving in the first place, the introduction of an acceleration vector will only complicate things. Let's help the OP get walking before asking them to run the 100m hurdles...
Gotcha.

110m Hurdles actually.

Sorry I have a habit of doing that.
ZunePod 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: 318
19 members and 299 guests
@sandris, ADY, dacapo, Dani77, djohnson, dre, HDshot, HemiMG, JasonR, MarkC, mer10, nibeck, prchn4christ, ryandb2, spiderguy84, timle8n1, tomtom100, vogueestylee
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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