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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 08-16-2011, 12:48 PM   #1 (permalink)
New User
 
Join Date: Aug 2011
Posts: 8
.Snipe is on a distinguished road
Default Curves / Bezier Curves and Moving object along path

Hello,
I have ventured in the world of iOS Dev a few weeks ago... and this forum was the biggest help I could get. Every time I would have a problem, I would find an answer here. Well, this time, no luck.

Did you ever reach a state where you would like to through your Mac off a bridge ? ... been there 3 time today.

The task :
Create a curve/spline (initially a straight line) that goes from point A to B.
Between A and B, there are 3 dots (lets say Z,X,C).

Now, what I would like to do is move the Z,X,C points (one at a time) to form a spline.I experimented with Bezier but my problem is that the dots form a corner when moved and the spline is not smooth.

Second, I would like to have a sprite that moves along this Bezier Path, changing rotation according to spline.

Thank you so very much in advance.
.Snipe is offline   Reply With Quote
Old 08-16-2011, 01:27 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: New York
Posts: 18
TheStuFactor is on a distinguished road
Default

the below code animates an image, 'badge@2x.png', along a bezier path to the viewController's tabBar button. Basically it's an animation of a badge being 'dropped' into the tabBar (check out the code under 'setup the path of the animation').

Code:
- (void) showButtonAnimation{

 // show the button animation
    UIImageView *btnImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"badge@2x.png"]];
    [self.view addSubview:btnImg];
    btnImg.center = CGPointMake(centerPoint.x + (btnImg.frame.size.width / 2), centerPoint.y + (btnImg.frame.size.height / 2));
    
    // setup the path of the animation
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    
    CGPoint endPoint = CGPointMake(178.0f, 440.0f);
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, btnImg.frame.origin.x, btnImg.frame.origin.y);
    CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, btnImg.frame.origin.y, endPoint.x, btnImg.frame.origin.y, endPoint.x, endPoint.y);
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);
    
    // setup scaling
    CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
    [resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(16.0f, 16.0f)]];
    resizeAnimation.fillMode = kCAFillModeForwards;
    resizeAnimation.removedOnCompletion = NO;
    
    // setup fade
    CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.1]];
    fadeOutAnimation.fillMode = kCAFillModeForwards;
    fadeOutAnimation.removedOnCompletion = NO;
    
    CAAnimationGroup *group = [CAAnimationGroup animation]; 
    group.fillMode = kCAFillModeForwards;
    group.removedOnCompletion = NO;
    [group setAnimations:[NSArray arrayWithObjects: pathAnimation, resizeAnimation, fadeOutAnimation,nil]];
    group.duration = 0.5f;
    group.delegate = self;
    [group setValue:btnImg forKey:@"imageViewBeingAnimated"];
    
    [btnImg.layer addAnimation:group forKey:@"curveAnimation"];
    [btnImg release];
}
TheStuFactor is offline   Reply With Quote
Old 08-18-2011, 03:43 AM   #3 (permalink)
New User
 
Join Date: Aug 2011
Posts: 8
.Snipe is on a distinguished road
Default

Will take a look at it in a bit.
Experimenting with setting up the spline at the moment. Will come back with result.
.Snipe is offline   Reply With Quote
Old 08-23-2011, 05:09 AM   #4 (permalink)
New User
 
Join Date: Aug 2011
Posts: 8
.Snipe is on a distinguished road
Default

Hello again...
thank you for the help. Everything is moving along rather well. Will post back when I have the code done as to give back at least something to all that will eventually have problems with Bezier as I did.

But for now, I am looking for the following :

I have a bezier curve, but I want the object to move above it, something like the image here : http://www.esentiali.com/iphonesdk/001.png. Let me explain.

I have the curve working (almost), but the issue is as following. In order to get the bike from the start of the curve to move above the curve (like A,B,C) I enlarged the frame of the Image and left some transparent space below the bike (see D) so the center of the image is on the curve, but visually the bike is moving above the curve. If I don't create the extra transparent space, I get the unwanted result (see E).

The A,B,C,D method is fine, until I need to detect collision, obviously since the area of the image is way too big. I though of 2 workarounds :

1. Clone the path (which will be invisible) and let the small (normal) image move on it, but I don't know how to clone the path so that the bike moves corectly, since the invisible curve has to be different than the original.

2. Create a collision Rect in the Bike Rect and detect collision of that, but can't get this to work either.

Now the structure is as follows. In my ViewController I create a UIView which contains the following :

Code:
bike = [CALayer layer];
	bike.bounds = CGRectMake(0, 0, 40.0, 40.0);
	bike.position = CGPointMake(p01_1.x,p01_1.y);
	bike.contents = (id)([UIImage imageNamed:@"bike.png"].CGImage);
	[self.layer addSublayer:bike];
	
	bikeCollision = [CALayer layer];
	bikeCollision.bounds = CGRectMake(0, 0, 15.0, 9.0);
	bikeCollision.position = CGPointMake(p01_1.x-7,p01_1.y-20);
	bikeCollision.contents = (id)([UIImage imageNamed:@"bike_collision.png"].CGImage);
	[self.layer addSublayer:bikeCollision];

	//I animate both layer along the path and I get the Rect like this : 

	CGRect bikeCollisionFrame = [(CALayer*)[bikeCollision presentationLayer] frame];
It works like this, but the bikeCollisionFrame is on the path, now if I want it above it I tried adding the bikeCollision not to the view but as a SubLayer of bike, so that it moves together with the bike when it is animated , but I can't get it's Rect (more like I can get the Rect , but it is not moving).

Code:
bike = [CALayer layer];
	bike.bounds = CGRectMake(0, 0, 40.0, 40.0);
	bike.position = CGPointMake(p01_1.x,p01_1.y);
	bike.contents = (id)([UIImage imageNamed:@"bike.png"].CGImage);
	[self.layer addSublayer:bike];
	
	bikeCollision = [CALayer layer];
	bikeCollision.bounds = CGRectMake(0, 0, 15.0, 9.0);
	bikeCollision.position = CGPointMake(20,10);
	bikeCollision.contents = (id)([UIImage imageNamed:@"bike_collision.png"].CGImage);
	[bike addSublayer:bikeCollision];

	//I animate ONLY the bike layer along the path and I get the Rect like this : 
	
	CGRect p = [bikeCollision bounds];
	CGRect bikeFrameRect = [[self.layer superlayer] convertRect:p fromLayer:bike];
	NSLog(@"bikeFrameRect pos.x : %.1f, pos.y : %.1f",bikeFrameRect.origin.x,bikeFrameRect.origin.y);
But here the Log shows no position change, probably due to the fact that I animate the bike layer, which is the container layer for the bikeCollision layer, and for some reason the parent is animated and the child is not.

PS, my animation script :

Code:
anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
		anim.path = bezierPath_1.CGPath;
		anim.rotationMode = kCAAnimationRotateAuto;
		anim.repeatCount = 1; //HUGE_VALF;
		anim.duration = animDuration;
		
		[bike removeAllAnimations];
		//[bikeCollision removeAllAnimations];
		
		[bike addAnimation:anim forKey:@"race"];
		//[bikeCollision addAnimation:anim forKey:@"race"];
I might have gone the wrong way. Could someone help or point me in the right direction ?
.Snipe is offline   Reply With Quote
Old 08-23-2011, 07:11 AM   #5 (permalink)
New User
 
Join Date: Aug 2011
Posts: 8
.Snipe is on a distinguished road
Default

Must be a good day. i could not quite understand it, but I could not get it right and access the SubLayer in the following structure : UIViewController > UIView > CALayer > CALayer - sublayer (bikeCollision).

I got it to work like this :

Code:
CALayer* tmpBikeCollision = (CALayer*)[BikeCollision presentationLayer];
	CGRect bikeCollisionFrame = [self.layer convertRect:CGRectMake(0, 0, 15, 9) fromLayer:tmpBikeCollision];
and with bikeCollisionFrame I can detect collision positioning inside the bike CALayer as per workaround #2 mentioned above.

Last edited by .Snipe; 08-23-2011 at 09:17 AM.
.Snipe 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: 410
11 members and 399 guests
AppleDev, chemistry, Emy, Gi-lo, ipodphone, mistergreen2011, pipposanta, Retouchable, skrew88, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,923
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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