Speed depending on a fixed duration ?

Started by MetalZelda, May 04, 2017, 11:38:37 PM

Previous topic - Next topic
May 04, 2017, 11:38:37 PM Last Edit: May 04, 2017, 11:40:55 PM by MetalZelda
hi

So while working on some stuffs, primarly cutscenes, something gets my attention.
Let's assume that the hero is at a fixed point (let's say X 100 Y 200)
The camera in situation 1 will be set at X 300 Y 200
The camera in situation 2 will be set at X 800 Y 150
These camera have as target, the hero

so, if we set a speed in the most basic way, let's say 500 and start the movement on the camera, the arrival time will not be the same. This is what leads me to this thread

Is there any calculation that will allow the camera in both situation 1 and 2 that will allow the camera to arrive to the hero at, let's say, 2 seconds, with speed calculated by this arrival time ?

I already know that the calculation will include the default speed (500) and the target arrival time (2000 ms)

Any help will be appreciated

The equation you want to keep in mind is the following:
distance = rate * time -> rate = distance / time

So first calculate the distance to the hero from both cameras (find hypothenuse of right angle triangle made up of the delta x and delta y distances from camera to hero)

Code (lua) Select
local hero = {x=100, y=200}
local camera1 = {x=300, y=200}
local camera2 = {x=800, y=150}

local distance_1 = math.sqrt( (hero.x - camera1.x)^2 + (hero.y - camera1.y)^2 )
--> distance_1 = math.sqrt( (100 - 300)^2 + (200 - 200)^2 )
--> distance_1 = math.sqrt( -200^2 + 0^2)
--> distance_1 = 200 pixels

local distance_2 =  math.sqrt( (hero.x - camera2.x)^2 + (hero.y - camera2.y)^2 )
--> distance_2 = math.sqrt( (100 - 800)^2 + (200 - 150)^2 )
--> distance_2 = math.sqrt( -700^2 + 50^2 )
--> distance_2 = math.sqrt( 490000 + 2500)
--> distance_2 = 701.78 pixels


Then to find the speed of the camera (rate), simply divide the distance in pixels by time in seconds:

Code (lua) Select
local rate_1 = 200 / 2 --this is the camera_1 speed
--> rate_1 = 100 pixels per second

local rate_2 = 701.78 / 2 --this is the camera_2 speed
--> rate_2 = 350.89 pixels per second


Another way to think about it is that since the distance of the 2nd camera is 3.5x the distance of the first camera, the speed of the second camera should be 3.5 times the speed of the first camera.

May 05, 2017, 12:52:02 PM #2 Last Edit: May 05, 2017, 01:01:41 PM by MetalZelda
I thing I got it right, so, this should be right ?

Code (lua) Select

local speed = sol.main.get_speed({
  target1 = {x = xx2, y = yy2},
  target2 = {x = x2 , y = y2},
  duration = 650,
})


Code (lua) Select
-- Function to have a speed depending on a distance
-- The target speed is dependant of the time
function sol.main.get_speed(speed)
  local target1 = {x = speed.target1.x, y = speed.target1.y}
  local target2 = {x = speed.target2.x, y = speed.target2.y}
 
  local result  = math.sqrt((target1.x - target2.x)^2 + (target1.y - target2.y)^2)
 
  return result / 2 * (speed.duration / 100) -- Speed may vary depending on the given duration
end


EDIT, might not be the right solution, if I test to measure the duration between the 2 movement, the arrival time isn't the same, even if a duration has been set

Quote from: MetalZelda on May 05, 2017, 12:52:02 PM
I thing I got it right, so, this should be right ?

Code (lua) Select

local speed = sol.main.get_speed({
  target1 = {x = xx2, y = yy2},
  target2 = {x = x2 , y = y2},
  duration = 650,
})


Code (lua) Select
-- Function to have a speed depending on a distance
-- The target speed is dependant of the time
function sol.main.get_speed(speed)
  local target1 = {x = speed.target1.x, y = speed.target1.y}
  local target2 = {x = speed.target2.x, y = speed.target2.y}
 
  local result  = math.sqrt((target1.x - target2.x)^2 + (target1.y - target2.y)^2)
 
  return result / 2 * (speed.duration / 100) -- Speed may vary depending on the given duration
end


EDIT, might not be the right solution, if I test to measure the duration between the 2 movement, the arrival time isn't the same, even if a duration has been set

Some suggestions:
1-I might be wrong, but line 9 seems completely wrong, or I do not understand how you are doing this.
2-If you are only using that "sol.main.get_speed" in 1 cutscene, it is better to define it as a local function (because it is not needed elsewhere).
3-I would give the duration in milliseconds, but that is just a personal choice.
4-There is already a function sol.main.get_distance, which would shorten a bit line 7, and the code would be equivalent.

I propose the following code:
Code (Lua) Select

local hx, hy = hero:get_position()
local c1_x, c1_y = 300, 200 -- Initial position for the camera.
local c2_x, c2_y = 800, 150 -- Final position for the camera.
local duration = 2000 -- In milliseconds. Duration of the movement.
local distance = sol.main.get_distance(c1_x, c1_y, c2_x, c2_y)
local speed = distance * (1000 / duration) -- In pixels per second.

"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

May 05, 2017, 08:28:05 PM #4 Last Edit: May 05, 2017, 10:47:06 PM by MetalZelda
Somethings weird

Movement speed doesn't apply for the camera if the movement is camera -> entity

Code (lua) Select
local speed =  sol.main.get_speed({
  target1 = {x = hx, y = hy},
  target2 = {x = tx , y = ty},
  duration = 5000,
})
  movement:set_target(camera:get_position_to_track(hx, hy))
  movement:set_speed(speed)
 
  print(speed, movement:get_speed())


I'm using Diarandor's solution, but it's not the faulty thing here.
Targets are set, but when I print these value, this is what I get

79.8, 100
Seems like camera doesn't accept another speed when in manual ?

Weirdly, camera speed fluctuate between 96 and 100

I cannot find any problem. Maybe we need more info to help, or it could be an engine problem (maybe with target movements?).
As a possible workaround, try to move the camera with a straight movement, and tell us if the speed change works.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

May 06, 2017, 12:04:56 AM #6 Last Edit: May 06, 2017, 12:43:09 AM by MetalZelda
You're right, it works perfectly with a straight movement, so there is still some problems regarding target type movement

Anyway, thanks  :)