My objective is to have secondBody 'orbit' firstBody with a constant velocity (500 in this case).
Based on Constant movement in SpriteKit I have implemented the following:
override func didMoveToView(view: SKView) {
physicsWorld.gravity = CGVector(dx: 0, dy: 0)
let firstNode = SKShapeNode(circleOfRadius: 10)
addChild(firstNode)
firstNode.position = CGPointMake(CGRectGetWidth(frame) / 2.0, CGRectGetHeight(frame) / 2.0)
let firstPhysicsBody = SKPhysicsBody(circleOfRadius: 10)
firstPhysicsBody.dynamic = false
firstNode.physicsBody = firstPhysicsBody
let secondNode = SKShapeNode(circleOfRadius: 10)
addChild(secondNode)
secondNode.position = CGPointMake(CGRectGetWidth(frame) / 2.0 + 40, CGRectGetHeight(frame) / 2.0 + 40)
let secondPhysicsBody = SKPhysicsBody(circleOfRadius: 10)
secondPhysicsBody.friction = 0
secondPhysicsBody.linearDamping = 0
secondPhysicsBody.angularDamping = 0
secondPhysicsBody.affectedByGravity = false
secondNode.physicsBody = secondPhysicsBody
let joint = SKPhysicsJointPin.jointWithBodyA(firstPhysicsBody, bodyB: secondPhysicsBody, anchor: firstNode.position)
joint.frictionTorque = 0
physicsWorld.addJoint(joint)
secondPhysicsBody.velocity = CGVector(dx: 0, dy: 500)
}
The problem that I am having is that secondNode is slowing down as time goes on. You will notice that I am setting all manner of things like gravity
on SKPhysicsWorld
, friction
linearDamping
& angularDamping
on SKPhysicsBody
and frictionTorque
on SKPhysicsJoint
.
So, what am I doing wrong? And how can I keep secondNode's speed constant without doing horrid calculations in -update
?
Also - I am aware that I can add an SKAction to follow a circular path but that isn't a reasonable solution in this case.
If there's something simple which I'm missing, could you also advice which of the '0's and 'false's that I'm setting can be removed.
Thanks