Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
krmpotic committed Sep 14, 2023
1 parent d906e66 commit 298cb5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
Binary file removed Snow_Ball.png
Binary file not shown.
37 changes: 12 additions & 25 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
function love.load()
b = love.graphics.newImage("Snow_Ball.png")
b_x, b_y = 100, 100
b_w, b_h = 36, 36
R = 20

v_x, v_y = 0, 0
love.graphics.setColor(255,255,255)
love.graphics.setBackgroundColor(0,0,0)

a = 0
a_k = 500
k_s = 2 -- slingshot strength
end

function love.draw()
local x, y = love.mouse.getPosition()
love.graphics.draw(b, b_x, b_y)
love.graphics.circle("fill", b_x, b_y, R)

local x, y = love.mouse.getPosition()
if love.mouse.isDown(1) then
love.graphics.line(x,y,x_press,y_press)
end
if love.mouse.isDown(2) then
love.graphics.line(x,y,b_x+b_w/2,b_y+b_h/2)
love.graphics.line(x,y,b_x,b_y)
end
love.graphics.print("k="..a_k .." a="..a, 10, 10)
end
Expand All @@ -28,21 +28,16 @@ function love.update(dt)
local x, y = love.mouse.getPosition()

if love.mouse.isDown(1) then
b_x = x - b_w / 2
b_y = y - b_h / 2
return -- quit function?
b_x = x
b_y = y
elseif love.mouse.isDown(2) then
local t = love.timer.getTime()
local dx, dy = x - b_x, y - b_y
local d = norm(dx, dy)
local d = math.max(d, 10)
-- local k = 500
-- a = k * (t - t_start) / (d*d)
k = 500
local d = math.sqrt(dx*dx+dy*dy)
local d = math.max(d, R) -- else acceleration gets huge at small distance
a = a_k / (d*d)
v_x = v_x + dx/d * a
v_y = v_y + dy/d * a

end

b_x = b_x + v_x * dt
Expand All @@ -54,23 +49,15 @@ function love.mousepressed(x, y, button, istouch)
x_press = x
y_press = y
end
if button == 2 then
t_start = love.timer.getTime()
end
end

function love.mousereleased(x, y, button, istouch)
if button == 1 then
local k = 1
v_x = k * (x_press - x)
v_y = k * (y_press - y)
v_x = k_s * (x_press - x)
v_y = k_s * (y_press - y)
end
end

function love.wheelmoved(x, y)
a_k = a_k + 20 * y
end

function norm(x, y)
return math.sqrt(x*x+y*y)
a_k = a_k + a_k / 20 * y -- 5% change
end

0 comments on commit 298cb5e

Please sign in to comment.