How to Make Physics Bodies Stick to Nodes Anchor Points

How to make physics bodies stick to nodes anchor points

You need to apply the anchor point to the physics body, it has no understanding of what sprite is, it is just another piece of information sprite uses, so use the following calculation to determine where the center of the sprite should be, and apply that to the physics body so that it may shift:

 let centerPoint = CGPointMake(sprite.size.width / 2 - (sprite.size.width * sprite.anchorPoint.x), sprite.size.height / 2 - (sprite.size.height * sprite.anchorPoint.y))
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size, center: centerPoint)

Swift 3+:

 let centerPoint = CGPoint(x:sprite.size.width / 2 - (sprite.size.width * sprite.anchorPoint.x), y:sprite.size.height / 2 - (sprite.size.height * sprite.anchorPoint.y))
sprite.physicsBody = SKPhysicsBody(rectangleOf: sprite.size, center: centerPoint)

Less text:

 let centerPoint = CGPoint(x:sprite.size.width * (0.5 - sprite.anchorPoint.x), y:sprite.size.height *(0.5 - sprite.anchorPoint.y))

physicsBody is misaligned with spriteNode

The anchorPoint determines where the node's texture is drawn relative to the node's position. It simply does not affect physics bodies because it's a purely visual property (a texture offset).

For physics-driven nodes it is actually counter-productive to change the anchorPoint from its default because that will change the point around which the texture will rotate. And the physics body will usually also change the node's rotation.

So even if you were to move the physics body shape's vertices to match the sprite with a modified anchorPoint, the physics shape will be misaligned with the image as soon as the body starts rotating. And it'll seem to behave weird.

Plus whatever you want to achieve using anchorPoint you can more flexibly achieve by using the node hierarchy to your advantage. Use a SKNode as the physics node, and add a non-physics sprite node as child to that node and offset it the way you wanted the image to be offset by changing the sprite's anchorPoint.

You end up having two nodes, one invisible representing the physics body and one (or more) sprite(s) representing the visuals for the body but not necessarily tied to the body's center position.

SKPhysicsJointPin pin position

Documentation is pretty lacking for these. To use the clock reference, and since you obviously know the code, I'll give you the English breakdown:

  1. add 2 clock hand sprites to the scene
  2. set both anchor points on the clock hand sprites to (.5,0)
  3. position both sprites at (100,100)
  4. create 2 physics bodies, add them to each clock hand
  5. create the SKPhysicsJointPin, use anchor point (100,100)
  6. add the joint to the scene

You should in theory, now have 2 clock hands, able to spin via their own anchor points around the point in the scene at (100,100).

Sticking Node to another Node when Colliding

You can connect the two nodes with a fixed physics joint. Physics joints, in general, connect two physics bodies that are in the scene. The fixed version, SKPhysicsJointFixed, joins two bodies such that they act as a single body.

Here's an example of how connects two physics bodies with a fixed joint:

First, add this to your SKScene subclass (e.g., GameScene.m)

func joinPhysicsBodies(bodyA:SKPhysicsBody, bodyB:SKPhysicsBody, point:CGPoint) {
let joint = SKPhysicsJointFixed.jointWithBodyA(bodyA, bodyB: bodyB, anchor: point)
self.physicsWorld.addJoint(joint)
}

and add this in the if statement in didBeginContact

self.joinPhysicsBodies(firstBody, bodyB:secondBody, point:contact.contactPoint)

where contact.contactPoint is the point of the contact.



Related Topics



Leave a reply



Submit