Swift: set speed for distance

I am trying to make a moving platform on Swift and in order to make the player move with the platform, I needed to change self.moveByX() , which was much easier on physicsBody?.velocity (and add some friction for the platform and the player).

So, I made a function like this:

 self.runAction( SKAction.repeatActionForever( SKAction.sequence([ SKAction.runBlock({ print(self.position.x) self.physicsBody?.velocity = CGVectorMake(20, 0.0) }), SKAction.waitForDuration(3), SKAction.runBlock({ print(self.position.x) self.physicsBody?.velocity = CGVectorMake(-20, 0.0) }), SKAction.waitForDuration(3) ]) ) ) 

The problem is that I expected the difference between the positions to be exactly 60 (20 * 3 s), but I get platform x logs as follows:

 596.042907714844 544.228393554688 596.048950195312 544.234008789062 596.054565429688 544.116333007812 595.936584472656 544.121887207031 595.942199707031 544.127685546875 595.824584960938 544.009704589844 

So, the difference between the positions is something between 51 ~ 52

How to make the platform move at a certain and exact distance, changing the speed?

Edit: I don't know how relevant this is, but I installed the physical body of the platform as follows:

 self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size) self.physicsBody?.categoryBitMask = PhysicsCategory.Ground self.physicsBody?.contactTestBitMask = PhysicsCategory.Player self.physicsBody?.collisionBitMask = PhysicsCategory.Player self.physicsBody?.allowsRotation = false self.physicsBody?.affectedByGravity = false self.physicsBody?.dynamic = true self.physicsBody?.friction = 1.0 self.physicsBody?.restitution = 0.0 self.physicsBody?.mass = 99999999999 
+7
ios swift game-physics skphysicsbody
source share
1 answer

I have not worked with the Sprite Kit before, but I think your problem is that the linearDamping property for SKPhysicsBody is set to 0.1 by default, with fast calculation (20 * 3-20 * 3 * 0.1 = 54). sounds right if the speed fades out every frame.

Here is the documentation for the property:

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/linearDamping

Edit: I thought I should at least check my answer, so I wrote a short (rough) program:

 var dampingOccurancesPerSecond = 60.0 //Corresponds to frame rate var numberOfSeconds = 3.0 var time = 0.0 @IBAction func test(sender: UIButton) { let result = checkDistanceForVelocity(20, damping: 0.1, distanceTraveled: 0.0) print(result) } func checkDistanceForVelocity(velocity: Double, damping: Double, distanceTraveled: Double) -> Double { if time < numberOfSeconds { time += 1/dampingOccurancesPerSecond let newVelocity = velocity - (velocity * damping/dampingOccurancesPerSecond) let newDistance = (distanceTraveled + velocity/dampingOccurancesPerSecond) return checkDistanceForVelocity(newVelocity, damping: damping, distanceTraveled: newDistance) } return distanceTraveled } 

Small variations in distance traveled are also likely to be associated with small changes in your frame rate. When changing dampingOccurancesPerSecond from 60 → 50, the total distance traveled from changing 52.1203109538336 → 51.8808576268828

Hope this helps!

+2
source share

All Articles