How to move platform with velocity

前端 未结 1 1036
生来不讨喜
生来不讨喜 2021-01-15 16:02

I need to move a node but instead of SKAction to use velocity Because in this question Moving Node on top of a moving platform

I want the node to move with the platf

相关标签:
1条回答
  • 2021-01-15 16:30

    You should edit this one to make it more clear. It looks like you want to use the physics engine to achieve the moving platform effect instead of using SKActions because you need your platforms to be dynamic and interact with the physics engine (like in this example).

    So you have two options. The first is to move your node to a point, and keep checking if the node arrived at that point. If it did, then move the node back to its starting point. To move a node to a particular point using real-time motion, you can see my answer here. If your platforms only move in one direction (horizontal or vertical) then you should only apply the velocity in that direction.

    Another approach I often use when moving platforms is centripetal motion. This will allow you to move platforms in a circle. What's even cooler is that if you restrict the centripetal motion to one direction (horizontal or vertical) then you can move the platform easily and get a perfect ease-in and ease-out effect. You can see an example of how to simulate real-time centripetal motion in my answer here.

    Below is the code for moving the platform horizontally by exploiting this centripetal motion effect I described above. What's nice about this is that it allows you to set the radius as well as the period of the platform's motion. But if you need your platform to travel some arbitrary path of points this won't work, so you will need to resort to using the first option that I mentioned.

    class GameScene: SKScene {
        var platform: SKSpriteNode!
        var platformAngularDistance: CGFloat = 0
    
        override func didMoveToView(view: SKView) {
            physicsWorld.gravity = CGVector(dx: 0, dy: 0)
            platform = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 80, height: 20))
            platform.position = CGPoint(x: self.size.width/2.0+50, y: self.size.height/2.0)
            platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size)
            self.addChild(platform)
        }
        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 = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0) //Point to orbit.
            let orbitRadius: CGFloat = 50 /*CGPoint(x: 50, y: 50)*/ //Radius of orbit.
            let normal = CGVector(dx:orbitPosition.x + CGFloat(cos(self.platformAngularDistance)) * orbitRadius, dy:0 /*orbitPosition.y + CGFloat(sin(self.node2AngularDistance))*orbitRadius.y*/)
            self.platformAngularDistance += (CGFloat(M_PI)*2.0)/period*dt;
            if (self.platformAngularDistance>CGFloat(M_PI)*2)
            {
                self.platformAngularDistance = 0
            }
            if (self.platformAngularDistance < 0) {
                self.platformAngularDistance = CGFloat(M_PI)*2
            }
            platform.physicsBody!.velocity = CGVector(dx:(normal.dx-platform.position.x)/dt ,dy:0/*(normal.dy-platform.position.y)/dt*/);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题