Hi, I am new to swift. very new...im trying to make a bouncing ball simulation. balls will bounce off walls, floor, and each other. most of the program I've figured out on my own, but I still have one issue -
Im going to skip explaining how they bounce off the floor and the walls, and dive straight into them bouncing off each other.
I have a class called balls with three balls in it. then, I have a for loop updating each ball. I have to somehow reference any ball that could potentially hit the current ball that's being looped over. I asked chat gpt, and their answer was to declare "other" balls. I will share my code now so you can see (but keep in mind there are probably tons of errors. I am brand new to swift and kind of skipped a lot of fundamentals. I plan to continue studying swift alongside this simulation project, but I wanted to have some fun with it.) So the one main concern right now is referencing these "other " balls so that when they get within a certain distance of each other, they know to bounce off in the other direction
please help me understand how and when to reference the "other" balls that aren't being looped over at the current iteration
import SwiftUI
import Foundation
import CoreGraphics
class Balls
{
//var color: String
var xPosition: Int
var yPosition: Int
var xVelocity: Int
var yVelocity: Int
var radius: Int
var gravity: CGFloat
var restitution: Int
var other: Balls?
init(xPosition: Int, yPosition: Int, xVelocity: Int, yVelocity: Int, radius: Int, gravity: CGFloat, restitution: Int)
//ADD COLOR
{
//self.color = color
self.xPosition = xPosition
self.yPosition = yPosition
self.xVelocity = xVelocity
self.yVelocity = yVelocity
self.radius = radius
self.gravity = gravity
self.restitution = restitution
}
}
let ball1: Balls = Balls (xPosition: 100, yPosition: 100, xVelocity: 3, yVelocity: 0, radius: 3, gravity: 0.3, restitution: 1)
let ball2: Balls = Balls (xPosition: 200, yPosition: 50, xVelocity: -2, yVelocity: 2, radius: 3, gravity: 0.3, restitution: 1)
let ball3: Balls = Balls (xPosition: 300, yPosition: 150, xVelocity: 4, yVelocity: -3, radius: 3, gravity: 0.3, restitution: 1)
var timer = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
@State var balls: [Balls]
init()
{
balls = [ball1, ball2, ball3]
}
//struct ContentView: View
//{
var body: some View
{
VStack
{
Color.gray.edgesIgnoringSafeArea(.all)
ForEach($balls) { $ball in
{
Circle()
.fill(Color.black)
.frame(width: 50, height: 50)
.position($ball.xPosition, $ball.yPosition)
.onReceive(timer) { _in
ball.yVelocity += ball.gravity
ball.xPosition = CGPoint(ball.xPosition + ball.xVelocity)
ball .yPosition = CGPoint (ball.yPosition + ball.yVelocity)
if ball.yPosition >= 500 - 25
{
ball.yPosition = 500 - 25
ball.yVelocity = -ball.yVelocity * ball.restitution
}
if ball.xPosition <= 25
{
ball.xPosition = 25
ball.xVelocity = -ball.xVelocity
}
if ball.xPosition >= 375
{
ball.xPosition = 375
ball .xVelocity = -ball.velocityX
}
let dx: int = other.xPosition - ball.xPosition
let dy: int = other.yPosition - ball.yPosition
let distance: int = sqrt (dx * dx + dy * dy)
if distance < ball.radius + other.radius
{
ball.xVelocity = -ball.xVelocity * ball.restitution
ball.yVelocity = -ball.yVelocity * ball.restitution
other.xVelocity = -other.xVelocity * ball.restitution
other.yVelocity = -other.yVelocity * ball.restitution
}
}
}
}
}
}
#Preview
{
ContentView()
}