Distance and speed calculation
Posted: Wed Sep 24, 2014 12:08 pm
Hey there!
(Might be more math related than lua but w/e)
I'm trying to do some stuffz (wow, with a z!) move relative to the distance so they all reach their goal regardless of the distance.
Kinda bad with this kind of math, so just the correct formula would be appreciated.
main.lua
(The part I can't figure out is commented 'math')
(Might be more math related than lua but w/e)
I'm trying to do some stuffz (wow, with a z!) move relative to the distance so they all reach their goal regardless of the distance.
Kinda bad with this kind of math, so just the correct formula would be appreciated.
main.lua
(The part I can't figure out is commented 'math')
Code: Select all
function love.load()
windowx = 960
windowy = 540
settings={vsync=true,resizable=false,centered=true,fsaa=8}
love.window.setMode(windowx, windowy, settings)
centery = love.window:getHeight()/2
centerx = love.window:getWidth()/2
love.window.setTitle("Cönnected")
points = {{centerx,centery},{centerx-100,centery+100},{centerx+50,centery+50}}
drawing = {{1,2},{1,3}}
contick = 0
conspeed = 3
end
function distanceFrom(x1,y1,x2,y2) return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) end
function love.update(dt)
if contick >= conspeed then
contick = 0
end
contick = contick + dt
end
function love.draw()
love.graphics.setBackgroundColor(255,255,255)
love.graphics.setColor(0,0,100)
for i = 1,#drawing do
love.graphics.circle("line",points[drawing[i][1]][1],points[drawing[i][1]][2],5)
love.graphics.circle("line",points[drawing[i][2]][1],points[drawing[i][2]][2],5)
end
for i = 1,#drawing do
local pos11 = points[drawing[i][1]][1]
local pos12 = points[drawing[i][1]][2]
local pos21 = points[drawing[i][2]][1]
local pos22 = points[drawing[i][2]][2]
local angle = math.atan2(pos22 - pos12, pos21 - pos11)
local distance = distanceFrom(pos11,pos21,pos12,pos22)
local speed = --MATH
love.graphics.circle("fill", pos11+math.cos(angle)*(contick*speed), pos12+math.sin(angle)*(contick*speed), 3)
end
end