Advertisement
Guest User

Untitled

a guest
Feb 14th, 2025
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.57 KB | None | 0 0
  1. local Class = require("middleclass")
  2.  
  3. local Balls = {}
  4.  
  5. local Ball = Class("Ball")
  6. Ball.radius = 2
  7.  
  8. function Ball:initialize()
  9.     self.x = love.math.random() * love.graphics.getWidth()
  10.     self.y = love.math.random() * love.graphics.getHeight()
  11.  
  12.     local angle = love.math.random() * math.pi * 2
  13.     local speed = 30
  14.     self.vx = speed * math.cos(angle)
  15.     self.vy = speed * math.sin(angle)
  16. end
  17.  
  18. function Ball:update(dt, index)
  19.     self.x = self.x + self.vx * dt
  20.     self.y = self.y + self.vy * dt
  21.  
  22.     self:checkWallCollision()
  23.     self:checkBallCollisions(index)
  24. end
  25.  
  26. function Ball:checkWallCollision()
  27.     if (self.x < 0) then
  28.         self.x = 0
  29.         self.vx = self.vx * -1
  30.     end
  31.  
  32.     if (self.x > love.graphics.getWidth()) then
  33.         self.x = love.graphics.getWidth()
  34.         self.vx = self.vx * -1
  35.     end
  36.  
  37.     if (self.y < 0) then
  38.         self.y = 0
  39.         self.vy = self.vy * -1
  40.     end
  41.  
  42.     if (self.y > love.graphics.getHeight()) then
  43.         self.y = love.graphics.getHeight()
  44.         self.vy = self.vy * -1
  45.     end
  46. end
  47.  
  48. function Ball:checkBallCollisions(index)
  49.     for i = index + 1, #Balls do
  50.         local other = Balls[i]
  51.  
  52.         local dx = other.x - self.x
  53.         local dy = other.y - self.y
  54.         local distanceSquared = dx * dx + dy * dy
  55.         local combinedRadius = self.radius + other.radius
  56.         local combinedRadiusSquared = combinedRadius * combinedRadius
  57.  
  58.         if distanceSquared < combinedRadiusSquared then
  59.             local midX = (self.x + other.x) * 0.5
  60.             local midY = (self.y + other.y) * 0.5
  61.  
  62.             local normalX = (self.x - midX) / combinedRadius
  63.             local normalY = (self.y - midY) / combinedRadius
  64.  
  65.             self.x = midX + normalX * combinedRadius * 1.01
  66.             self.y = midY + normalY * combinedRadius * 1.01
  67.  
  68.             other.x = midX - normalX * combinedRadius * 1.01
  69.             other.y = midY - normalY * combinedRadius * 1.01
  70.  
  71.             self.vx, other.vx = other.vx, self.vx
  72.             self.vy, other.vy = other.vy, self.vy
  73.         end
  74.     end
  75. end
  76.  
  77. function Ball:draw()
  78.     love.graphics.circle("fill", self.x, self.y, self.radius)
  79. end
  80.  
  81. local function spawnBalls(amount)
  82.     for i = 1, amount do
  83.         table.insert(Balls, Ball())
  84.     end
  85. end
  86.  
  87. function love.load()
  88.     spawnBalls(8000)
  89. end
  90.  
  91. function love.update(dt)
  92.     for i, ball in ipairs(Balls) do
  93.         ball:update(dt, i)
  94.     end
  95. end
  96.  
  97. function love.draw()
  98.     for _, ball in ipairs(Balls) do
  99.         ball:draw()
  100.     end
  101.  
  102.     love.graphics.print(love.timer.getFPS())
  103. end
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement