Post

Replies

Boosts

Views

Activity

Reply to Swarm simulation
You would use SpriteKit for that. I'm new to it too, but I used a magnetic field around randomly moving objects to create something similar to a swarm. First of all random movement was quite a challenge. I haven't figured it out completely yet, but the movement now at least seems to be random.
Feb ’23
Reply to Swarm simulation
import SwiftUI import SpriteKit struct ContentView: View {     var scene: SKScene {         let scene = GameScene()         scene.size = CGSize(width: 400, height: 700)         scene.scaleMode = .fill         return scene     }          var body: some View {         SpriteView(scene: scene)             .frame(width: 400,height: 700)             .ignoresSafeArea()     } } class GameScene: SKScene{     var boxes: [SKShapeNode] = []     var lastUpdateTime: TimeInterval?          override func didMove(to view: SKView) {         view.preferredFramesPerSecond = 60         physicsBody = SKPhysicsBody(edgeLoopFrom: frame)     }          override func touchesBegan(_ touches: Set, with event: UIEvent?) {         guard let touches = touches.first else {return}         let location = touches.location(in: self)         let box = SKShapeNode(circleOfRadius: 5)         box.fillColor = .brown         box.position = location         box.physicsBody = SKPhysicsBody(circleOfRadius: 10)         box.physicsBody?.affectedByGravity = false         let magneticField = SKFieldNode.radialGravityField()         magneticField.strength = 1000         magneticField.falloff = 1.0         magneticField.region = SKRegion(radius: 10)         magneticField.physicsBody = box.physicsBody         magneticField.position = box.position         addChild(box)         boxes.append(box)     }          override func update(_ currentTime: TimeInterval) {         // Calculate time since last update         let deltaTime = currentTime - (lastUpdateTime ?? currentTime)                  // Update game state based on time since last update         updateGameState(deltaTime: deltaTime)                  // Store the current time as the last update time         lastUpdateTime = currentTime     }     private func updateGameState(deltaTime: TimeInterval) {         // Update game state based on time since last update         for box in boxes {             let dx = CGFloat.random(in: -50...50)             let dy = CGFloat.random(in: -50...50)             let randomMovement = SKAction.move(by: CGVector(dx: dx, dy: dy), duration: 1.0)                          // Check if box is beyond screen edges             let screenBounds = UIScreen.main.bounds             let minX = screenBounds.minX + box.frame.width/2             let maxX = screenBounds.maxX - box.frame.width/2             let minY = screenBounds.minY + box.frame.height/2             let maxY = screenBounds.maxY - box.frame.height/2             box.run(randomMovement)             if box.position.x < minX  {                     box.position.x = minX                              }else if box.position.x > maxX - 35 {                     box.position.x = maxX - 35                              } else if box.position.y < minY + 10 {                     box.position.y = minY + 10                              }else if box.position.y > maxY - 240 {                     box.position.y = maxY - 240                              }         }     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } }
Mar ’23