Hi there - I've searched the forums, and found the a few posts talking about how to test if a rotated sprite collides with a cgrect, but have tried the solutions posted and got no results ;-(
e.g.
http://www.cocos2d-iphone.org/forum/topic/1693#post-10649
What I want to do is test if a ROTATED sprite collides with a touched location, but it doesn't seem to work - it still sees the sprite as not rotated when I do my collision test. I've added the following to my sprite class as suggested by most of the approaches i saw:
-(CGRect) realContentRect
{
CGRect rect = CGRectMake(0, 0, contentSize_.width, contentSize_.height);
return CGRectApplyAffineTransform(rect, [self nodeToParentTransform]);
}
-(BOOL) intersectsRect:(CGRect) targetRect
{
return CGRectIntersectsRect([self realContentRect], targetRect);
}
-(BOOL) intersectsPt:(CGPoint) targetPt
{
CGRect targetRect = CGRectMake(targetPt.x, targetPt.y, 1, 1);
return [self intersectsRect:targetRect];
}
and my touchesEnded event tests for collision with my rotated player like so:
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if ([player intersectsPoint:location])
{
NSLog(@"clicked on rotated player");
}
}
any ideas why this doesnt work? it works as if the player sprite isn't rotated. thanks so much for any pointers!