im writing a bouncing ball simulation in swift UI. I can't understand how to declare "other" so that this code I am showing below will detect and react to collisions with other balls.
If "self" is used to reference the current instance of a class, how do I manipulate the relationship with other instances?
I know "self" is part of swift, but how about "other"? how does one reference other balls in the simulation? how do I reference any / all other instances of my "Ball" class?
The error message says "cannot find "other" in scope"
vx and vy are velocities, dx and dy are distances
I am new to swift...
If "self" is used to reference the current instance of a class, how do I manipulate the relationship with other instances?
I know "self" is part of swift, but how about "other"? how does one reference other balls in the simulation? how do I reference any / all other instances of my "Ball" class?
The error message says "cannot find "other" in scope"
vx and vy are velocities, dx and dy are distances
I am new to swift...
Code:
let dx: int = other.x - x
let dy: int = other.y - y
let distance: int = sqrt (dx * dx + dy * dy)
if distance < self.radius + other.radius
{
self.vx = -self.vx
self.vy = -self.vy
other.vx = -other.vx
other.vy = -other.vy
}
Last edited: