Hi, im wondering if anyone out there can correct my swift code. I am making a bouncing ball simulation in Xcode with Swift UI. It should have a grey background and 3 black balls that bounce off the floor, the right and left walls, and each other. Im realizing that my for loops for each of 3 circles/balls are totally out of whack with proper swift syntax. I am wondering if anyone can correct those parts of my code (while I try to learn swift) just as a fun way to learn the language
a bunch of lines have been commented out just because I don't know how to implement those concepts yet, as well as code involving color being commented out throughout the code. also, the "ball" vs "balls" in the second half is wrong, I haven't gotten there because I don't know how to do those things yet
any part of the program you can help me with is appreciated
a bunch of lines have been commented out just because I don't know how to implement those concepts yet, as well as code involving color being commented out throughout the code. also, the "ball" vs "balls" in the second half is wrong, I haven't gotten there because I don't know how to do those things yet
any part of the program you can help me with is appreciated
Code:
import SwiftUI
class Ball: ObservableObject, Identifiable
{
//var color: String
var xPosition: Int
var yPosition: Int
var xVelocity: Int
var yVelocity: Int
var radius: Int
var gravity: CGFloat
var restitution: Int
init (xPosition: Int, yPosition: Int, xVelocity: Int, yVelocity: Int, radius: Int, gravity: CGFloat, restitution: CGFloat)
// COLOR
//gravity and restitution values initialized???
{
//self.color = color
self.xPosition = xPosition
self.yPosition = yPosition
self.xVelocity = xVelocity
self.yVelocity = xVelocity
self.radius = radius
self.gravity = gravity
self.restitution = restitution
}
}
//COLOR
var ball1 = Ball(xPosition: 100, yPosition: 100, xVelocity: 3, yVelocity: 0, radius: 3, gravity: 0.3, restitution: 1)
var ball2 = Ball(xPosition: 200, yPosition: 50, xVelocity: -2, yVelocity: 2, radius: 3, gravity: 0.3, restitution: 1)
var ball3 = Ball(xPosition: 300, yPosition: 150, xVelocity: 4, yVelocity: -3, radius: 3, gravity: 0.3, restitution: 1)
//struct ContentView: View ???
//{ ???
var timer = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
// var body: some View
// {
// VStack
{
//Background color
Color.gray.edgesIgnoringSafeArea(.all)
for ball in balls
{
Circle()
.fill(Color.black)
.frame(width: 50, height: 50)
.position(balls[].xPosition, balls[].yPosition)
}
}
.onReceive(timer)
{
// _ in
for ball in balls
{
ball.yVelocity += 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 * restitution
}
if ball.xPosition <= 25
{
ball.xPosition = 25
ball.xVelocity = -ball.xVelocity
}
if ball.xPosition >= 375
{
ball.xPosition = 375
ball.xVelocity = -ball.velocityX
}
//Bounce off other balls
for balls in balls
{
let dx: int = balls[i].x - x
let dy: int = balls[i].y - y
let distance: int = sqrt (dx * dx + dy * dy)
if distance < self.radius + balls[i].radius
{
self.vx = -self.vx * restitution
self.vy = -self.vy * restitution
balls[i].vx = -balls[i].vx * restitution
balls[i].vy = -balls[i].vy * restitution
}
}
}
}