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 f
The slowing down behavior is often to be expected when dealing with physics engines. They are not perfect, especially in more complex scenarios like constraints. You should not rely on Sprite Kit to provide perfect continuos motion simulation because we have no clue what the physic's engine is doing under-the-hood; there are so many factors involved.
In cases where you need continuous motion, (especially something like constant centripetal motion) it is always best to calculate and preserve these values yourself. There is simply no escaping writing calculations in your update method for behaviors like this.
So I wrote a quick example project that calculates the necessary velocity needed to orbit a particular point. You simply specify a period, orbit position and orbit radius. Note that because I am calculating everything, there is no need for any SKJoints
, so that makes this implementation also more lightweight. I highly recommend you do your orbiting this way, as it gives you total control over how you want your nodes to orbit each other (i.e. you could have nodes orbit moving nodes, you could have oval orbiting paths, you could adjust the orbit during key moments in your game, etc.)
import SpriteKit
class GameScene: SKScene {
var node1: SKShapeNode!
var node2: SKShapeNode!
var node2AngularDistance: CGFloat = 0
override func didMoveToView(view: SKView) {
physicsWorld.gravity = CGVector(dx: 0, dy: 0)
node1 = SKShapeNode(circleOfRadius: 10)
node1.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
node1.physicsBody = SKPhysicsBody(circleOfRadius: 10)
node2 = SKShapeNode(circleOfRadius: 10)
node2.position = CGPoint(x: self.size.width/2.0+50, y: self.size.height/2.0)
node2.physicsBody = SKPhysicsBody(circleOfRadius: 10)
self.addChild(node1)
self.addChild(node2)
}
override func update(currentTime: NSTimeInterval) {
let dt: CGFloat = 1.0/60.0 //Delta Time
let period: CGFloat = 3 //Number of seconds it takes to complete 1 orbit.
let orbitPosition = node1.position //Point to orbit.
let orbitRadius = CGPoint(x: 50, y: 50) //Radius of orbit.
let normal = CGVector(dx:orbitPosition.x + CGFloat(cos(self.node2AngularDistance))*orbitRadius.x ,dy:orbitPosition.y + CGFloat(sin(self.node2AngularDistance))*orbitRadius.y);
self.node2AngularDistance += (CGFloat(M_PI)*2.0)/period*dt;
if (fabs(self.node2AngularDistance)>CGFloat(M_PI)*2)
{
self.node2AngularDistance = 0
}
node2.physicsBody!.velocity = CGVector(dx:(normal.dx-node2.position.x)/dt ,dy:(normal.dy-node2.position.y)/dt);
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
node1.position = (touches.first! as! UITouch).locationInNode(self)
}
}
Below is a gif showing the centripetal motion. Notice that because it is completely dynamic we can actually move the centripetal point as it is orbiting.
If however you really want to use your current SKJoints
implementation for some reason then I have another solution for you. Simply keep updating your node's linear velocity so that it never slows down.
override func update(currentTime: NSTimeInterval) {
let magnitude = sqrt(secondNode.physicsBody!.velocity.dx*secondNode.physicsBody!.velocity.dx+secondNode.physicsBody!.velocity.dy*secondNode.physicsBody!.velocity.dy)
let angle = Float(atan2(secondNode.physicsBody!.velocity.dy, secondNode.physicsBody!.velocity.dx))
if magnitude < 500 {
secondNode.physicsBody!.velocity = CGVector(dx: CGFloat(cosf(angle)*500), dy: CGFloat(sinf(angle)*500))
}
}
Regardless of the solution you pick (and once again I highly recommend the first solution!), you can choose to apply the velocity over time rather than instantaneously for a more realistic effect. I talk more about this in my answer here
Hopefully I have provided you with enough information to resolve your issue. Let me know if you have any questions. And best of luck with your game!