views:

1075

answers:

4

Dear All, I'm currently learning Iphone programing and I'm having some trouble wrapping my mind around CAKeyframeAnimation

I'm using CAKeyframeAnimation to animate objects on the screen like so:

CGPathMoveToPoint(thePath, NULL, start.x, start.y); CGPathAddLineToPoint(thePath, NULL, finish.x, finish.y); animation.path = thePath; animation.duration = animationDuration;

// Add the animation group to the layer [Layer addAnimation:animation forKey:@"animate"]

I use this basic idea to move my objects about but now I would like to be able to detect collision between objects.

I was thinking of adding to each animated objects on the screen an observer [animated addObserver:...]

and let when the position changes call up a method that checks the current position against the posiiton of all the other animated objects.

problem is there could be a lot of objects (maybe up to 100 (estimation but maybe more)), moving about to check against a list of 10 - 20 objects for collisions. therefore I'm worried that its going to be a lot of test at each iteration.

what would you suggest me to do?

+1  A: 

Doing collision detection well can be a hairy problem. Often it's good idea to model it independently of graphics. I suggest that you familiarize yourself with some open source physics engine that has collision detection built in. Box2D is a popular and good choice and it has a liberal licencse suitable for iPhone games.

Using Core Animation transformations for a game with a lot of moving animations can be tricky. Actual position of a CALayer during an animation is not reflected in it's coordinates, only end-point or starting point depending on your choice. Thus you need to built your own abstraction for this. edited: As Brad points out, you can get a position during animation from a presentation layer.

tequilatango
Actually, you can get the current position of a layer mid-animation by grabbing its presentationLayer and querying that.
Brad Larson
Brad, thanks, I hadn't realized this, good info!
tequilatango
+1  A: 

what would you suggest me to do?

I don't think Core animation is the correct framework for your application. 100 sprites is going to push core animation's capabilities on the iPhone and, there is no way to get the type of updates you require.

Roger Nolan
I believe you're right Roger. I'm quite sure CA won't do it with this kind of frame to frame based action. This flirts with OpenGL ES.
Kriem
A: 

for(UIView *aView in arrayOfMovingObjects){

    CGRect aFrame = [(CALayer*)[aView.layer presentationLayer] frame];

    for(UIView *anotherView in arrayOfMovingObjects){

        if(aView!=anotherView){

             CGRect anotherFrame = [(CALayer*)[anotherView.layer presentationLayer] frame];

             if(CGRectIntersectsRect(aFrame, anotherFrame){

             // handle collision

             }
        }
   }
}

I'm not too sure about performance with a large number of objects, but this will work. Also you need to handle store detected collisions, otherwise you will detect every collision twice.

Corey Floyd
+3  A: 

I believe that CoreAnimation (CA) is intended to be used for animations that do not affect your "business logic" (or game logic in that case) but instead just please the user or give him visual feedback. If you need to do more than canceling an animation or be notified when it's done, it is a strong indication against using CA.

As tequilatango pointed out, it is usually a good idea to separate collision and graphics. Or even more general: Graphics are usually a one-way road to the user. Very rarely should the results of your rendering affect other parts of your program.

racha