Iphone Core Coordinate Tracking & Collision Animation

I am currently learning Iphone Programming and I am having some CAKeyframeAnimation related issues

I am using CAKeyframeAnimation to animate objects on the screen like this:

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

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

I am using this basic idea to move objects, but now I would like to be able to detect collisions between objects.

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

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

the problem is that there could be many objects (maybe up to 100 (estimate, but maybe more)) going down to a list of 10-20 objects to collide with. so I am concerned that there will be many tests per iteration.

what would you suggest me to do?

0


a source to share


4 answers


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 lot of objects, but it will work. Also you need to handle the detected collisions, otherwise you will find each collision twice.

+4


a source


I believe CoreAnimation (CA) is intended for use in animations that do not affect your "business logic" (or game logic for that matter), but simply please the user or provide visual feedback. If you need to do more than cancel the animation or be notified when it is done, this is a strong directive against using CA.



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

+3


a source


Effective collision detection can be a hairy problem. It is often a good idea to model it independently of the graphics. I suggest you check out some open source physics engine that has collision detection built in. Box2D is a popular and good choice and has a liberal license suitable for iPhone games.

Using Core Animation transforms for a game with many moving animations can be tricky. The actual position of the CALayer during animation is not reflected in its coordinates, only the end point or start point, depending on your choice. Thus, you need to create your own abstraction for this. edited: As Brad points out, you can get the position during animation from the presentation level.

+1


a source


what would you suggest me to do?

I don't think Core animation is the right framework for your application. 100 sprites will advance basic animation capabilities on iPhone, and there is no way to get the type of updates required.

+1


a source







All Articles