简体   繁体   中英

CGRect changes with CCSprite Rotation?

i am trying to scale and rotate a CCSprite using the following methods

[rectangleSprite setRotation:touch1.y];

rectangleSprite.scaleX=(touch1.x-touch2.x)/100;
rectangleSprite.scaleY=(touch1.y-touch2.y)/100;

But when i scale or rotate the sprite its CGRect expands to cover the whole rotated area and collision detection through CGRectIntersectsRect doesn't work. after a lot of searching i came across following solutions like this but they doesn't seem to work

how to check for collision with rotated CGRect?

after rotating how can i apply the CGRectApplyAffineTransform to get the new rect according to rotation or is there any other possible way with it? i am applying the transform in the following way but the rectangle i am getting has negative origin values.. what am i possibly doing wrong?

CGAffineTransform affine;

//  add a rotate
affine = CGAffineTransformMakeRotation(-CC_DEGREES_TO_RADIANS(angle));


CGRect start =[rectangleSprite boundingBox];
NSLog(@"cg rect %f %f",start.size.height ,start.size.width);

CGRect  newRect = CGRectApplyAffineTransform(start, affine);

NSLog(@"cg rect %f %f",rct.origin.x,rct.origin.y);

You could always leverage one of the physics libraries packaged with cocos2d to do the collision detection for you instead of rolling your own collision detection. You just have the physics bodies follow your sprite location and rotation in the update function instead of the other way around so the physics engine doesn't move your sprites for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM