views:

519

answers:

5

I'm working on an iPhone app with some simple animation.

I have a view I want to translate, but not along a line. I want to translate it parabolically. Imagine that I am animating a car moving along a curved road.

I know I can set the transform properly to an instance of CGAffineTransform

Problem is, I have no idea how to create the transform. I know how to scale, translate, etc. but how do I translate parabolically? Is it even possible?

A: 

It sounds like you need to periodically set the transform property to a CGAffineTransformTranslate created with x,y parameters chosen along the parabola (do what Russell suggested to get x from y or vice versa).

If you also want your car to rotate as it curves along the parabola (will look more realistic) you will need CGAffineTransormRotate. Determine the angle based on the slope of the parabola at the point you're currently drawing at. You'll need an atan() function to compute the angle from the slope. Sounds like a pain, hopefully there's an easier way.

I really don't know what I'm talking about, I just read this blog entry: Demystifying CGAffineTransform.

Dan
OK this sounds like a massive ass pain. I think what I'm going to try and do instead is combine a rotation and a translate, inspired by your answer here. Damn, I wish I just paid a little bit more attention 10 years ago in that Linear Algebra class...
bpapa
+2  A: 

An aesthetically pleasing way to animate along curves is to use splines. These are mathematically simple and computationally efficient. The least order spline that would solve a parabolic arch is a quadratic spline.

TokenMacGuy
A: 

I think TokenMacGuy's suggestion to use splines is a good one. Again, I don't know what I'm talking about but maybe this will give you some ideas.

Does it have to be exactly a parabola or will an approximation work? What you want to do sounds very much like rendering text along a curve (see screenshot too). That series of posts seems like a pretty good how-to -- explains angle calculations and stuff. Each "character" corresponds to a position of your car. Parameterizing on arc length ensures your velocity is constant.

As far as how to do that on an iPhone... no idea.

Dan
+5  A: 

To animate along a smooth curve, you'll want to use a CAKeyframeAnimation. In this answer I provide code for a combined animation that moves an image view along a curve, while resizing it and fading it out. The relevant part of that code is as follows:

// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;

CGPoint endPoint = CGPointMake(480.0f - 30.0f, 40.0f);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

This creates a curve with two control points, one at the origin of the view and the other at the upper-right corner of the the display (in landscape mode). For more on constructing paths, see the Quartz 2D Programming Guide or the Animation Types and Timing Programming Guide.

To use this animation, you'll need to add it to your view's layer using something like the following:

[imageViewForAnimation.layer addAnimation:pathAnimation forKey:@"curveAnimation"];
Brad Larson
I wound up just translating diagonally, and it looked good enough, so the question kinda became obsolete. I don't really care enough to try out Brad's method, but sure, it seems right. :P Marking as answered.
bpapa
I just tried it and it works well. ;)
Euan
A: 

i am getting an error that viewOrigin undeclared....should i have to include any classes or framework for this

iphone66