### Author Topic: Speed depending on a fixed duration ?  (Read 630 times)

#### MetalZelda

• Hero Member
• Posts: 544
##### Speed depending on a fixed duration ?
« on: May 04, 2017, 11:38:37 pm »
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
« Last Edit: May 04, 2017, 11:40:55 pm by MetalZelda »

#### llamazing

• Full Member
• Posts: 106
##### Re: Speed depending on a fixed duration ?
« Reply #1 on: May 05, 2017, 01:35:11 am »
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
1. local hero = {x=100, y=200}
2. local camera1 = {x=300, y=200}
3. local camera2 = {x=800, y=150}
4.
5. local distance_1 = math.sqrt( (hero.x - camera1.x)^2 + (hero.y - camera1.y)^2 )
6. --> distance_1 = math.sqrt( (100 - 300)^2 + (200 - 200)^2 )
7. --> distance_1 = math.sqrt( -200^2 + 0^2)
8. --> distance_1 = 200 pixels
9.
10. local distance_2 =  math.sqrt( (hero.x - camera2.x)^2 + (hero.y - camera2.y)^2 )
11. --> distance_2 = math.sqrt( (100 - 800)^2 + (200 - 150)^2 )
12. --> distance_2 = math.sqrt( -700^2 + 50^2 )
13. --> distance_2 = math.sqrt( 490000 + 2500)
14. --> 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
1. local rate_1 = 200 / 2 --this is the camera_1 speed
2. --> rate_1 = 100 pixels per second
3.
4. local rate_2 = 701.78 / 2 --this is the camera_2 speed
5. --> 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.

#### MetalZelda

• Hero Member
• Posts: 544
##### Re: Speed depending on a fixed duration ?
« Reply #2 on: May 05, 2017, 12:52:02 pm »
I thing I got it right, so, this should be right ?

Code: Lua
1. local speed = sol.main.get_speed({
2.                   target1 = {x = xx2, y = yy2},
3.                   target2 = {x = x2 , y = y2},
4.                   duration = 650,
5.                 })

Code: Lua
1. -- Function to have a speed depending on a distance
2. -- The target speed is dependant of the time
3. function sol.main.get_speed(speed)
4.   local target1 = {x = speed.target1.x, y = speed.target1.y}
5.   local target2 = {x = speed.target2.x, y = speed.target2.y}
6.
7.   local result  = math.sqrt((target1.x - target2.x)^2 + (target1.y - target2.y)^2)
8.
9.   return result / 2 * (speed.duration / 100) -- Speed may vary depending on the given duration
10. end
11.

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
« Last Edit: May 05, 2017, 01:01:41 pm by MetalZelda »

#### Diarandor

• Hero Member
• Posts: 935
• Cats are cool! （ΦωΦ）
##### Re: Speed depending on a fixed duration ?
« Reply #3 on: May 05, 2017, 02:13:34 pm »
I thing I got it right, so, this should be right ?

Code: Lua
1. local speed = sol.main.get_speed({
2.                   target1 = {x = xx2, y = yy2},
3.                   target2 = {x = x2 , y = y2},
4.                   duration = 650,
5.                 })

Code: Lua
1. -- Function to have a speed depending on a distance
2. -- The target speed is dependant of the time
3. function sol.main.get_speed(speed)
4.   local target1 = {x = speed.target1.x, y = speed.target1.y}
5.   local target2 = {x = speed.target2.x, y = speed.target2.y}
6.
7.   local result  = math.sqrt((target1.x - target2.x)^2 + (target1.y - target2.y)^2)
8.
9.   return result / 2 * (speed.duration / 100) -- Speed may vary depending on the given duration
10. end
11.

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
1. local hx, hy = hero:get_position()
2. local c1_x, c1_y = 300, 200 -- Initial position for the camera.
3. local c2_x, c2_y = 800, 150 -- Final position for the camera.
4. local duration = 2000 -- In milliseconds. Duration of the movement.
5. local distance = sol.main.get_distance(c1_x, c1_y, c2_x, c2_y)
6. local speed = distance * (1000 / duration) -- In pixels per second.
7.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

#### MetalZelda

• Hero Member
• Posts: 544
##### Re: Speed depending on a fixed duration ?
« Reply #4 on: May 05, 2017, 08:28:05 pm »
Somethings weird

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

Code: Lua
1. local speed =  sol.main.get_speed({
2.   target1 = {x = hx, y = hy},
3.   target2 = {x = tx , y = ty},
4.   duration = 5000,
5. })
6.   movement:set_target(camera:get_position_to_track(hx, hy))
7.   movement:set_speed(speed)
8.
9.   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

Code: [Select]
79.8, 100Seems like camera doesn't accept another speed when in manual ?

Weirdly, camera speed fluctuate between 96 and 100
« Last Edit: May 05, 2017, 10:47:06 pm by MetalZelda »

#### Diarandor

• Hero Member
• Posts: 935
• Cats are cool! （ΦωΦ）
##### Re: Speed depending on a fixed duration ?
« Reply #5 on: May 05, 2017, 11:21:18 pm »
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.”

#### MetalZelda

• Hero Member
• Posts: 544
##### Re: Speed depending on a fixed duration ?
« Reply #6 on: May 06, 2017, 12:04:56 am »
You're right, it works perfectly with a straight movement, so there is still some problems regarding target type movement

Anyway, thanks
« Last Edit: May 06, 2017, 12:43:09 am by MetalZelda »