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
ios swift game-physics skphysicsbody
Sergio Toledo Piza
source share