Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocos2d: Detect touch on rotated sprite?

How would one detect a touch on a rotated CCSprite?

I am familiar with the general technique of using ccTouchesBegan and contentSize, anchorPoint etc. to have a sprite detect if a touch was within its bounds ... but I am not sure how to proceed once the sprite has been rotated by some angle.

I want the sprite itself to detect the touch (encapsulation) and report the event via a delegate to another object.

If anyone has some code to share ... would be great.

like image 991
poundev23 Avatar asked Dec 12 '22 20:12

poundev23


1 Answers

Try using the CCNode convertTouchToNodeSpaceAR: method to convert the point to the rotated coordinates and then you can do the compare of the sprite bounds.

I made this a category on CCNode so it's available to any CCNode or subclass.

@interface CCNode (gndUtils)

// Lets a node test to see if a touch is in it.
// Takes into account the scaling/rotation/transforms of all 
// the parents in the parent chain.
// Note that rotation of a rectangle doesn't produce a rectangle 
// (and we are using a simple rectangle test)
//   so this is testing the smallest rectangle that encloses the rotated node.
// This does the converstion to view and then world coordinates
// so if you are testing lots of nodes, do that converstion manually
//
//  CGPoint touchLoc = [touch locationInView: [touch view]];  // convert to "View"
//  touchLoc = [[CCDirector sharedDirector] convertToGL: touchLoc]; // move to "World"
// and then use worldPointInNode: method instead for efficiency.

- (BOOL) touchInNode: (UITouch *) touch;

// allows a node to test if a world point is in it.
- (BOOL) worldPointInNode: (CGPoint) worldPoint;

@end

and the implementation:

@implementation CCNode (gndUtils)

- (BOOL) touchInNode: (UITouch *) touch
{
    CGPoint touchLoc = [touch locationInView: [touch view]];            // convert to "View coordinates" from "window" presumably
    touchLoc = [[CCDirector sharedDirector] convertToGL: touchLoc];     // move to "cocos2d World coordinates"

    return [self worldPointInNode: touchLoc];
}

- (BOOL) worldPointInNode: (CGPoint) worldPoint
{
    // scale the bounding rect of the node to world coordinates so we can see if the worldPoint is in the node.
    CGRect bbox = CGRectMake( 0.0f, 0.0f, self.contentSize.width, self.contentSize.height );    // get bounding box in local 
    bbox = CGRectApplyAffineTransform(bbox, [self nodeToWorldTransform] );      // convert box to world coordinates, scaling etc.
    return CGRectContainsPoint( bbox, worldPoint );
}
@end
like image 117
Dad Avatar answered Jan 29 '23 04:01

Dad