Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Physicsbody doesn't adhere to node's anchor point

My scene has a bunch of rectangles with physics bodies that are the same size as the rectangle. I like to anchor all of my objects to CGPointZero, however I've noticed when I do that the physicsbody remains anchored in the middle. In other words, the position of my physics body is like 100 pixels lower and to the left of the visual representation.

Here is a simple snippet of code:

SKSpriteNode* square = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(width, height)];
square.anchorPoint = CGPointZero; //position based on bottom-left corner
square.position = CGPointMake(x, y);

square.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(width, height)];

Any ideas or advice to solving this problem would be appreciated. For example, if I could visualize the physics bodies, that might help, but I'm not sure how to.

UPDATE: So I've solved the problem by simply not setting the anchor point and repositioning my rectangles. So the problem still exists, but I have a work around in place and the work around is working well.

like image 504
Sophie McCarrell Avatar asked Nov 18 '13 10:11

Sophie McCarrell


4 Answers

I wrote this to fix Apple's lack thereof:

use pathForRectangleOfSize:withAnchorPoint: to replace your call to bodyWithRectangleOfSize: whose brief documentation tells us the problem: "Creates a rectangular physics body centered on the owning node’s origin."

@implementation SKPhysicsBody (CWAdditions)

+ (CGPathRef)pathForRectangleOfSize:(CGSize)size withAnchorPoint:(CGPoint)anchor {
  CGPathRef path = CGPathCreateWithRect( CGRectMake(-size.width * anchor.x, -size.height * anchor.y,
                                                    size.width,   size.height), nil);
  return path;
}

+ (SKPhysicsBody *)bodyWithRectangleOfSize:(CGSize)size withAnchorPoint:(CGPoint)anchor {
  CGPathRef path = [self pathForRectangleOfSize:size withAnchorPoint:anchor];
  return [self bodyWithPolygonFromPath:path];
}

@end

Edit: There is a new API in 7.1 to provide for this oversight.

+ (SKPhysicsBody *)bodyWithRectangleOfSize:(CGSize)s center:(CGPoint)center
like image 99
bshirley Avatar answered Nov 15 '22 05:11

bshirley


You can use

      [SKPhysicsBody bodyWithRectangleOfSize:size center: center];


       CGPoint center = CGPointMake(size.width*(anchorPoint.x-0.5f), size.height*(0.5f-anchorPoint.y))

should transform the bounding box according to the anchorPoint of the parents node

like image 20
Dmitry Alexandrovsky Avatar answered Nov 15 '22 06:11

Dmitry Alexandrovsky


I am using Swift but the SKPhysicsBody was kind of half width and height wrong. I am using anchor point(0,0). Then I used the method with rectangleOfSize, center :

var cc = SKSpriteNode(color: UIColor.greenColor(), size: CGSizeMake(32, 64))
cc.physicsBody = SKPhysicsBody(rectangleOfSize: cc.size, center: CGPointMake(32/2, 64/2))

I hope it works for you too guys...thanks !

like image 6
maniacdeveloperbr Avatar answered Nov 15 '22 05:11

maniacdeveloperbr


You need the anchorPoint only when you set your spriteNode's position. I don't quite understand why would you need to move physicsBody (which is the same size of node, I presume) to a corner... But you might find useful this class method [SKPhysicsBody bodyWithPolygonFromPath:path].

Here is a nice generator for that: http://dazchong.com/spritekit/

like image 5
Maxiller Avatar answered Nov 15 '22 06:11

Maxiller