views:

208

answers:

2

Just wondering if there was another way to check a touched area using a polygon of a particular shape rather than just using CGRectMake.

Here's the same code for this example:

CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView: [touch view]]];
CGRect mySurface = CGRectMake(x, y, temp.contentSize.width, temp.contentSize.height);

if(CGRectContainsPoint(mySurface, location)) {
    // do something
    return kEventHandled;
}

Was thinking of having several CGRect object in the if statement, but was wondering if there was a better way to do this like something like image map in html for anchors.

+2  A: 

Consider CGPath:

Create a path, there are many GPath functions that are available. Then use:

Checks whether a point is contained in a graphics path.

bool CGPathContainsPoint (
   CGPathRef path,
   const CGAffineTransform *m,
   CGPoint point,
   bool eoFill
);
zaph
I am curious on how to generate the path of a certain curve image. The Curve has a thickness, but how do we generate that Path for CGPathRef if it were that case? From what i understand, it's just an array of all the points within the array? But how do we detect it if the points are not static.
Frank
A: 

alternatively you can use a physics engine like Box2D or Chipmunk to handle collisions. Both engines are integrated in cocos2D. This way you can define circles, squares, polygons... and collisions are handled for you. It is precise and probably also quite fast although i haven't measured this. On the downside you have to keep this physics world in sync with your objects and you have to setup everything which takes a bit of time.

So the question is always: do i need it to be that precise or are simple bounding boxes good enough?!

Christian
Could you put some sample codes here of chipmunk or box2d collision detection?
Ali Bozorgkhan