2017-06-19 49 views
2

我试图在SpriteKit中创建一个游戏,并且我需要一种布局对象的方法,将它们放置在随机生成的点中并不适合我,因此分组过多。泊松磁盘发生器不包括四个视图象限中的三个

经过一番研究,泊松磁盘生成器看起来像我所需要的。

我试图实现我自己的。但是,所有的位置都关闭了,它们在显示屏的3/4处没有显示。

我哪里错了?

在的100

r = 100

一分钟半径的10

r = 10

class GameScene: SKScene { 

    var radius = 100 
    var lookUpCount = 30 
    var grid = [CGPoint?](), ordered = [CGPoint](), active = [CGPoint]() 
    var w = CGFloat() 
    var cols = Int() 
    var rows = Int() 
    var sizeScren = CGSize() 


    override func didMove(to view: SKView) { 
     generate() 

     for item in ordered { 
      let gamePiece = SKSpriteNode(imageNamed: "Spaceship") 
      gamePiece.setScale(0.0625) 
      gamePiece.position = item 
      addChild(gamePiece) 
     } 
    } 

    func distance(p1: CGPoint, p2: CGPoint) -> CGFloat { 
     let dx = p1.x - p2.x 
     let dy = p1.y - p2.y 
     return sqrt(dx * dx + dy * dy) 
    } 

    func generateRandomPointAround(point: CGPoint, minDist: CGFloat) -> CGPoint 
    { 

     let rd1 = CGFloat(Float(arc4random())/Float(UINT32_MAX)) 
     let rd2 = CGFloat(Float(arc4random())/Float(UINT32_MAX)) 

     //random radius 
     let radius = minDist * (rd1 + 1) 
     //random angle 
     let angle = 2 * CGFloat.pi * rd2 

     //new point is generated around the point (x, y) 

     let newX = point.x + radius * cos(angle) 
     let newY = point.y + radius * sin(angle) 
     return CGPoint(x: newX, y: newY) 

    } 


    func generate() { 


     sizeScren = UIScreen.main.bounds.size 

     //create cell 
     w = (CGFloat(Double(radius)/sqrt(2))) 
     cols = Int(floor(sizeScren.height/w)) 
     rows = Int(floor(sizeScren.width/w)) 

     grid = [CGPoint?](repeating: nil, count: (cols * rows)) 

     let x = sizeScren.height/2 
     let y = sizeScren.width/2 
     let i = floor(x/w) 
     let j = floor(y/w) 

     //first posistion 

     let pos = CGPoint (x: frame.midX, y: frame.midY) 
     let index = Int(i + j * CGFloat(cols)) 
     grid[index] = pos 
     active.append(pos) 

     while (active.count > 0) { 
      let randomIndex = Int(arc4random_uniform(UInt32(active.count))) 
      let currentPos = active[randomIndex] 
      var found = false 
      Mainloop: for _ in 0..<Int(lookUpCount) { 

       let samplePoint = generateRandomPointAround(point: currentPos, minDist: CGFloat(radius)) 

       let col = floor(samplePoint.x/w) 
       let row = floor(samplePoint.y/w) 

       //check neighbouring cells are empty and valid, if have a point in already 

       if (col > -1 && row > -1 && CGFloat(col) < CGFloat(cols) && CGFloat(row) < CGFloat(rows) && (grid[Int(col + CGFloat(row) * CGFloat(cols))] == nil)) { 
        var ok = true 
        for index1 in -1...1 { 
         for index2 in -1...1 { 

          //break down complexity for swift compiler 

          let part1 = Int(col + CGFloat(index1)) 
          let part2 = Int(row + CGFloat(index2)) 
          let part3 = part1 + part2 * cols 
          let sampleIndex = part3 

          let isIndexValid = grid.indices.contains(sampleIndex) 

          if isIndexValid { 
           let neighbor = grid[sampleIndex] 

           if neighbor != nil { 
            let distanceAmount = distance(p1: samplePoint, p2: neighbor!) 
            if distanceAmount < CGFloat(radius) { 
             ok = false 
            } 

           } 

          } 
         } 
        } 

        if (ok == true) { 
         found = true 
         grid[Int(col + row * CGFloat(cols))] = samplePoint 
         active.append(samplePoint) 
         ordered.append(samplePoint) 
//      break MainLoop 
        } 
       } 
      } 

      if (!found) { 
       active.remove(at: randomIndex) 
      } 
     } 


    } 
} 
+3

是(0,0)的中心屏幕或其中一个角落?因为如果它是中心,那么'if(col> -1 && row> -1'将排除4个象限中的3个 – samgak

+0

(0,0)为中心,但我认为它不重要,因为应该有点我想我的问题更多的是将二维数组映射到一维数组上。 – Magrafear

回答

1

(0,0)一个最小半径是屏幕的中心,并且samplePoint被w除以行和列索引。然后用这个测试过滤掉所有的负行和列索引:if (col > -1 && row > -1 ...samplePoint之后没有调整,所以这将有效地消除4个象限中的3个。为了解决这个问题你可以抵消你的指数是这样的:

let col = floor((samplePoint.x + x)/w) 
let row = floor((samplePoint.y + y)/w) 

xy已经被定义为半个屏幕宽度和高度分别为)

相关问题