如何对圆形SKPhysicsBody施加摩擦力

时间:2014-06-16 15:18:29

标签: ios sprite-kit

我试图在SpriteKit中理解摩擦或物理学的其他方面。我创建了一个旋转的圆圈。我在旋转圆圈的顶部放置了一个正方形。我预计广场会随着圆圈一起旋转而掉落,但是方形不会移动。我设置physicsBody.friction = 1.0试图使表面变粘,但它没有帮助。我需要对物理进行哪些更改才能使方块受到圆周旋转的影响?

我使用Playground创建了一个自包含的示例。测试:

  1. 下载附件bluecircle.png并将其放在桌面上(或任何地方)
  2. 将以下代码复制到您的Playground
  3. 将NSImage路径更改为您保存的任何地方bluecircle.png
  4. 代码:

    import Cocoa
    import SpriteKit
    import XCPlayground
    
    let screenWidth = 1400.0
    let screenHeight = 1000.0
    let xOffset = 300.0
    
    let smallsquareSize = CGSize(width: 100, height: 100)
    let bluesquareSize = CGSize(width: 400 , height: 400)
    // the img is a blue circle w/ radius of 200 located on my desktop
    let img = NSImage(byReferencingFile: "/Users/CHANGETHIS/Desktop/bluecircle.png")
    
    class PlayScene: SKScene {
    
        let redsquare = SKSpriteNode(color: SKColor.redColor(), size: smallsquareSize)
        let greensquare = SKSpriteNode(color: SKColor.greenColor(), size: smallsquareSize)
        let bluesquare = SKSpriteNode(color: SKColor.blueColor(), size: bluesquareSize)
        let bluecircle = SKSpriteNode(texture: SKTexture(image: img))
    
        var lastTime = NSTimeInterval(0)
    
    
        init(size: CGSize) {
            super.init(size: size)
        }
    
    
        override func didMoveToView(view: SKView!) {
    
            backgroundColor = SKColor.blackColor()
    
            // RED SQUARE
            redsquare.position = CGPoint(x: screenWidth/2 - xOffset, y: screenHeight - 100)
            redsquare.physicsBody = SKPhysicsBody(rectangleOfSize: smallsquareSize)
            redsquare.physicsBody.friction = 1.0
            addChild(redsquare)
    
            // BLUE SQUARE
            bluesquare.position = CGPoint(x: screenWidth / 2 - xOffset, y: screenHeight / 2)
            bluesquare.physicsBody = SKPhysicsBody(rectangleOfSize: bluesquareSize)
            bluesquare.physicsBody.dynamic = false
            bluesquare.physicsBody.affectedByGravity = false
            bluesquare.physicsBody.friction = 1.0
            addChild(bluesquare)
    
    
            // GREEN SQUARE
            greensquare.position = CGPoint(x: screenWidth/2 + xOffset, y: screenHeight - 100)
            greensquare.physicsBody = SKPhysicsBody(rectangleOfSize: smallsquareSize)
            greensquare.physicsBody.friction = 1.0
            addChild(greensquare)
    
            // BLUE CIRCLE
            bluecircle.position = CGPoint(x: screenWidth / 2 + xOffset, y: screenHeight / 2)
            bluecircle.physicsBody = SKPhysicsBody(circleOfRadius: 200)
            bluecircle.physicsBody.dynamic = false
            bluecircle.physicsBody.affectedByGravity = false
            bluecircle.physicsBody.friction = 1.0
            addChild(bluecircle)
    
        }
    
        override func update(currentTime: NSTimeInterval) {
            let delta = currentTime - lastTime
            lastTime = currentTime
    
            bluesquare.zRotation += delta
            bluecircle.zRotation += delta
    
        }
    }
    
    var view = SKView(frame:NSRect(x: 0.0, y: 0.0, width: screenWidth, height: screenHeight))
    var playScene = PlayScene(size: CGSize(width: screenWidth, height: screenHeight))
    
    view.presentScene(playScene)
    view.showsFPS = true
    view.showsDrawCount = true
    view.showsNodeCount = true
    XCPShowView("View", view)
    

    `

    bluecircle.png Screenshot

1 个答案:

答案 0 :(得分:0)

我认为你问题的答案是关节。在你的节点之间创建一个固定的联合我相信会解决你的问题。

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsJoint_Ref/Reference/Reference.html