Page 1 of 1

Distance and speed calculation

Posted: Wed Sep 24, 2014 12:08 pm
by PartyCow
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')

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

Re: Distance and speed calculation

Posted: Wed Sep 24, 2014 2:25 pm
by artofwork
Kind of clueless to the purpose of this code.

However if you give speed a static value like say 24 it reaches its goal to the circle on the right but only makes it half way to the circle on the left.

From playing around with the code a little bit i see your using the same loop with this fragment of code

Code: Select all

love.graphics.circle("fill", pos11+math.cos(angle)*(contick*speed), pos12+math.sin(angle)*(contick*speed), 3)
There is 3 ways you can go about changing the distance of the circle on the left to reach its destination, 1 you can use a modifier to effect the 2nd sphere, 2 use a separate loop or 3 write a function to handle the objects separately

Re: Distance and speed calculation

Posted: Wed Sep 24, 2014 10:12 pm
by davisdude
If you are asking about the amount you need to increment each time, it depends on how long you want them to take, and how fancy you want to make it.
In general, the equation for constant acceleration is:

Code: Select all

V = Δx / t
(note that "Δ" means "the change in," so Δx is x1 - x2).
If you want it to increment a certain amount based on the time, you would have a function like this:

Code: Select all

function GetIncrementedDistance( Position1, Position2, Time ) 
    local Difference = Position1 - Position2
    local VelocityX = Difference / Time
end

Re: Distance and speed calculation

Posted: Thu Sep 25, 2014 4:38 am
by ivan
Speed formula:

Code: Select all

-- speed is the distance traveled per unit of time
speed = distance/time
distance = speed*time
time = distance/speed
-- velocity is the speed and direction traveled per unit of time
velocity = heading_vector*speed
heading_vector.x = cos(heading_angle_in_radians)
heading_vector.y = sin(heading_angle_in_radians)
Acceleration formula:

Code: Select all

-- acceleration is the change in velocity per unit of time
acceleration = change_in_velocity/time
change_in_velocity = final_velocity - initial_velocity
Acceleration is used as follows:

Code: Select all

function update_position(dt)
  velocity = velocity + acceleration*dt
  position = position + velocity*dt
end
Where dt is the update interval.
When using acceleration, you usually want to limit the maximum velocity.

Code: Select all

  if length(velocity) > velocity_limit then
    velocity = normalize(velocity)*velocity_limit
  end