0

I'm trying to do a basic radar, where I have a sprite that is triangular shaped. I want it to rotate from the top point, so I set the anchor point to (0.5,1). Everything seems to work by now.

Now I need this to detect collisions, so I added a physicsBody to this sprite, and here comes my problem. PhysicsBody won't rotate around the same point as the sprite does due to the anchor point.

Here's a piece of code from my radar:

radar = [SKSpriteNode spriteNodeWithImageNamed:@"radar.png"];
/* Triangular path */
CGFloat offsetX = radar.frame.size.width * radar.anchorPoint.x;
CGFloat offsetY = radar.frame.size.height * radar.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 1 - offsetX, 1 - offsetY);
CGPathAddLineToPoint(path, NULL, 175 - offsetX, 1 - offsetY);
CGPathAddLineToPoint(path, NULL, 91 - offsetX, 239 - offsetY);
CGPathCloseSubpath(path);
/*end path*/
radar.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
radar.physicsBody.dynamic = NO;
radar.anchorPoint = CGPointMake(0.5, 1);

radar.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

And here's an image where you can see what happens. (PhysicsBody -> Green, RadarSprite->Blue, Point where I want it to rotate around->Red dot, Angle of rotation->Red arrows) enter image description here

So, the question is, what can I do to fix this? Thank you very much in advance

mursang
  • 586
  • 5
  • 24
  • 1
    Look at this previous question http://stackoverflow.com/questions/20045203/physicsbody-doesnt-adhere-to-nodes-anchor-point If that doesn't solve your issue, I suggest consider writing your own method for collision checking. – sangony Feb 17 '15 at 23:59
  • 1
    link the link above mentions, you have to use this initializer + (SKPhysicsBody *)bodyWithRectangleOfSize:(CGSize)s center:(CGPoint)center – hamobi Feb 18 '15 at 01:34
  • 1
    The position of each point in the physicsBody's path is relative to the sprite's position. so the path points should be: `(0,0),(-triangleWidth/2,-triangleHeight),(triangleWidth/2,-triangleHeight)` where triangleWidth&Height are the size of your sprite. –  Feb 18 '15 at 10:42
  • Thank you all!! Finally fixed it as Okapi said, adjusting the path points. – mursang Feb 18 '15 at 12:04

0 Answers0